001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.util;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.InputStream;
021: import java.io.IOException;
022: import java.io.OutputStream;
023: import java.io.OutputStreamWriter;
024: import java.io.Reader;
025: import java.io.Writer;
026: import java.io.InputStreamReader;
027:
028: /**
029: * Utility functions related to Streams.
030: *
031: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
032: * @version $Id: Streams.java 516448 2007-03-09 16:25:47Z ate $
033: */
034: public class Streams {
035: static final int BLOCK_SIZE = 4096;
036:
037: public static void drain(InputStream r, OutputStream w)
038: throws IOException {
039: byte[] bytes = new byte[BLOCK_SIZE];
040: try {
041: int length = r.read(bytes);
042: while (length != -1) {
043: if (length != 0) {
044: w.write(bytes, 0, length);
045: }
046: length = r.read(bytes);
047: }
048: } finally {
049: bytes = null;
050: }
051:
052: }
053:
054: public static void drain(Reader r, Writer w) throws IOException {
055: char[] bytes = new char[BLOCK_SIZE];
056: try {
057: int length = r.read(bytes);
058: while (length != -1) {
059: if (length != 0) {
060: w.write(bytes, 0, length);
061: }
062: length = r.read(bytes);
063: }
064: } finally {
065: bytes = null;
066: }
067:
068: }
069:
070: public static void drain(Reader r, OutputStream os)
071: throws IOException {
072: Writer w = new OutputStreamWriter(os);
073: drain(r, w);
074: w.flush();
075: }
076:
077: public static void drain(InputStream is, Writer w)
078: throws IOException {
079: Reader r = new InputStreamReader(is);
080: drain(r, w);
081: w.flush();
082: }
083:
084: public static byte[] drain(InputStream r) throws IOException {
085: ByteArrayOutputStream bytes = new ByteArrayOutputStream();
086: drain(r, bytes);
087: return bytes.toByteArray();
088: }
089:
090: public static String getAsString(InputStream is) {
091: int c = 0;
092: char lineBuffer[] = new char[128], buf[] = lineBuffer;
093: int room = buf.length, offset = 0;
094: try {
095: loop: while (true) {
096: // read chars into a buffer which grows as needed
097: switch (c = is.read()) {
098: case -1:
099: break loop;
100:
101: default:
102: if (--room < 0) {
103: buf = new char[offset + 128];
104: room = buf.length - offset - 1;
105: System.arraycopy(lineBuffer, 0, buf, 0, offset);
106: lineBuffer = buf;
107: }
108: buf[offset++] = (char) c;
109: break;
110: }
111: }
112: } catch (IOException ioe) {
113: ioe.printStackTrace();
114: }
115: if ((c == -1) && (offset == 0)) {
116: return null;
117: }
118: return String.copyValueOf(buf, 0, offset);
119: }
120:
121: }
|