KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > common > io > ReaderInputStream


1 /*
2  * Java Network Programming, Second Edition
3  * Merlin Hughes, Michael Shoffner, Derek Hamner
4  * Manning Publications Company; ISBN 188477749X
5  *
6  * http://nitric.com/jnp/
7  *
8  * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner;
9  * all rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE ABOVE NAMED AUTHORS "AS IS" AND ANY
24  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS, THEIR
27  * PUBLISHER OR THEIR EMPLOYERS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34  * OF THE POSSIBILITY OF SUCH DAMAGE.
35  */

36 package com.ibatis.common.io;
37
38 import java.io.*;
39
40 /**
41  * An InputStream backed by a Reader
42  */

43 public class ReaderInputStream extends InputStream {
44   protected Reader reader;
45   protected ByteArrayOutputStream byteArrayOut;
46   protected Writer writer;
47   protected char[] chars;
48   protected byte[] buffer;
49   protected int index, length;
50
51   /**
52    * Constructor to supply a Reader
53    *
54    * @param reader - the Reader used by the InputStream
55    */

56   public ReaderInputStream(Reader reader) {
57     this.reader = reader;
58     byteArrayOut = new ByteArrayOutputStream();
59     writer = new OutputStreamWriter(byteArrayOut);
60     chars = new char[1024];
61   }
62
63   /**
64    * Constructor to supply a Reader and an encoding
65    *
66    * @param reader - the Reader used by the InputStream
67    * @param encoding - the encoding to use for the InputStream
68    * @throws UnsupportedEncodingException if the encoding is not supported
69    */

70   public ReaderInputStream(Reader reader, String JavaDoc encoding) throws UnsupportedEncodingException {
71     this.reader = reader;
72     byteArrayOut = new ByteArrayOutputStream();
73     writer = new OutputStreamWriter(byteArrayOut, encoding);
74     chars = new char[1024];
75   }
76
77   /**
78    * @see java.io.InputStream#read()
79    */

80   public int read() throws IOException {
81     if (index >= length)
82       fillBuffer();
83     if (index >= length)
84       return -1;
85     return 0xff & buffer[index++];
86   }
87
88   protected void fillBuffer() throws IOException {
89     if (length < 0)
90       return;
91     int numChars = reader.read(chars);
92     if (numChars < 0) {
93       length = -1;
94     } else {
95       byteArrayOut.reset();
96       writer.write(chars, 0, numChars);
97       writer.flush();
98       buffer = byteArrayOut.toByteArray();
99       length = buffer.length;
100       index = 0;
101     }
102   }
103
104   /**
105    * @see java.io.InputStream#read(byte[], int, int)
106    */

107   public int read(byte[] data, int off, int len) throws IOException {
108     if (index >= length)
109       fillBuffer();
110     if (index >= length)
111       return -1;
112     int amount = Math.min(len, length - index);
113     System.arraycopy(buffer, index, data, off, amount);
114     index += amount;
115     return amount;
116   }
117
118   /**
119    * @see java.io.InputStream#available()
120    */

121   public int available() throws IOException {
122     return (index < length) ? length - index :
123         ((length >= 0) && reader.ready()) ? 1 : 0;
124   }
125
126   /**
127    * @see java.io.InputStream#close()
128    */

129   public void close() throws IOException {
130     reader.close();
131   }
132 }
133
Popular Tags