001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016:
017: package org.columba.core.io;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024:
025: /**
026: * Contains utility methods for handling streams.
027: */
028: public class StreamUtils {
029: private static final int BUFFERSIZE = 8000;
030:
031: /**
032: * Copies length bytes from an InputStream to an OutputStream.
033: *
034: * @param in
035: * InputStream from wihch the bytes are to copied.
036: * @param out
037: * OutputStream in which the bytes are copied.
038: * @param length
039: * The number of bytes to copy
040: * @return Number of bytes which are copied.
041: * @throws IOException
042: * If the streams are unavailable.
043: */
044: public static int streamCopy(InputStream in, OutputStream out,
045: int length) throws IOException {
046: byte[] buffer = new byte[BUFFERSIZE];
047: int read;
048: int copied = 0;
049:
050: while ((read = in.read(buffer, 0, Math.min(BUFFERSIZE, length
051: - copied))) > 0) {
052: out.write(buffer, 0, read);
053: copied += read;
054: }
055:
056: return copied;
057: }
058:
059: /**
060: * Copies all bytes from an InputStream to an OutputStream. The buffer size
061: * is set to 8000 bytes.
062: *
063: * @param _isInput
064: * InputStream from wihch the bytes are to copied.
065: * @param _osOutput
066: * OutputStream in which the bytes are copied.
067: * @return Number of bytes which are copied.
068: * @throws IOException
069: * If the Streams are unavailable.
070: */
071: public static long streamCopy(InputStream in, OutputStream out)
072: throws IOException {
073: byte[] buffer = new byte[BUFFERSIZE];
074: int read;
075: long copied = 0;
076:
077: while ((read = in.read(buffer)) > 0) {
078: out.write(buffer, 0, read);
079: copied += read;
080: }
081:
082: return copied;
083: }
084:
085: /**
086: * Reads a InputStream of chars into a StringBuffer.
087: *
088: * @param in
089: * the InputStream to read from
090: * @return the interpreted InputStream
091: * @throws IOException
092: */
093: public static StringBuffer readCharacterStream(InputStream in)
094: throws IOException {
095: StringBuffer result = new StringBuffer(in.available());
096: int read = in.read();
097:
098: while (read > 0) {
099: result.append((char) read);
100: read = in.read();
101: }
102:
103: in.close();
104:
105: return result;
106: }
107:
108: public static byte[] readInByteArray(InputStream in)
109: throws IOException {
110: byte[] result = new byte[in.available()];
111:
112: in.read(result);
113:
114: in.close();
115:
116: return result;
117: }
118:
119: /**
120: * Copies all bytes from the given InputStream into an internal
121: * ByteArrayOutputStream and returnes a new InputStream with all bytes from
122: * the ByteArrayOutputStream. The data are real copied so this method
123: * "clones" the given Inputstream and returns a new InputStream with same
124: * data.
125: *
126: * @param from
127: * InputStream from which all data are to copy
128: * @return a new InputStream with all data from the given InputStream
129: * @throws IOException
130: */
131: public static InputStream streamClone(InputStream from)
132: throws IOException {
133: ByteArrayOutputStream out = new ByteArrayOutputStream();
134: streamCopy(from, out);
135:
136: return new ByteArrayInputStream(out.toByteArray());
137: }
138: }
|