一、前言
基于directwebremoting包dwr-3.0.jar中的org.directwebremoting.util.CopyUtils复制拷贝工具类,对二进制数组转输出流copy、二进制转文本输出copy、标准输入输出流copy、标准输入输出文本copy等操作处理。
二、源码说明
package org.directwebremoting.util;@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@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@ {@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@ {@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@}