001: /*
002: * $Id: Streams.java 460514 2006-05-04 18:43:27Z jonl $ $Revision: 460514 $
003: * $Date: 2006-05-04 20:43:27 +0200 (Thu, 04 May 2006) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.util.io;
019:
020: import java.io.BufferedReader;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.InputStreamReader;
024: import java.io.OutputStream;
025: import java.io.Reader;
026:
027: /**
028: * Utilities methods for working with input and output streams.
029: *
030: * @author Jonathan Locke
031: */
032: public final class Streams {
033: /**
034: * Writes the input stream to the output stream. Input is done without a
035: * Reader object, meaning that the input is copied in its raw form.
036: *
037: * @param in
038: * The input stream
039: * @param out
040: * The output stream
041: * @return Number of bytes copied from one stream to the other
042: * @throws IOException
043: */
044: public static int copy(final InputStream in, final OutputStream out)
045: throws IOException {
046: final byte[] buffer = new byte[4096];
047: int bytesCopied = 0;
048: while (true) {
049: int byteCount = in.read(buffer, 0, buffer.length);
050: if (byteCount <= 0) {
051: break;
052: }
053: out.write(buffer, 0, byteCount);
054: bytesCopied += byteCount;
055: }
056: return bytesCopied;
057: }
058:
059: /**
060: * Reads a stream as a string.
061: *
062: * @param in
063: * The input stream
064: * @return The string
065: * @throws IOException
066: */
067: public static String readString(final InputStream in)
068: throws IOException {
069: return readString(new BufferedReader(new InputStreamReader(in)));
070: }
071:
072: /**
073: * Reads a string using a character encoding.
074: *
075: * @param in
076: * The input
077: * @param encoding
078: * The character encoding of the input data
079: * @return The string
080: * @throws IOException
081: */
082: public static String readString(final InputStream in,
083: final CharSequence encoding) throws IOException {
084: return readString(new BufferedReader(new InputStreamReader(in,
085: encoding.toString())));
086: }
087:
088: /**
089: * Reads all input from a reader into a string.
090: *
091: * @param in
092: * The input
093: * @return The string
094: * @throws IOException
095: */
096: public static String readString(final Reader in) throws IOException {
097: final StringBuffer buffer = new StringBuffer(2048);
098: int value;
099:
100: while ((value = in.read()) != -1) {
101: buffer.append((char) value);
102: }
103:
104: return buffer.toString();
105: }
106:
107: /**
108: * Private to prevent instantiation.
109: */
110: private Streams() {
111: }
112: }
|