KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > util > Streams


1 /* ====================================================================
2  * BBC Syndication Server 1.0
3  *
4  * Copyright (c) 2002-2003 The BBC. All rights reserved.
5  *
6  */

7
8 package org.apache.jetspeed.util;
9
10 import java.io.ByteArrayOutputStream JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStreamReader JavaDoc;
14 import java.io.OutputStream JavaDoc;
15 import java.io.OutputStreamWriter JavaDoc;
16 import java.io.Reader JavaDoc;
17 import java.io.Writer JavaDoc;
18
19 /**
20  * <P>Utility functions related to Streams</P>
21  *
22  * @author <a HREF="mailto:taylor@apache.org">David Sean Taylor</a>
23  * @version $Id: Streams.java,v 1.2 2003/04/23 01:44:52 taylor Exp $
24  */

25 public class Streams
26 {
27   static final int BLOCK_SIZE=4096;
28
29   public static void drain(InputStream JavaDoc r,OutputStream JavaDoc w) throws IOException JavaDoc
30   {
31       byte[] bytes=new byte[BLOCK_SIZE];
32       try
33       {
34         int length=r.read(bytes);
35         while(length!=-1)
36         {
37             if(length!=0)
38                 {
39                     w.write(bytes,0,length);
40                 }
41             length=r.read(bytes);
42         }
43     }
44     finally
45     {
46       bytes=null;
47     }
48
49   }
50
51   public static void drain(Reader JavaDoc r,Writer JavaDoc w) throws IOException JavaDoc
52   {
53     char[] bytes=new char[BLOCK_SIZE];
54     try
55     {
56         int length=r.read(bytes);
57         while(length!=-1)
58         {
59             if(length!=0)
60             {
61                 w.write(bytes,0,length);
62             }
63             length=r.read(bytes);
64         }
65     }
66     finally
67     {
68         bytes=null;
69     }
70
71   }
72
73   public static void drain(Reader JavaDoc r,OutputStream JavaDoc os) throws IOException JavaDoc
74   {
75         Writer JavaDoc w=new OutputStreamWriter JavaDoc(os);
76         drain(r,w);
77         w.flush();
78   }
79
80   public static byte[] drain(InputStream JavaDoc r) throws IOException JavaDoc
81   {
82         ByteArrayOutputStream JavaDoc bytes=new ByteArrayOutputStream JavaDoc();
83         drain(r,bytes);
84         return bytes.toByteArray();
85   }
86
87   public static void drain(InputStream JavaDoc is, Writer JavaDoc w) throws IOException JavaDoc
88   {
89       Reader JavaDoc r = new InputStreamReader JavaDoc(is);
90       drain(r,w);
91       w.flush();
92   }
93
94   public static String JavaDoc getAsString(InputStream JavaDoc is)
95   {
96       int c=0;
97       char lineBuffer[]=new char[128], buf[]=lineBuffer;
98       int room= buf.length, offset=0;
99       try
100       {
101           loop: while (true)
102           {
103             // read chars into a buffer which grows as needed
104
switch (c = is.read() )
105                 {
106                     case -1: break loop;
107
108                     default: if (--room < 0)
109                              {
110                                  buf = new char[offset + 128];
111                                  room = buf.length - offset - 1;
112                                  System.arraycopy(lineBuffer, 0,
113                                           buf, 0, offset);
114                                  lineBuffer = buf;
115                              }
116                              buf[offset++] = (char) c;
117                              break;
118                 }
119           }
120       }
121       catch(IOException JavaDoc ioe)
122       {
123           ioe.printStackTrace();
124       }
125       if ((c == -1) && (offset == 0))
126       {
127           return null;
128       }
129       return String.copyValueOf(buf, 0, offset);
130   }
131
132   public static String JavaDoc getAsString(Reader JavaDoc is)
133   {
134       int c=0;
135       char lineBuffer[]=new char[128], buf[]=lineBuffer;
136       int room= buf.length, offset=0;
137       try
138       {
139           loop: while (true)
140           {
141             // read chars into a buffer which grows as needed
142
switch (c = is.read() )
143                 {
144                     case -1: break loop;
145
146                     default: if (--room < 0)
147                              {
148                                  buf = new char[offset + 128];
149                                  room = buf.length - offset - 1;
150                                  System.arraycopy(lineBuffer, 0,
151                                           buf, 0, offset);
152                                  lineBuffer = buf;
153                              }
154                              buf[offset++] = (char) c;
155                              break;
156                 }
157           }
158       }
159       catch(IOException JavaDoc ioe)
160       {
161           ioe.printStackTrace();
162       }
163       if ((c == -1) && (offset == 0))
164       {
165           return null;
166       }
167       return String.copyValueOf(buf, 0, offset);
168   }
169
170
171
172 }
173
174
Popular Tags