一、前言
基于directwebremoting包dwr-3.0.jar中的org.directwebremoting.util.CopyUtils复制拷贝工具类,对二进制数组转输出流copy、二进制转文本输出copy、标准输入输出流copy、标准输入输出文本copy等操作处理。
二、源码说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | package org.directwebremoting.util; import java.io.ByteArrayInputStream; 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.StringReader; import java.io.Writer; public class CopyUtils { private static final int DEFAULT_BUFFER_SIZE = 4096 ; public static void copy( byte [] input, OutputStream output) throws IOException { output.write(input); } public static void copy( byte [] input, Writer output) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(input); copy(in, output); } public static void copy( byte [] input, Writer output, String encoding) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(input); copy(in, output, encoding); } public static int copy(InputStream input, OutputStream output) throws IOException { byte [] buffer = new byte [ 4096 ]; int count = 0 ; int n = 0 ; while (- 1 != (n = input.read(buffer))) { output.write(buffer, 0 , n); count += n; } return count; } public static int copy(Reader input, Writer output) throws IOException { char [] buffer = new char [ 4096 ]; int count = 0 ; int n = 0 ; while (- 1 != (n = input.read(buffer))) { output.write(buffer, 0 , n); count += n; } return count; } public static void copy(InputStream input, Writer output) throws IOException { InputStreamReader in = new InputStreamReader(input); copy(in, output); } public static void copy(InputStream input, Writer output, String encoding) throws IOException { InputStreamReader in = new InputStreamReader(input, encoding); copy(in, output); } public static void copy(Reader input, OutputStream output) throws IOException { OutputStreamWriter out = new OutputStreamWriter(output); copy(input, out); out.flush(); } public static void copy(String input, OutputStream output) throws IOException { StringReader in = new StringReader(input); OutputStreamWriter out = new OutputStreamWriter(output); copy(in, out); out.flush(); } public static void copy(String input, Writer output) throws IOException { output.write(input); } } |