001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.util;
007:
008: import java.io.BufferedReader;
009: import java.io.ByteArrayInputStream;
010: import java.io.ByteArrayOutputStream;
011: import java.io.EOFException;
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.io.InputStreamReader;
015: import java.io.OutputStream;
016: import java.io.Reader;
017: import java.io.StringReader;
018: import java.io.StringWriter;
019: import java.io.UnsupportedEncodingException;
020: import java.io.Writer;
021: import java.sql.SQLException;
022:
023: import org.h2.constant.SysProperties;
024: import org.h2.engine.Constants;
025: import org.h2.message.Message;
026:
027: /**
028: * This utility class contains input/output functions.
029: */
030: public class IOUtils {
031:
032: private static final int BUFFER_BLOCK_SIZE = 4 * 1024;
033:
034: public static void closeSilently(OutputStream out) {
035: if (out != null) {
036: try {
037: trace("closeSilently", null, out);
038: out.close();
039: } catch (IOException e) {
040: // ignore
041: }
042: }
043: }
044:
045: public static void skipFully(InputStream in, long skip)
046: throws IOException {
047: while (skip > 0) {
048: long skipped = in.skip(skip);
049: if (skipped <= 0) {
050: throw new EOFException();
051: }
052: skip -= skipped;
053: }
054: }
055:
056: public static void skipFully(Reader reader, long skip)
057: throws IOException {
058: while (skip > 0) {
059: long skipped = reader.skip(skip);
060: if (skipped <= 0) {
061: throw new EOFException();
062: }
063: skip -= skipped;
064: }
065: }
066:
067: public static long copyAndClose(InputStream in, OutputStream out)
068: throws IOException {
069: try {
070: long len = copyAndCloseInput(in, out);
071: out.close();
072: return len;
073: } finally {
074: closeSilently(out);
075: }
076: }
077:
078: public static long copyAndCloseInput(InputStream in,
079: OutputStream out) throws IOException {
080: try {
081: return copy(in, out);
082: } finally {
083: closeSilently(in);
084: }
085: }
086:
087: public static long copy(InputStream in, OutputStream out)
088: throws IOException {
089: long written = 0;
090: byte[] buffer = new byte[4 * 1024];
091: while (true) {
092: int len = in.read(buffer);
093: if (len < 0) {
094: break;
095: }
096: out.write(buffer, 0, len);
097: written += len;
098: }
099: return written;
100: }
101:
102: public static long copyAndCloseInput(Reader in, Writer out)
103: throws IOException {
104: long written = 0;
105: try {
106: char[] buffer = new char[4 * 1024];
107: while (true) {
108: int len = in.read(buffer);
109: if (len < 0) {
110: break;
111: }
112: out.write(buffer, 0, len);
113: written += len;
114: }
115: } finally {
116: in.close();
117: }
118: return written;
119: }
120:
121: public static void closeSilently(InputStream in) {
122: if (in != null) {
123: try {
124: trace("closeSilently", null, in);
125: in.close();
126: } catch (IOException e) {
127: // ignore
128: }
129: }
130: }
131:
132: public static void closeSilently(Reader reader) {
133: if (reader != null) {
134: try {
135: reader.close();
136: } catch (IOException e) {
137: // ignore
138: }
139: }
140: }
141:
142: public static void closeSilently(Writer writer) {
143: if (writer != null) {
144: try {
145: writer.flush();
146: writer.close();
147: } catch (IOException e) {
148: // ignore
149: }
150: }
151: }
152:
153: public static byte[] readBytesAndClose(InputStream in, int length)
154: throws IOException {
155: try {
156: if (length <= 0) {
157: length = Integer.MAX_VALUE;
158: }
159: int block = Math.min(BUFFER_BLOCK_SIZE, length);
160: ByteArrayOutputStream out = new ByteArrayOutputStream(block);
161: byte[] buff = new byte[block];
162: while (length > 0) {
163: int len = Math.min(block, length);
164: len = in.read(buff, 0, len);
165: if (len < 0) {
166: break;
167: }
168: out.write(buff, 0, len);
169: length -= len;
170: }
171: return out.toByteArray();
172: } finally {
173: in.close();
174: }
175: }
176:
177: public static String readStringAndClose(Reader in, int length)
178: throws IOException {
179: try {
180: if (length <= 0) {
181: length = Integer.MAX_VALUE;
182: }
183: int block = Math.min(BUFFER_BLOCK_SIZE, length);
184: StringWriter out = new StringWriter(
185: length == Integer.MAX_VALUE ? block : length);
186: char[] buff = new char[block];
187: while (length > 0) {
188: int len = Math.min(block, length);
189: len = in.read(buff, 0, len);
190: if (len < 0) {
191: break;
192: }
193: out.write(buff, 0, len);
194: length -= len;
195: }
196: return out.toString();
197: } finally {
198: in.close();
199: }
200: }
201:
202: /**
203: * Read the given number of bytes to the buffer.
204: *
205: * @param in the input stream
206: * @param buffer the output buffer
207: * @param off the offset in the buffer
208: * @param max the number of bytes to read at most
209: * @return the number of bytes read
210: */
211: public static int readFully(InputStream in, byte[] buffer, int off,
212: int max) throws IOException {
213: int len = Math.min(max, buffer.length);
214: int result = 0;
215: while (len > 0) {
216: int l = in.read(buffer, off, len);
217: if (l < 0) {
218: break;
219: }
220: result += l;
221: off += l;
222: len -= l;
223: }
224: return result;
225: }
226:
227: public static int readFully(Reader in, char[] buffer, int max)
228: throws IOException {
229: int off = 0, len = Math.min(max, buffer.length);
230: if (len == 0) {
231: return 0;
232: }
233: while (true) {
234: int l = len - off;
235: if (l <= 0) {
236: break;
237: }
238: l = in.read(buffer, off, l);
239: if (l < 0) {
240: break;
241: }
242: off += l;
243: }
244: return off <= 0 ? -1 : off;
245: }
246:
247: public static Reader getReader(InputStream in) throws SQLException {
248: try {
249: // InputStreamReader may read some more bytes
250: return in == null ? null : new BufferedReader(
251: new InputStreamReader(in, Constants.UTF8));
252: } catch (UnsupportedEncodingException e) {
253: throw Message.convert(e);
254: }
255: }
256:
257: public static InputStream getInputStream(String s)
258: throws SQLException {
259: if (s == null) {
260: return null;
261: }
262: return new ByteArrayInputStream(StringUtils.utf8Encode(s));
263: }
264:
265: public static InputStream getInputStream(Reader x)
266: throws SQLException {
267: return x == null ? null : new ReaderInputStream(x);
268: }
269:
270: public static Reader getReader(String s) {
271: return s == null ? null : new StringReader(s);
272: }
273:
274: public static Reader getAsciiReader(InputStream x)
275: throws SQLException {
276: try {
277: return x == null ? null : new InputStreamReader(x,
278: "US-ASCII");
279: } catch (UnsupportedEncodingException e) {
280: throw Message.convert(e);
281: }
282: }
283:
284: private static void trace(String method, String fileName, Object o) {
285: if (SysProperties.TRACE_IO) {
286: System.out.println("IOUtils." + method + " " + fileName
287: + " " + o);
288: }
289: }
290:
291: }
|