KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > lf5 > util > StreamUtils


1 /*
2  * Copyright 1999-2005 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.log4j.lf5.util;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 /**
25  * Provides utility methods for input and output streams.
26  *
27  * @author Richard Wan
28  */

29
30 // Contributed by ThoughtWorks Inc.
31

32 public abstract class StreamUtils {
33   //--------------------------------------------------------------------------
34
// Constants:
35
//--------------------------------------------------------------------------
36

37   /**
38    * Default value is 2048.
39    */

40   public static final int DEFAULT_BUFFER_SIZE = 2048;
41
42   //--------------------------------------------------------------------------
43
// Protected Variables:
44
//--------------------------------------------------------------------------
45

46   //--------------------------------------------------------------------------
47
// Private Variables:
48
//--------------------------------------------------------------------------
49

50   //--------------------------------------------------------------------------
51
// Constructors:
52
//--------------------------------------------------------------------------
53

54   //--------------------------------------------------------------------------
55
// Public Methods:
56
//--------------------------------------------------------------------------
57

58   /**
59    * Copies information from the input stream to the output stream using
60    * a default buffer size of 2048 bytes.
61    * @throws java.io.IOException
62    */

63   public static void copy(InputStream JavaDoc input, OutputStream JavaDoc output)
64       throws IOException JavaDoc {
65     copy(input, output, DEFAULT_BUFFER_SIZE);
66   }
67
68   /**
69    * Copies information from the input stream to the output stream using
70    * the specified buffer size
71    * @throws java.io.IOException
72    */

73   public static void copy(InputStream JavaDoc input,
74       OutputStream JavaDoc output,
75       int bufferSize)
76       throws IOException JavaDoc {
77     byte[] buf = new byte[bufferSize];
78     int bytesRead = input.read(buf);
79     while (bytesRead != -1) {
80       output.write(buf, 0, bytesRead);
81       bytesRead = input.read(buf);
82     }
83     output.flush();
84   }
85
86   /**
87    * Copies information between specified streams and then closes
88    * both of the streams.
89    * @throws java.io.IOException
90    */

91   public static void copyThenClose(InputStream JavaDoc input, OutputStream JavaDoc output)
92       throws IOException JavaDoc {
93     copy(input, output);
94     input.close();
95     output.close();
96   }
97
98   /**
99    * @returns a byte[] containing the information contained in the
100    * specified InputStream.
101    * @throws java.io.IOException
102    */

103   public static byte[] getBytes(InputStream JavaDoc input)
104       throws IOException JavaDoc {
105     ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
106     copy(input, result);
107     result.close();
108     return result.toByteArray();
109   }
110
111   //--------------------------------------------------------------------------
112
// Protected Methods:
113
//--------------------------------------------------------------------------
114

115   //--------------------------------------------------------------------------
116
// Private Methods:
117
//--------------------------------------------------------------------------
118

119   //--------------------------------------------------------------------------
120
// Nested Top-Level Classes or Interfaces
121
//--------------------------------------------------------------------------
122

123 }
124
Popular Tags