KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > io > PipeHelper


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.io;
18
19 import java.beans.BeanInfo JavaDoc;
20 import java.beans.Introspector JavaDoc;
21 import java.beans.IntrospectionException JavaDoc;
22 import java.beans.PropertyDescriptor JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.io.BufferedReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.OutputStreamWriter JavaDoc;
30 import java.io.StringReader JavaDoc;
31 import java.io.Reader JavaDoc;
32 import java.io.Writer JavaDoc;
33
34 import javax.servlet.jsp.tagext.Tag JavaDoc;
35 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
36
37 /** A collection of helper methods for pipelining data between tags.
38   *
39   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
40   * @version $Revision: 1.3 $
41   */

42 public class PipeHelper {
43     
44     /** Size of the buffer used when piping a stream */
45     protected static final int BUFFER_SIZE = 64 * 1024;
46     
47     /** Temporarily disable the closing of Readers */
48     protected static final boolean CLOSE_READER = false;
49
50     
51     /** Uses the standard pipelining guidelines to produce a Reader
52       * instance for the given object.
53       * The object can be either a Reader, an InputStream or a String.
54       *
55       * @return a Reader for the given input or null if the instance
56       * could not be converted into a Reader.
57       */

58     public static Reader JavaDoc getReader(Object JavaDoc input) {
59         if ( input instanceof Reader JavaDoc ) {
60             return (Reader JavaDoc) input;
61         }
62         else if ( input instanceof InputStream JavaDoc ) {
63             return new InputStreamReader JavaDoc( (InputStream JavaDoc) input );
64         }
65         else if ( input instanceof String JavaDoc ) {
66             return new StringReader JavaDoc( (String JavaDoc) input );
67         }
68         return null;
69     }
70     
71     /** Uses the standard pipelining guidelines to produce a Writer
72       * instance for the given object.
73       * The object can be either a Writer or an InputStream.
74       *
75       * @return a Writer for the given input or null if the instance
76       * could not be converted into a Writer.
77       */

78     public static Writer JavaDoc getWriter(Object JavaDoc output) {
79         if ( output instanceof Writer JavaDoc ) {
80             return (Writer JavaDoc) output;
81         }
82         else if ( output instanceof OutputStream JavaDoc ) {
83             return new OutputStreamWriter JavaDoc( (OutputStream JavaDoc) output );
84         }
85         return null;
86     }
87     
88     
89     /** Pipes all the input from the given reader to the given writer.
90       * The reader will be closed whether an error occurs or not.
91       * Though the writer is left open.
92       */

93     public static void pipe(Reader JavaDoc reader, Writer JavaDoc writer) throws IOException JavaDoc {
94         pipe( reader, writer, new char[ BUFFER_SIZE / 2 ] );
95     }
96     
97     /** Pipes all the input from the given reader to the given writer.
98       * The reader will be closed whether an error occurs or not.
99       * Though the writer is left open.
100       */

101     public static void pipe(Reader JavaDoc reader, Writer JavaDoc writer, char[] buffer) throws IOException JavaDoc {
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 JavaDoc e) {
117                 }
118             }
119         }
120     }
121     
122     /** Pipes all the input to the given output.
123       * The input stream will be closed whether an error occurs or not.
124       * Though the output stream is left open is left open.
125       */

126     public static void pipe(InputStream JavaDoc input, OutputStream JavaDoc output) throws IOException JavaDoc {
127         pipe( input, output, new byte[ BUFFER_SIZE ] );
128     }
129     
130     
131     /** Pipes all the input to the given output.
132       * The input stream will be closed whether an error occurs or not.
133       * Though the output stream is left open is left open.
134       */

135     public static void pipe(InputStream JavaDoc input, OutputStream JavaDoc output, byte[] buffer) throws IOException JavaDoc {
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 JavaDoc e) {
150             }
151         }
152     }
153 }
154
Popular Tags