001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of 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,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.helpers;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.io.Reader;
025: import java.io.Writer;
026:
027: public final class IOUtils {
028:
029: private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
030:
031: private IOUtils() {
032:
033: }
034:
035: public static int copy(final InputStream input,
036: final OutputStream output) throws IOException {
037: return copy(input, output, DEFAULT_BUFFER_SIZE);
038: }
039:
040: public static int copy(final InputStream input,
041: final OutputStream output, int bufferSize)
042: throws IOException {
043: int avail = input.available();
044: if (avail > 262144) {
045: avail = 262144;
046: }
047: if (avail > bufferSize) {
048: bufferSize = avail;
049: }
050: final byte[] buffer = new byte[bufferSize];
051: int n = 0;
052: n = input.read(buffer);
053: int total = 0;
054: while (-1 != n) {
055: output.write(buffer, 0, n);
056: total += n;
057: n = input.read(buffer);
058: }
059: return total;
060: }
061:
062: public static void copy(final Reader input, final Writer output,
063: final int bufferSize) throws IOException {
064: final char[] buffer = new char[bufferSize];
065: int n = 0;
066: n = input.read(buffer);
067: while (-1 != n) {
068: output.write(buffer, 0, n);
069: n = input.read(buffer);
070: }
071: }
072:
073: public static String toString(final InputStream input)
074: throws IOException {
075:
076: StringBuffer buf = new StringBuffer();
077: final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
078: int n = 0;
079: n = input.read(buffer);
080: while (-1 != n) {
081: buf.append(new String(buffer, 0, n));
082: n = input.read(buffer);
083: }
084: return buf.toString();
085: }
086:
087: public static String toString(final Reader input)
088: throws IOException {
089:
090: StringBuffer buf = new StringBuffer();
091: final char[] buffer = new char[DEFAULT_BUFFER_SIZE];
092: int n = 0;
093: n = input.read(buffer);
094: while (-1 != n) {
095: buf.append(new String(buffer, 0, n));
096: n = input.read(buffer);
097: }
098: return buf.toString();
099: }
100:
101: public static String readStringFromStream(InputStream in)
102: throws IOException {
103:
104: StringBuilder sb = new StringBuilder(1024);
105:
106: for (int i = in.read(); i != -1; i = in.read()) {
107: sb.append((char) i);
108: }
109:
110: in.close();
111:
112: return sb.toString();
113: }
114:
115: public static byte[] readBytesFromStream(InputStream in)
116: throws IOException {
117:
118: ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
119:
120: for (int i = in.read(); i != -1; i = in.read()) {
121: bos.write(i);
122: }
123:
124: in.close();
125:
126: return bos.toByteArray();
127: }
128: }
|