一、前言
关于apache的commons-io源码包org.apache.commons.io.CopyUtils数据拷贝工具类,进行二进制数据与字节文本输出流OutputStream/Writer间复制copy操作、输入字节流InputStream与输出文本Writer间复制拷贝操作、输入文本Reader与输出流OutputStream复制拷贝操作、字符串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@@Deprecated@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@}