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.portals.applications.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: /**
071: * @deprecated encoding?
072: * @param r character reader
073: * @param os byte stream
074: * @throws IOException
075: */
076: public static void drain(Reader r, OutputStream os)
077: throws IOException {
078: Writer w = new OutputStreamWriter(os);
079: drain(r, w);
080: w.flush();
081: }
082:
083: /**
084: * @deprecated how can it know the encoding?
085: * @param is input stream (encoding?)
086: * @param w writer
087: * @throws IOException
088: */
089: public static void drain(InputStream is, Writer w)
090: throws IOException {
091: Reader r = new InputStreamReader(is);
092: drain(r, w);
093: w.flush();
094: }
095:
096: public static byte[] drain(InputStream r) throws IOException {
097: ByteArrayOutputStream bytes = new ByteArrayOutputStream();
098: drain(r, bytes);
099: return bytes.toByteArray();
100: }
101:
102: /**
103: * @deprecated encoding?
104: * @param is input stream
105: * @return
106: */
107: public static String getAsString(InputStream is) {
108: int c = 0;
109: char lineBuffer[] = new char[128], buf[] = lineBuffer;
110: int room = buf.length, offset = 0;
111: try {
112: loop: while (true) {
113: // read chars into a buffer which grows as needed
114: switch (c = is.read()) {
115: case -1:
116: break loop;
117:
118: default:
119: if (--room < 0) {
120: buf = new char[offset + 128];
121: room = buf.length - offset - 1;
122: System.arraycopy(lineBuffer, 0, buf, 0, offset);
123: lineBuffer = buf;
124: }
125: buf[offset++] = (char) c;
126: break;
127: }
128: }
129: } catch (IOException ioe) {
130: ioe.printStackTrace();
131: }
132: if ((c == -1) && (offset == 0)) {
133: return null;
134: }
135: return String.copyValueOf(buf, 0, offset);
136: }
137:
138: }
|