KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > util > IOUtils


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.util;
6
7 import java.io.ByteArrayInputStream JavaDoc;
8 import java.io.ByteArrayOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.io.InputStreamReader JavaDoc;
12 import java.io.OutputStream JavaDoc;
13 import java.io.Reader JavaDoc;
14 import java.io.StringReader JavaDoc;
15 import java.io.StringWriter JavaDoc;
16 import java.io.UnsupportedEncodingException JavaDoc;
17 import java.io.Writer JavaDoc;
18 import java.sql.SQLException JavaDoc;
19
20 import org.h2.engine.Constants;
21 import org.h2.message.Message;
22
23 public class IOUtils {
24     
25     private static final int BUFFER_BLOCK_SIZE = 4 * 1024;
26     
27     public static void closeSilently(OutputStream JavaDoc out) {
28         if(out != null) {
29             try {
30                 out.close();
31             } catch(IOException JavaDoc e) {
32                 // ignore
33
}
34         }
35     }
36     
37     public static void skipFully(InputStream JavaDoc in, long skip) throws IOException JavaDoc {
38         while(skip > 0) {
39             skip -= in.skip(skip);
40         }
41     }
42
43     public static void skipFully(Reader JavaDoc reader, long skip) throws IOException JavaDoc {
44         while(skip > 0) {
45             skip -= reader.skip(skip);
46         }
47     }
48     
49     public static long copyAndClose(InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc {
50         try {
51             return copyAndCloseInput(in, out);
52         } finally {
53             out.close();
54         }
55     }
56
57     public static long copyAndCloseInput(InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc {
58         int written = 0;
59         try {
60             byte[] buffer = new byte[4 * 1024];
61             while(true) {
62                 int len = in.read(buffer);
63                 if(len < 0) {
64                     break;
65                 }
66                 out.write(buffer, 0, len);
67                 written += len;
68             }
69         } finally {
70             in.close();
71         }
72         return written;
73     }
74     
75     public static long copyAndCloseInput(Reader JavaDoc in, Writer JavaDoc out) throws IOException JavaDoc {
76         long written = 0;
77         try {
78             char[] buffer = new char[4 * 1024];
79             while(true) {
80                 int len = in.read(buffer);
81                 if(len < 0) {
82                     break;
83                 }
84                 out.write(buffer, 0, len);
85                 written += len;
86             }
87         } finally {
88             in.close();
89         }
90         return written;
91     }
92
93     public static void closeSilently(InputStream JavaDoc in) {
94         if(in != null) {
95             try {
96                 in.close();
97             } catch(IOException JavaDoc e) {
98                 // ignore
99
}
100         }
101     }
102     
103     public static void closeSilently(Reader JavaDoc reader) {
104         if(reader != null) {
105             try {
106                 reader.close();
107             } catch(IOException JavaDoc e) {
108                 // ignore
109
}
110         }
111     }
112
113     public static void closeSilently(Writer JavaDoc writer) {
114         if(writer != null) {
115             try {
116                 writer.flush();
117                 writer.close();
118             } catch(IOException JavaDoc e) {
119                 // ignore
120
}
121         }
122     }
123
124     public static byte[] readBytesAndClose(InputStream JavaDoc in, int length) throws IOException JavaDoc {
125         try {
126             if(length <= 0) {
127                 length = Integer.MAX_VALUE;
128             }
129             int block = Math.min(BUFFER_BLOCK_SIZE, length);
130             ByteArrayOutputStream JavaDoc out=new ByteArrayOutputStream JavaDoc(block);
131             byte[] buff=new byte[block];
132             while(length > 0) {
133                 int len = Math.min(block, length);
134                 len = in.read(buff, 0, len);
135                 if(len < 0) {
136                     break;
137                 }
138                 out.write(buff, 0, len);
139                 length -= len;
140             }
141             return out.toByteArray();
142         } finally {
143             in.close();
144         }
145     }
146     
147     public static String JavaDoc readStringAndClose(Reader JavaDoc in, int length) throws IOException JavaDoc {
148         if(length <= 0) {
149             length = Integer.MAX_VALUE;
150         }
151         int block = Math.min(BUFFER_BLOCK_SIZE, length);
152         StringWriter JavaDoc out=new StringWriter JavaDoc(length == Integer.MAX_VALUE ? block : length);
153         char[] buff=new char[block];
154         while(length > 0) {
155             int len = Math.min(block, length);
156             len = in.read(buff, 0, len);
157             if(len < 0) {
158                 break;
159             }
160             out.write(buff, 0, len);
161             length -= len;
162         }
163         in.close();
164         return out.toString();
165     }
166
167     public static int readFully(InputStream JavaDoc in, byte[] buffer, int max) throws IOException JavaDoc {
168         int off = 0, len = Math.min(max, buffer.length);
169         if(len == 0) {
170             return 0;
171         }
172         while (true) {
173             int l = len - off;
174             if (l <= 0) {
175                 break;
176             }
177             l = in.read(buffer, off, l);
178             if (l < 0) {
179                 break;
180             }
181             off += l;
182         }
183         return off < 0 ? -1 : off;
184     }
185
186     public static int readFully(Reader JavaDoc in, char[] buffer, int max) throws IOException JavaDoc {
187         int off = 0, len = Math.min(max, buffer.length);
188         if(len == 0) {
189             return 0;
190         }
191         while (true) {
192             int l = len - off;
193             if (l <= 0) {
194                 break;
195             }
196             l = in.read(buffer, off, l);
197             if (l < 0) {
198                 break;
199             }
200             off += l;
201         }
202         return off < 0 ? -1 : off;
203     }
204     
205     public static Reader JavaDoc getReader(InputStream JavaDoc in) throws SQLException JavaDoc {
206         try {
207             // InputStreamReader may read some more bytes
208
return in == null ? null : new InputStreamReader JavaDoc(in, Constants.UTF8);
209         } catch (UnsupportedEncodingException JavaDoc e) {
210             throw Message.convert(e);
211         }
212     }
213
214     public static InputStream JavaDoc getInputStream(String JavaDoc s) throws SQLException JavaDoc {
215         if(s == null) {
216             return null;
217         }
218         return new ByteArrayInputStream JavaDoc(StringUtils.utf8Encode(s));
219     }
220
221     public static InputStream JavaDoc getInputStream(Reader JavaDoc x) throws SQLException JavaDoc {
222         return x == null ? null : new ReaderInputStream(x);
223     }
224
225     public static Reader JavaDoc getReader(String JavaDoc s) {
226         return s == null ? null : new StringReader JavaDoc(s);
227     }
228
229     public static Reader JavaDoc getAsciiReader(InputStream JavaDoc x) throws SQLException JavaDoc {
230         try {
231             return x == null ? null : new InputStreamReader JavaDoc(x, "US-ASCII");
232         } catch (UnsupportedEncodingException JavaDoc e) {
233             throw Message.convert(e);
234         }
235     }
236
237 }
238
Popular Tags