001: /*
002: * Java Network Programming, Second Edition
003: * Merlin Hughes, Michael Shoffner, Derek Hamner
004: * Manning Publications Company; ISBN 188477749X
005: *
006: * http://nitric.com/jnp/
007: *
008: * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner;
009: * all rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions
013: * are met:
014: *
015: * 1. Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * 2. Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in
020: * the documentation and/or other materials provided with the
021: * distribution.
022: *
023: * THIS SOFTWARE IS PROVIDED BY THE ABOVE NAMED AUTHORS "AS IS" AND ANY
024: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
025: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS, THEIR
027: * PUBLISHER OR THEIR EMPLOYERS BE LIABLE FOR ANY DIRECT, INDIRECT,
028: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
029: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
030: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
031: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
032: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
033: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
034: * OF THE POSSIBILITY OF SUCH DAMAGE.
035: */
036: package com.ibatis.common.io;
037:
038: import java.io.*;
039:
040: /**
041: * An InputStream backed by a Reader
042: */
043: public class ReaderInputStream extends InputStream {
044: protected Reader reader;
045: protected ByteArrayOutputStream byteArrayOut;
046: protected Writer writer;
047: protected char[] chars;
048: protected byte[] buffer;
049: protected int index, length;
050:
051: /**
052: * Constructor to supply a Reader
053: *
054: * @param reader - the Reader used by the InputStream
055: */
056: public ReaderInputStream(Reader reader) {
057: this .reader = reader;
058: byteArrayOut = new ByteArrayOutputStream();
059: writer = new OutputStreamWriter(byteArrayOut);
060: chars = new char[1024];
061: }
062:
063: /**
064: * Constructor to supply a Reader and an encoding
065: *
066: * @param reader - the Reader used by the InputStream
067: * @param encoding - the encoding to use for the InputStream
068: * @throws UnsupportedEncodingException if the encoding is not supported
069: */
070: public ReaderInputStream(Reader reader, String encoding)
071: throws UnsupportedEncodingException {
072: this .reader = reader;
073: byteArrayOut = new ByteArrayOutputStream();
074: writer = new OutputStreamWriter(byteArrayOut, encoding);
075: chars = new char[1024];
076: }
077:
078: /**
079: * @see java.io.InputStream#read()
080: */
081: public int read() throws IOException {
082: if (index >= length)
083: fillBuffer();
084: if (index >= length)
085: return -1;
086: return 0xff & buffer[index++];
087: }
088:
089: protected void fillBuffer() throws IOException {
090: if (length < 0)
091: return;
092: int numChars = reader.read(chars);
093: if (numChars < 0) {
094: length = -1;
095: } else {
096: byteArrayOut.reset();
097: writer.write(chars, 0, numChars);
098: writer.flush();
099: buffer = byteArrayOut.toByteArray();
100: length = buffer.length;
101: index = 0;
102: }
103: }
104:
105: /**
106: * @see java.io.InputStream#read(byte[], int, int)
107: */
108: public int read(byte[] data, int off, int len) throws IOException {
109: if (index >= length)
110: fillBuffer();
111: if (index >= length)
112: return -1;
113: int amount = Math.min(len, length - index);
114: System.arraycopy(buffer, index, data, off, amount);
115: index += amount;
116: return amount;
117: }
118:
119: /**
120: * @see java.io.InputStream#available()
121: */
122: public int available() throws IOException {
123: return (index < length) ? length - index
124: : ((length >= 0) && reader.ready()) ? 1 : 0;
125: }
126:
127: /**
128: * @see java.io.InputStream#close()
129: */
130: public void close() throws IOException {
131: reader.close();
132: }
133: }
|