Java tutorial
/* * Copyright 2004-2012 the Seasar Foundation and the Others. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ package org.seasar.mayaa.impl.util; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import java.net.URL; import java.net.URLConnection; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.seasar.mayaa.impl.CONST_IMPL; /** * @author Koji Suga (Gluegent, Inc.) */ public class IOUtil { private static final Log LOG = LogFactory.getLog(IOUtil.class); private static boolean _useURLCache = false; static { // ??????????? String env = System.getProperty("org.seasar.mayaa.useURLCache"); if (env != null && env.equalsIgnoreCase("true")) { _useURLCache = true; } } private IOUtil() { // no instantiation. } /** * InputStreamclose? * ??????INFO?? * * @param stream close?InputStream */ public static void close(InputStream stream) { if (stream != null) { try { stream.close(); } catch (IOException ignore) { LOG.info(ignore.getMessage(), ignore); } } } /** * Readerclose? * ??????INFO?? * * @param reader close?Reader */ public static void close(Reader reader) { if (reader != null) { try { reader.close(); } catch (IOException ignore) { LOG.info(ignore.getMessage(), ignore); } } } /** * OutputStreamclose? * ??????INFO?? * * @param stream close?OutputStream */ public static void close(OutputStream stream) { if (stream != null) { try { stream.close(); } catch (IOException ignore) { LOG.info(ignore.getMessage(), ignore); } } } /** * InputStream??String???? * ??????InputStreamclose? * * @param is ??InputStream * @param encoding * @return InputStream? * @throws RuntimeException ?????? */ public static String readStream(InputStream is, String encoding) { try { Reader reader = new InputStreamReader(is, encoding); try { StringBuffer sb = new StringBuffer(); char[] buffer = new char[1024]; int readSize = -1; while ((readSize = reader.read(buffer)) > 0) { sb.append(buffer, 0, readSize); } return sb.toString(); } finally { is.close(); } } catch (Throwable t) { throw new RuntimeException(t); } } /** * OutputStream??String??? * ???????OutputStreamclose? * * @param os ???InputStream * @param value ? * @param encoding * @throws RuntimeException ?????? */ public static void writeStream(OutputStream os, String value, String encoding) { try { Writer writer = new OutputStreamWriter(os, encoding); try { writer.write(value); writer.flush(); } finally { os.close(); } } catch (Throwable t) { throw new RuntimeException(t); } } /** * URL?InputStream?? * ??URL???? * ?????jar????? * ?????? * url?null???null? * * URL????????? * "org.seasar.mayaa.useURLCache=true" ??? * * @param url ?URL * @return InputStream */ public static InputStream openStream(URL url) { if (url == null) { return null; } try { URLConnection connection = url.openConnection(); // ??jar???? // TODO useURLCache?????? connection.setUseCaches(_useURLCache); return connection.getInputStream(); } catch (IOException e) { throw new RuntimeException(e); } } /** * URL?????? * url?null???????false? * * @param url ?URL * @return ?true */ public static File getFile(URL url) { if (url != null) { if ("file".equalsIgnoreCase(url.getProtocol())) { return new File(url.toString().substring(5)); } } return null; } /** * URL??? * file??????{@link CONST_IMPL.NULL_DATE_MILLIS}? * * @param url ?URL * @return InputStream */ public static long getLastModified(URL url) { File file = getFile(url); if (file != null) { return file.lastModified(); } // TODO ServletContext#getResource ? file ????? jndi ? URL ????? return CONST_IMPL.NULL_DATE_MILLIS; } /** * ?ContextClassLoader???InputStream? * ????????????????null? * ???? * * @param name ???? * @return InputStream */ public static URL getResource(String name) { return getResource(name, Thread.currentThread().getContextClassLoader()); } /** * ?ContextClassLoader???InputStream? * ????????????????null? * ???? * * @param name ???? * @return InputStream */ public static InputStream getResourceAsStream(String name) { return getResourceAsStream(name, Thread.currentThread().getContextClassLoader()); } /** * ?????? * java.lang.Class#resolveName(String) * * @param neighbor ?? * @param name ???? * @return ?? */ protected static String resolveName(Class neighbor, String name) { if (name == null) { return name; } if (name.startsWith("/") == false) { Class c = neighbor; while (c.isArray()) { c = c.getComponentType(); } String baseName = c.getName(); int index = baseName.lastIndexOf('.'); if (index != -1) { name = baseName.substring(0, index).replace('.', '/') + '/' + name; } } else { name = name.substring(1); } return name; } /** * neighbor?ClassLoader???URL? * ????????????????null? * ???? * neighbor?null????ContextClassLoader? * * @param name ???? * @param neighbor ?????? * @return InputStream */ public static URL getResource(String name, Class neighbor) { if (neighbor == null) { return getResource(name); } return getResource(resolveName(neighbor, name), neighbor.getClassLoader()); } /** * neighbor?ClassLoader???InputStream? * ????????????????null? * ??neighbor??? * neighbor?null????ContextClassLoader? * * @param name ???? * @param neighbor ?????? * @return InputStream */ public static InputStream getResourceAsStream(String name, Class neighbor) { return openStream(getResource(name, neighbor)); } /** * ??ClassLoader???URL? * ????????????????null? * ???? * loader?null???null? * * @param name ???? * @param loader ???? * @return InputStream */ public static URL getResource(String name, ClassLoader loader) { if (loader == null) { return null; } return loader.getResource(name); } /** * ??ClassLoader???InputStream? * ????????????????null? * ???? * loader?null???null? * * @param name ???? * @param loader ???? * @return InputStream */ public static InputStream getResourceAsStream(String name, ClassLoader loader) { return openStream(getResource(name, loader)); } }