KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > sql > conv > StreamUtils


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.sql.conv;
13
14 import java.io.Reader JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17
18 /**
19  * Utility methods for converting Strings to and from byte and character
20  * streams.
21  * @keep-all
22  */

23 public class StreamUtils {
24
25     private static final int BUF_LEN = 4096;
26
27     /**
28      * Buffer to hold a chunk of character data read from a stream.
29      */

30     public static class CharBuf {
31         public char[] cbuf = new char[BUF_LEN];
32         public CharBuf next;
33     }
34
35     /**
36      * Buffer to hold a chunk of byte data read from the stream.
37      */

38     public static class ByteBuf {
39         public byte[] bbuf = new byte[BUF_LEN];
40         public ByteBuf next;
41     }
42
43     /**
44      * Read all of the characters from in into a String and close in.
45      */

46     public static String JavaDoc readAll(Reader JavaDoc in) throws IOException JavaDoc {
47         CharBuf root = new CharBuf();
48         int sz = in.read(root.cbuf);
49         if (sz < 0) {
50             in.close();
51             return "";
52         }
53         if (sz < BUF_LEN) {
54             in.close();
55             return new String JavaDoc(root.cbuf, 0, sz);
56         }
57         int bufCount = 0;
58         for (CharBuf buf = root;;) {
59             buf = buf.next = new CharBuf();
60             ++bufCount;
61             sz = in.read(buf.cbuf);
62             if (sz < 0) {
63                 sz = 0;
64                 break;
65             }
66             if (sz < BUF_LEN) break;
67         }
68         in.close();
69         int tot = bufCount * BUF_LEN + sz;
70         char[] a = new char[tot];
71         int pos = 0;
72         for (CharBuf buf = root; ; buf = buf.next) {
73             if (buf.next == null) {
74                 if (sz > 0) System.arraycopy(buf.cbuf, 0, a, pos, sz);
75                 break;
76             } else {
77                 System.arraycopy(buf.cbuf, 0, a, pos, BUF_LEN);
78                 pos += BUF_LEN;
79             }
80         }
81         return new String JavaDoc(a);
82     }
83
84     /**
85      * Read all of the bytes from in into a String and close in.
86      */

87     public static String JavaDoc readAll(InputStream JavaDoc in, String JavaDoc encoding) throws IOException JavaDoc {
88         ByteBuf root = new ByteBuf();
89         int sz = in.read(root.bbuf);
90         if (sz < 0) {
91             in.close();
92             return "";
93         }
94         if (sz < BUF_LEN) {
95             in.close();
96             return new String JavaDoc(root.bbuf, 0, sz, encoding);
97         }
98         int bufCount = 0;
99         for (ByteBuf buf = root;;) {
100             buf = buf.next = new ByteBuf();
101             ++bufCount;
102             sz = in.read(buf.bbuf);
103             if (sz < 0) {
104                 sz = 0;
105                 break;
106             }
107             if (sz < BUF_LEN) break;
108         }
109         in.close();
110         int tot = bufCount * BUF_LEN + sz;
111         byte[] a = new byte[tot];
112         int pos = 0;
113         for (ByteBuf buf = root; ; buf = buf.next) {
114             if (buf.next == null) {
115                 if (sz > 0) System.arraycopy(buf.bbuf, 0, a, pos, sz);
116                 break;
117             } else {
118                 System.arraycopy(buf.bbuf, 0, a, pos, BUF_LEN);
119                 pos += BUF_LEN;
120             }
121         }
122         return new String JavaDoc(a, encoding);
123     }
124
125     /**
126      * Read all of the bytes from in into a byte[] and close in.
127      */

128     public static byte[] readAll(InputStream JavaDoc in) throws IOException JavaDoc {
129         ByteBuf root = new ByteBuf();
130         int sz = in.read(root.bbuf);
131         if (sz < 0) {
132             in.close();
133             return new byte[0];
134         }
135         if (sz < BUF_LEN) {
136             in.close();
137             byte[] a = new byte[sz];
138             System.arraycopy(root.bbuf, 0, a, 0, sz);
139             return a;
140         }
141         int bufCount = 0;
142         for (ByteBuf buf = root;;) {
143             buf = buf.next = new ByteBuf();
144             ++bufCount;
145             sz = in.read(buf.bbuf);
146             if (sz < 0) {
147                 sz = 0;
148                 break;
149             }
150             if (sz < BUF_LEN) break;
151         }
152         in.close();
153         int tot = bufCount * BUF_LEN + sz;
154         byte[] a = new byte[tot];
155         int pos = 0;
156         for (ByteBuf buf = root; ; buf = buf.next) {
157             if (buf.next == null) {
158                 if (sz > 0) System.arraycopy(buf.bbuf, 0, a, pos, sz);
159                 break;
160             } else {
161                 System.arraycopy(buf.bbuf, 0, a, pos, BUF_LEN);
162                 pos += BUF_LEN;
163             }
164         }
165         return a;
166     }
167
168 }
169
170  
171
Popular Tags