001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdbc.sql.conv;
012:
013: import java.io.Reader;
014: import java.io.IOException;
015: import java.io.InputStream;
016:
017: /**
018: * Utility methods for converting Strings to and from byte and character
019: * streams.
020: * @keep-all
021: */
022: public class StreamUtils {
023:
024: private static final int BUF_LEN = 4096;
025:
026: /**
027: * Buffer to hold a chunk of character data read from a stream.
028: */
029: public static class CharBuf {
030: public char[] cbuf = new char[BUF_LEN];
031: public CharBuf next;
032: }
033:
034: /**
035: * Buffer to hold a chunk of byte data read from the stream.
036: */
037: public static class ByteBuf {
038: public byte[] bbuf = new byte[BUF_LEN];
039: public ByteBuf next;
040: }
041:
042: /**
043: * Read all of the characters from in into a String and close in.
044: */
045: public static String readAll(Reader in) throws IOException {
046: CharBuf root = new CharBuf();
047: int sz = in.read(root.cbuf);
048: if (sz < 0) {
049: in.close();
050: return "";
051: }
052: if (sz < BUF_LEN) {
053: in.close();
054: return new String(root.cbuf, 0, sz);
055: }
056: int bufCount = 0;
057: for (CharBuf buf = root;;) {
058: buf = buf.next = new CharBuf();
059: ++bufCount;
060: sz = in.read(buf.cbuf);
061: if (sz < 0) {
062: sz = 0;
063: break;
064: }
065: if (sz < BUF_LEN)
066: break;
067: }
068: in.close();
069: int tot = bufCount * BUF_LEN + sz;
070: char[] a = new char[tot];
071: int pos = 0;
072: for (CharBuf buf = root;; buf = buf.next) {
073: if (buf.next == null) {
074: if (sz > 0)
075: System.arraycopy(buf.cbuf, 0, a, pos, sz);
076: break;
077: } else {
078: System.arraycopy(buf.cbuf, 0, a, pos, BUF_LEN);
079: pos += BUF_LEN;
080: }
081: }
082: return new String(a);
083: }
084:
085: /**
086: * Read all of the bytes from in into a String and close in.
087: */
088: public static String readAll(InputStream in, String encoding)
089: throws IOException {
090: ByteBuf root = new ByteBuf();
091: int sz = in.read(root.bbuf);
092: if (sz < 0) {
093: in.close();
094: return "";
095: }
096: if (sz < BUF_LEN) {
097: in.close();
098: return new String(root.bbuf, 0, sz, encoding);
099: }
100: int bufCount = 0;
101: for (ByteBuf buf = root;;) {
102: buf = buf.next = new ByteBuf();
103: ++bufCount;
104: sz = in.read(buf.bbuf);
105: if (sz < 0) {
106: sz = 0;
107: break;
108: }
109: if (sz < BUF_LEN)
110: break;
111: }
112: in.close();
113: int tot = bufCount * BUF_LEN + sz;
114: byte[] a = new byte[tot];
115: int pos = 0;
116: for (ByteBuf buf = root;; buf = buf.next) {
117: if (buf.next == null) {
118: if (sz > 0)
119: System.arraycopy(buf.bbuf, 0, a, pos, sz);
120: break;
121: } else {
122: System.arraycopy(buf.bbuf, 0, a, pos, BUF_LEN);
123: pos += BUF_LEN;
124: }
125: }
126: return new String(a, encoding);
127: }
128:
129: /**
130: * Read all of the bytes from in into a byte[] and close in.
131: */
132: public static byte[] readAll(InputStream in) throws IOException {
133: ByteBuf root = new ByteBuf();
134: int sz = in.read(root.bbuf);
135: if (sz < 0) {
136: in.close();
137: return new byte[0];
138: }
139: if (sz < BUF_LEN) {
140: in.close();
141: byte[] a = new byte[sz];
142: System.arraycopy(root.bbuf, 0, a, 0, sz);
143: return a;
144: }
145: int bufCount = 0;
146: for (ByteBuf buf = root;;) {
147: buf = buf.next = new ByteBuf();
148: ++bufCount;
149: sz = in.read(buf.bbuf);
150: if (sz < 0) {
151: sz = 0;
152: break;
153: }
154: if (sz < BUF_LEN)
155: break;
156: }
157: in.close();
158: int tot = bufCount * BUF_LEN + sz;
159: byte[] a = new byte[tot];
160: int pos = 0;
161: for (ByteBuf buf = root;; buf = buf.next) {
162: if (buf.next == null) {
163: if (sz > 0)
164: System.arraycopy(buf.bbuf, 0, a, pos, sz);
165: break;
166: } else {
167: System.arraycopy(buf.bbuf, 0, a, pos, BUF_LEN);
168: pos += BUF_LEN;
169: }
170: }
171: return a;
172: }
173:
174: }
|