一、前言
基于apache-rat包(0.9)中org.apache.commons.io.CopyUtils复制工具类,对常用的输入字节数组byte[]、输入流InputStream、读入Reader、字符串String方式,输出为写出流OutputStream、写出Writer等方式间常用的转换处理操作等。
二、源码说明
package org.apache.commons.io;@b@@b@import java.io.ByteArrayInputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.InputStreamReader;@b@import java.io.OutputStream;@b@import java.io.OutputStreamWriter;@b@import java.io.Reader;@b@import java.io.StringReader;@b@import java.io.Writer;@b@@b@/**@b@ * @deprecated@b@ */@b@public class CopyUtils@b@{@b@ private static final int DEFAULT_BUFFER_SIZE = 4096;@b@@b@ public static void copy(byte[] input, OutputStream output)@b@ throws IOException@b@ {@b@ output.write(input);@b@ }@b@@b@ public static void copy(byte[] input, Writer output)@b@ throws IOException@b@ {@b@ ByteArrayInputStream in = new ByteArrayInputStream(input);@b@ copy(in, output);@b@ }@b@@b@ public static void copy(byte[] input, Writer output, String encoding)@b@ throws IOException@b@ {@b@ ByteArrayInputStream in = new ByteArrayInputStream(input);@b@ copy(in, output, encoding);@b@ }@b@@b@ public static int copy(InputStream input, OutputStream output)@b@ throws IOException@b@ {@b@ byte[] buffer = new byte[4096];@b@ int count = 0;@b@ int n = 0;@b@ while (-1 != (n = input.read(buffer))) {@b@ output.write(buffer, 0, n);@b@ count += n;@b@ }@b@ return count;@b@ }@b@@b@ public static int copy(Reader input, Writer output)@b@ throws IOException@b@ {@b@ char[] buffer = new char[4096];@b@ int count = 0;@b@ int n = 0;@b@ while (-1 != (n = input.read(buffer))) {@b@ output.write(buffer, 0, n);@b@ count += n;@b@ }@b@ return count;@b@ }@b@@b@ public static void copy(InputStream input, Writer output)@b@ throws IOException@b@ {@b@ InputStreamReader in = new InputStreamReader(input);@b@ copy(in, output);@b@ }@b@@b@ public static void copy(InputStream input, Writer output, String encoding)@b@ throws IOException@b@ {@b@ InputStreamReader in = new InputStreamReader(input, encoding);@b@ copy(in, output);@b@ }@b@@b@ public static void copy(Reader input, OutputStream output)@b@ throws IOException@b@ {@b@ OutputStreamWriter out = new OutputStreamWriter(output);@b@ copy(input, out);@b@@b@ out.flush();@b@ }@b@@b@ public static void copy(String input, OutputStream output)@b@ throws IOException@b@ {@b@ StringReader in = new StringReader(input);@b@ OutputStreamWriter out = new OutputStreamWriter(output);@b@ copy(in, out);@b@@b@ out.flush();@b@ }@b@@b@ public static void copy(String input, Writer output)@b@ throws IOException@b@ {@b@ output.write(input);@b@ }@b@}