一、前言
基于granite源码包org.granite.util.URIUtil资源访问链接工具类,通过URL分别获取getInputStream输入流、getContentAsString字符串、getContentAsBytes字节数组及最新修改时间lastModified等。
二、源码说明
package org.granite.util;@b@@b@import java.io.BufferedInputStream;@b@import java.io.BufferedReader;@b@import java.io.ByteArrayOutputStream;@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.InputStreamReader;@b@import java.net.JarURLConnection;@b@import java.net.URI;@b@import java.net.URL;@b@import java.net.URLConnection;@b@import java.nio.charset.Charset;@b@import java.util.jar.JarEntry;@b@@b@public class URIUtil@b@{@b@ public static final String CLASSPATH_SCHEME = "class";@b@@b@ public static String normalize(String uri)@b@ {@b@ if (uri == null)@b@ return null;@b@@b@ if ((File.separatorChar == '\\') && (uri.startsWith("file://")))@b@ uri = uri.replace('\\', '/');@b@@b@ uri = uri.replace(" ", "%20");@b@@b@ return uri;@b@ }@b@@b@ public static InputStream getInputStream(URI uri, ClassLoader loader) throws IOException {@b@ InputStream is = null;@b@@b@ String scheme = uri.getScheme();@b@ if ("class".equals(scheme)) {@b@ if (loader != null)@b@ is = loader.getResourceAsStream(uri.getSchemeSpecificPart());@b@ if (is != null) break label110;@b@ is = Thread.currentThread().getContextClassLoader().getResourceAsStream(uri.getSchemeSpecificPart());@b@ if (is != null) break label110;@b@ throw new IOException("Resource not found exception: " + uri);@b@ }@b@@b@ if ((scheme == null) || (scheme.length() <= 1))@b@ is = new FileInputStream(uri.toString());@b@ else@b@ is = uri.toURL().openStream();@b@@b@ label110: return is;@b@ }@b@@b@ public static InputStream getInputStream(URI uri) throws IOException {@b@ return getInputStream(uri, null);@b@ }@b@@b@ public static String getContentAsString(URI uri) throws IOException {@b@ return getContentAsString(uri, Charset.defaultCharset()); }@b@@b@ public static String getContentAsString(URI uri, Charset charset) throws IOException {@b@ InputStream is = null;@b@ try {@b@ String str;@b@ is = getInputStream(uri);@b@ BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset));@b@@b@ StringBuilder sb = new StringBuilder(1024);@b@@b@ char[] chars = new char[256];@b@ int count = -1;@b@ while ((count = reader.read(chars)) != -1)@b@ sb.append(chars, 0, count);@b@@b@ return sb.toString();@b@ } finally {@b@ if (is != null)@b@ is.close();@b@ }@b@ }@b@@b@ public static byte[] getContentAsBytes(URI uri) throws IOException {@b@ InputStream is = null;@b@ try {@b@ byte[] arrayOfByte;@b@ is = new BufferedInputStream(getInputStream(uri));@b@@b@ ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);@b@@b@ int b = 0;@b@ while ((b = is.read()) != -1)@b@ baos.write(b);@b@@b@ return baos.toByteArray();@b@ } finally {@b@ if (is != null)@b@ is.close();@b@ }@b@ }@b@@b@ public static long lastModified(URI uri) throws IOException {@b@ if (uri == null)@b@ return -1L;@b@@b@ String scheme = uri.getScheme();@b@ if ((scheme == null) || (scheme.length() <= 1))@b@ return new File(uri).lastModified();@b@ return lastModified(uri.toURL());@b@ }@b@@b@ public static long lastModified(URL url) throws IOException {@b@ long lastModified = -1L;@b@@b@ if (url != null) {@b@ URLConnection connection = url.openConnection();@b@ if (connection instanceof JarURLConnection) {@b@ JarEntry entry = ((JarURLConnection)connection).getJarEntry();@b@ if (entry != null)@b@ lastModified = entry.getTime();@b@ }@b@ if (lastModified == -1L)@b@ lastModified = connection.getLastModified();@b@ }@b@@b@ return ((lastModified == 0L) ? -1L : lastModified);@b@ }@b@}