1 16 17 package org.apache.taglibs.io; 18 19 import java.beans.BeanInfo ; 20 import java.beans.Introspector ; 21 import java.beans.IntrospectionException ; 22 import java.beans.PropertyDescriptor ; 23 import java.lang.reflect.Method ; 24 import java.io.BufferedReader ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.InputStreamReader ; 28 import java.io.OutputStream ; 29 import java.io.OutputStreamWriter ; 30 import java.io.StringReader ; 31 import java.io.Reader ; 32 import java.io.Writer ; 33 34 import javax.servlet.jsp.tagext.Tag ; 35 import javax.servlet.jsp.tagext.TagSupport ; 36 37 42 public class PipeHelper { 43 44 45 protected static final int BUFFER_SIZE = 64 * 1024; 46 47 48 protected static final boolean CLOSE_READER = false; 49 50 51 58 public static Reader getReader(Object input) { 59 if ( input instanceof Reader ) { 60 return (Reader ) input; 61 } 62 else if ( input instanceof InputStream ) { 63 return new InputStreamReader ( (InputStream ) input ); 64 } 65 else if ( input instanceof String ) { 66 return new StringReader ( (String ) input ); 67 } 68 return null; 69 } 70 71 78 public static Writer getWriter(Object output) { 79 if ( output instanceof Writer ) { 80 return (Writer ) output; 81 } 82 else if ( output instanceof OutputStream ) { 83 return new OutputStreamWriter ( (OutputStream ) output ); 84 } 85 return null; 86 } 87 88 89 93 public static void pipe(Reader reader, Writer writer) throws IOException { 94 pipe( reader, writer, new char[ BUFFER_SIZE / 2 ] ); 95 } 96 97 101 public static void pipe(Reader reader, Writer writer, char[] buffer) throws IOException { 102 try { 103 while (true) { 104 int size = reader.read( buffer ); 105 if ( size <= 0 ) { 106 return; 107 } 108 writer.write( buffer, 0, size ); 109 } 110 } 111 finally { 112 if ( CLOSE_READER ) { 113 try { 114 reader.close(); 115 } 116 catch (Exception e) { 117 } 118 } 119 } 120 } 121 122 126 public static void pipe(InputStream input, OutputStream output) throws IOException { 127 pipe( input, output, new byte[ BUFFER_SIZE ] ); 128 } 129 130 131 135 public static void pipe(InputStream input, OutputStream output, byte[] buffer) throws IOException { 136 try { 137 while (true) { 138 int size = input.read( buffer ); 139 if ( size <= 0 ) { 140 return; 141 } 142 output.write( buffer, 0, size ); 143 } 144 } 145 finally { 146 try { 147 input.close(); 148 } 149 catch (IOException e) { 150 } 151 } 152 } 153 } 154 | Popular Tags |