File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\util\IOUtils.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.util;
8
9 import java.io.BufferedReader;
10 import java.io.BufferedWriter;
11 import java.io.ByteArrayInputStream;
12 import java.io.ByteArrayOutputStream;
13 import java.io.Closeable;
14 import java.io.EOFException;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.InputStreamReader;
18 import java.io.OutputStream;
19 import java.io.OutputStreamWriter;
20 import java.io.Reader;
21 import java.io.StringReader;
22 import java.io.StringWriter;
23 import java.io.Writer;
24 import org.h2.constant.SysProperties;
25 import org.h2.engine.Constants;
26 import org.h2.message.DbException;
27
28 /**
29 * This utility class contains input/output functions.
30 */
31 public class IOUtils {
32
33 private IOUtils() {
34 // utility class
35 }
36
37 /**
38 * Close an output stream without throwing an exception.
39 *
40 * @param out the output stream or null
41 */
42 public static void closeSilently(Closeable out) {
43 if (out != null) {
44 try {
45 trace("closeSilently", null, out);
46 out.close();
47 } catch (Exception e) {
48 // ignore
49 }
50 }
51 }
52
53 /**
54 * Skip a number of bytes in an input stream.
55 *
56 * @param in the input stream
57 * @param skip the number of bytes to skip
58 * @throws EOFException if the end of file has been reached before all bytes
59 * could be skipped
60 * @throws IOException if an IO exception occurred while skipping
61 */
62 public static void skipFully(InputStream in, long skip) throws IOException {
63 try {
64 while (skip > 0) {
65 long skipped = in.skip(skip);
66 if (skipped <= 0) {
67 throw new EOFException();
68 }
69 skip -= skipped;
70 }
71 } catch (Exception e) {
72 throw DbException.convertToIOException(e);
73 }
74 }
75
76 /**
77 * Skip a number of characters in a reader.
78 *
79 * @param reader the reader
80 * @param skip the number of characters to skip
81 * @throws EOFException if the end of file has been reached before all
82 * characters could be skipped
83 * @throws IOException if an IO exception occurred while skipping
84 */
85 public static void skipFully(Reader reader, long skip) throws IOException {
86 try {
87 while (skip > 0) {
88 long skipped = reader.skip(skip);
89 if (skipped <= 0) {
90 throw new EOFException();
91 }
92 skip -= skipped;
93 }
94 } catch (Exception e) {
95 throw DbException.convertToIOException(e);
96 }
97 }
98
99 /**
100 * Copy all data from the input stream to the output stream and close both
101 * streams. Exceptions while closing are ignored.
102 *
103 * @param in the input stream
104 * @param out the output stream
105 * @return the number of bytes copied
106 */
107 public static long copyAndClose(InputStream in, OutputStream out) throws IOException {
108 try {
109 long len = copyAndCloseInput(in, out);
110 out.close();
111 return len;
112 } catch (Exception e) {
113 throw DbException.convertToIOException(e);
114 } finally {
115 closeSilently(out);
116 }
117 }
118
119 /**
120 * Copy all data from the input stream to the output stream and close the
121 * input stream. Exceptions while closing are ignored.
122 *
123 * @param in the input stream
124 * @param out the output stream (null if writing is not required)
125 * @return the number of bytes copied
126 */
127 public static long copyAndCloseInput(InputStream in, OutputStream out) throws IOException {
128 try {
129 return copy(in, out);
130 } catch (Exception e) {
131 throw DbException.convertToIOException(e);
132 } finally {
133 closeSilently(in);
134 }
135 }
136
137 /**
138 * Copy all data from the input stream to the output stream. Both streams
139 * are kept open.
140 *
141 * @param in the input stream
142 * @param out the output stream (null if writing is not required)
143 * @return the number of bytes copied
144 */
145 public static long copy(InputStream in, OutputStream out) throws IOException {
146 return copy(in, out, Long.MAX_VALUE);
147 }
148
149 /**
150 * Copy all data from the input stream to the output stream. Both streams
151 * are kept open.
152 *
153 * @param in the input stream
154 * @param out the output stream (null if writing is not required)
155 * @param length the maximum number of bytes to copy
156 * @return the number of bytes copied
157 */
158 public static long copy(InputStream in, OutputStream out, long length) throws IOException {
159 try {
160 long copied = 0;
161 int len = (int) Math.min(length, Constants.IO_BUFFER_SIZE);
162 byte[] buffer = new byte[len];
163 while (length > 0) {
164 len = in.read(buffer, 0, len);
165 if (len < 0) {
166 break;
167 }
168 if (out != null) {
169 out.write(buffer, 0, len);
170 }
171 copied += len;
172 length -= len;
173 len = (int) Math.min(length, Constants.IO_BUFFER_SIZE);
174 }
175 return copied;
176 } catch (Exception e) {
177 throw DbException.convertToIOException(e);
178 }
179 }
180
181 /**
182 * Copy all data from the reader to the writer and close the reader.
183 * Exceptions while closing are ignored.
184 *
185 * @param in the reader
186 * @param out the writer (null if writing is not required)
187 * @param length the maximum number of bytes to copy
188 * @return the number of characters copied
189 */
190 public static long copyAndCloseInput(Reader in, Writer out, long length) throws IOException {
191 try {
192 long copied = 0;
193 int len = (int) Math.min(length, Constants.IO_BUFFER_SIZE);
194 char[] buffer = new char[len];
195 while (length > 0) {
196 len = in.read(buffer, 0, len);
197 if (len < 0) {
198 break;
199 }
200 if (out != null) {
201 out.write(buffer, 0, len);
202 }
203 length -= len;
204 len = (int) Math.min(length, Constants.IO_BUFFER_SIZE);
205 copied += len;
206 }
207 return copied;
208 } catch (Exception e) {
209 throw DbException.convertToIOException(e);
210 } finally {
211 in.close();
212 }
213 }
214
215 /**
216 * Close an input stream without throwing an exception.
217 *
218 * @param in the input stream or null
219 */
220 public static void closeSilently(InputStream in) {
221 if (in != null) {
222 try {
223 trace("closeSilently", null, in);
224 in.close();
225 } catch (Exception e) {
226 // ignore
227 }
228 }
229 }
230
231 /**
232 * Close a reader without throwing an exception.
233 *
234 * @param reader the reader or null
235 */
236 public static void closeSilently(Reader reader) {
237 if (reader != null) {
238 try {
239 reader.close();
240 } catch (Exception e) {
241 // ignore
242 }
243 }
244 }
245
246 /**
247 * Close a writer without throwing an exception.
248 *
249 * @param writer the writer or null
250 */
251 public static void closeSilently(Writer writer) {
252 if (writer != null) {
253 try {
254 writer.flush();
255 writer.close();
256 } catch (Exception e) {
257 // ignore
258 }
259 }
260 }
261
262 /**
263 * Read a number of bytes from an input stream and close the stream.
264 *
265 * @param in the input stream
266 * @param length the maximum number of bytes to read, or -1 to read until
267 * the end of file
268 * @return the bytes read
269 */
270 public static byte[] readBytesAndClose(InputStream in, int length) throws IOException {
271 try {
272 if (length <= 0) {
273 length = Integer.MAX_VALUE;
274 }
275 int block = Math.min(Constants.IO_BUFFER_SIZE, length);
276 ByteArrayOutputStream out = new ByteArrayOutputStream(block);
277 copy(in, out, length);
278 return out.toByteArray();
279 } catch (Exception e) {
280 throw DbException.convertToIOException(e);
281 } finally {
282 in.close();
283 }
284 }
285
286 /**
287 * Read a number of characters from a reader and close it.
288 *
289 * @param in the reader
290 * @param length the maximum number of characters to read, or -1 to read
291 * until the end of file
292 * @return the string read
293 */
294 public static String readStringAndClose(Reader in, int length) throws IOException {
295 try {
296 if (length <= 0) {
297 length = Integer.MAX_VALUE;
298 }
299 int block = Math.min(Constants.IO_BUFFER_SIZE, length);
300 StringWriter out = new StringWriter(block);
301 copyAndCloseInput(in, out, length);
302 return out.toString();
303 } finally {
304 in.close();
305 }
306 }
307
308 /**
309 * Try to read the given number of bytes to the buffer. This method reads
310 * until the maximum number of bytes have been read or until the end of
311 * file.
312 *
313 * @param in the input stream
314 * @param buffer the output buffer
315 * @param off the offset in the buffer
316 * @param max the number of bytes to read at most
317 * @return the number of bytes read, 0 meaning EOF
318 */
319 public static int readFully(InputStream in, byte[] buffer, int off, int max) throws IOException {
320 try {
321 int len = Math.min(max, buffer.length);
322 int result = 0;
323 while (len > 0) {
324 int l = in.read(buffer, off, len);
325 if (l < 0) {
326 break;
327 }
328 result += l;
329 off += l;
330 len -= l;
331 }
332 return result;
333 } catch (Exception e) {
334 throw DbException.convertToIOException(e);
335 }
336 }
337
338 /**
339 * Try to read the given number of characters to the buffer. This method
340 * reads until the maximum number of characters have been read or until the
341 * end of file.
342 *
343 * @param in the reader
344 * @param buffer the output buffer
345 * @param max the number of characters to read at most
346 * @return the number of characters read
347 */
348 public static int readFully(Reader in, char[] buffer, int max) throws IOException {
349 try {
350 int off = 0, len = Math.min(max, buffer.length);
351 if (len == 0) {
352 return 0;
353 }
354 while (true) {
355 int l = len - off;
356 if (l <= 0) {
357 break;
358 }
359 l = in.read(buffer, off, l);
360 if (l < 0) {
361 break;
362 }
363 off += l;
364 }
365 return off <= 0 ? -1 : off;
366 } catch (Exception e) {
367 throw DbException.convertToIOException(e);
368 }
369 }
370
371 /**
372 * Create a buffered reader to read from an input stream using the UTF-8
373 * format. If the input stream is null, this method returns null. The
374 * InputStreamReader that is used here is not exact, that means it may read
375 * some additional bytes when buffering.
376 *
377 * @param in the input stream or null
378 * @return the reader
379 */
380 public static Reader getBufferedReader(InputStream in) {
381 try {
382 //
383 return in == null ? null : new BufferedReader(new InputStreamReader(in, Constants.UTF8));
384 } catch (Exception e) {
385 // UnsupportedEncodingException
386 throw DbException.convert(e);
387 }
388 }
389
390 /**
391 * Create a reader to read from an input stream using the UTF-8 format. If
392 * the input stream is null, this method returns null. The InputStreamReader
393 * that is used here is not exact, that means it may read some additional
394 * bytes when buffering.
395 *
396 * @param in the input stream or null
397 * @return the reader
398 */
399 public static Reader getReader(InputStream in) {
400 try {
401 // InputStreamReader may read some more bytes
402 return in == null ? null : new BufferedReader(new InputStreamReader(in, Constants.UTF8));
403 } catch (Exception e) {
404 // UnsupportedEncodingException
405 throw DbException.convert(e);
406 }
407 }
408
409 /**
410 * Create a buffered writer to write to an output stream using the UTF-8
411 * format. If the output stream is null, this method returns null.
412 *
413 * @param out the output stream or null
414 * @return the writer
415 */
416 public static Writer getBufferedWriter(OutputStream out) {
417 try {
418 return out == null ? null : new BufferedWriter(new OutputStreamWriter(out, Constants.UTF8));
419 } catch (Exception e) {
420 // UnsupportedEncodingException
421 throw DbException.convert(e);
422 }
423 }
424
425 /**
426 * Create a reader to read from a string.
427 * If the string is null, this method returns null.
428 *
429 * @param s the string or null
430 * @return the reader
431 */
432 public static Reader getReaderFromString(String s) {
433 return s == null ? null : new StringReader(s);
434 }
435
436 /**
437 * Wrap an input stream in a reader. The bytes are converted to characters
438 * using the US-ASCII character set.
439 *
440 * @param in the input stream
441 * @return the reader
442 */
443 public static Reader getAsciiReader(InputStream in) {
444 try {
445 return in == null ? null : new InputStreamReader(in, "US-ASCII");
446 } catch (Exception e) {
447 // UnsupportedEncodingException
448 throw DbException.convert(e);
449 }
450 }
451
452 /**
453 * Trace input or output operations if enabled.
454 *
455 * @param method the method from where this method was called
456 * @param fileName the file name
457 * @param o the object to append to the message
458 */
459 public static void trace(String method, String fileName, Object o) {
460 if (SysProperties.TRACE_IO) {
461 System.out.println("IOUtils." + method + " " + fileName + " " + o);
462 }
463 }
464
465 /**
466 * Create an input stream to read from a string. The string is converted to
467 * a byte array using UTF-8 encoding.
468 * If the string is null, this method returns null.
469 *
470 * @param s the string
471 * @return the input stream
472 */
473 public static InputStream getInputStreamFromString(String s) {
474 if (s == null) {
475 return null;
476 }
477 return new ByteArrayInputStream(StringUtils.utf8Encode(s));
478 }
479
480 }
481