一、前言
基于c3p0包中com.mchange.v1.io.InputStreamUtils、com.mchange.v1.io.OutputStreamUtils输入输出流工具类对输入流InputStream对比、将输入流转换为字节数组getBytes、将输入流转换为字符串getContentsAsString等常用操作等。
二、源码说明
1.InputStreamUtils
package com.mchange.v1.io;@b@@b@import java.io.ByteArrayInputStream;@b@import java.io.ByteArrayOutputStream;@b@import java.io.EOFException;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.UnsupportedEncodingException;@b@@b@public final class InputStreamUtils@b@{@b@ private static InputStream EMPTY_ISTREAM = new ByteArrayInputStream(new byte[0]);@b@@b@ public static boolean compare(InputStream is1, InputStream is2, long num_bytes)@b@ throws IOException@b@ {@b@ for (long num_read = 0L; num_read < num_bytes; num_read += 1L)@b@ {@b@ int b;@b@ if ((b = is1.read()) != is2.read())@b@ return false;@b@ if (b < 0)@b@ break;@b@ }@b@ return true;@b@ }@b@@b@ public static boolean compare(InputStream is1, InputStream is2) throws IOException@b@ {@b@ int b = 0;@b@ while (b >= 0)@b@ if ((b = is1.read()) != is2.read())@b@ return false;@b@ return true;@b@ }@b@@b@ public static byte[] getBytes(InputStream is, int max_len) throws IOException@b@ {@b@ ByteArrayOutputStream baos = new ByteArrayOutputStream(max_len);@b@ int i = 0; for (int b = is.read(); (b >= 0) && (i < max_len); ) {@b@ baos.write(b);@b@@b@ b = is.read(); ++i;@b@ }@b@ return baos.toByteArray();@b@ }@b@@b@ public static byte[] getBytes(InputStream is) throws IOException@b@ {@b@ ByteArrayOutputStream baos = new ByteArrayOutputStream();@b@ for (int b = is.read(); b >= 0; ) { baos.write(b); b = is.read(); }@b@ return baos.toByteArray();@b@ }@b@@b@ public static String getContentsAsString(InputStream is, String enc) throws IOException, UnsupportedEncodingException@b@ {@b@ return new String(getBytes(is), enc);@b@ }@b@@b@ public static String getContentsAsString(InputStream is) throws IOException@b@ {@b@ try {@b@ return getContentsAsString(is, System.getProperty("file.encoding", "8859_1"));@b@ }@b@ catch (UnsupportedEncodingException e) {@b@ throw new InternalError("You have no default character encoding, and iso-8859-1 is unsupported?!?!");@b@ }@b@ }@b@@b@ public static String getContentsAsString(InputStream is, int max_len, String enc)@b@ throws IOException, UnsupportedEncodingException@b@ {@b@ return new String(getBytes(is, max_len), enc);@b@ }@b@@b@ public static String getContentsAsString(InputStream is, int max_len) throws IOException@b@ {@b@ try {@b@ return getContentsAsString(is, max_len, System.getProperty("file.encoding", "8859_1"));@b@ }@b@ catch (UnsupportedEncodingException e) {@b@ throw new InternalError("You have no default character encoding, and iso-8859-1 is unsupported?!?!");@b@ }@b@ }@b@@b@ public static InputStream getEmptyInputStream()@b@ {@b@ return EMPTY_ISTREAM;@b@ }@b@@b@ public static void attemptClose(InputStream is) {@b@ try {@b@ if (is != null) is.close();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ }@b@@b@ public static void skipFully(InputStream is, long num_bytes) throws EOFException, IOException {@b@ long num_skipped = 0L;@b@ while (num_skipped < num_bytes)@b@ {@b@ long just_skipped = is.skip(num_bytes - num_skipped);@b@ if (just_skipped > 0L) {@b@ num_skipped += just_skipped;@b@ }@b@ else {@b@ int test_byte = is.read();@b@ if (is.read() < 0)@b@ throw new EOFException("Skipped only " + num_skipped + " bytes to end of file.");@b@@b@ num_skipped += 1L;@b@ }@b@ }@b@ }@b@}
2.OutputStreamUtils
package com.mchange.v1.io;@b@@b@import java.io.IOException;@b@import java.io.OutputStream;@b@@b@public final class OutputStreamUtils@b@{@b@ public static void attemptClose(OutputStream os)@b@ {@b@ try@b@ {@b@ if (os != null) os.close();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ }@b@}