View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.util;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.io.InputStream;
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.io.OutputStreamWriter;
24  import java.io.Reader;
25  import java.io.Writer;
26  import java.io.InputStreamReader;
27  
28  /***
29   * Utility functions related to Streams.
30   *
31   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
32   * @version $Id: Streams.java 516448 2007-03-09 16:25:47Z ate $
33   */
34  public class Streams
35  {
36    static final int BLOCK_SIZE=4096;
37  
38    public static void drain(InputStream r,OutputStream w) throws IOException
39    {
40        byte[] bytes=new byte[BLOCK_SIZE];
41        try
42        {
43          int length=r.read(bytes);
44          while(length!=-1)
45          {
46              if(length!=0)
47                  {
48                      w.write(bytes,0,length);
49                  }
50              length=r.read(bytes);
51          }
52      }
53      finally
54      {
55        bytes=null;
56      }
57  
58    }
59  
60    public static void drain(Reader r,Writer w) throws IOException
61    {
62      char[] bytes=new char[BLOCK_SIZE];
63      try
64      {
65          int length=r.read(bytes);
66          while(length!=-1)
67          {
68              if(length!=0)
69              {
70                  w.write(bytes,0,length);
71              }
72              length=r.read(bytes);
73          }
74      }
75      finally
76      {
77          bytes=null;
78      }
79  
80    }
81  
82    public static void drain(Reader r,OutputStream os) throws IOException
83    {
84          Writer w=new OutputStreamWriter(os);
85          drain(r,w);
86          w.flush();
87    }
88  
89    public static void drain(InputStream is, Writer w) throws IOException
90    {
91        Reader r = new InputStreamReader(is);
92        drain(r,w);
93        w.flush();
94    }
95  
96    public static byte[] drain(InputStream r) throws IOException
97    {
98          ByteArrayOutputStream bytes=new ByteArrayOutputStream();
99          drain(r,bytes);
100         return bytes.toByteArray();
101   }
102 
103   public static String getAsString(InputStream is)
104   {
105       int c=0;
106       char lineBuffer[]=new char[128], buf[]=lineBuffer;
107       int room= buf.length, offset=0;
108       try
109       {
110           loop: while (true)
111           {
112             // read chars into a buffer which grows as needed
113                 switch (c = is.read() )
114                 {
115                     case -1: break loop;
116 
117                     default: if (--room < 0)
118                              {
119                                  buf = new char[offset + 128];
120                                  room = buf.length - offset - 1;
121                                  System.arraycopy(lineBuffer, 0,
122                                           buf, 0, offset);
123                                  lineBuffer = buf;
124                              }
125                              buf[offset++] = (char) c;
126                              break;
127                 }
128           }
129       }
130       catch(IOException ioe)
131       {
132           ioe.printStackTrace();
133       }
134       if ((c == -1) && (offset == 0))
135       {
136           return null;
137       }
138       return String.copyValueOf(buf, 0, offset);
139   }
140 
141 
142 
143 }