KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > net > EncodedInputStream


1 /*
2     Derby - Class org.apache.derby.client.net.EncodedInputStream
3
4     Licensed to the Apache Software Foundation (ASF) under one
5     or more contributor license agreements. See the NOTICE file
6     distributed with this work for additional information
7     regarding copyright ownership. The ASF licenses this file
8     to you under the Apache License, Version 2.0 (the
9     "License"); you may not use this file except in compliance
10     with the License. You may obtain a copy of the License at
11
12     http://www.apache.org/licenses/LICENSE-2.0
13
14     Unless required by applicable law or agreed to in writing,
15     software distributed under the License is distributed on an
16     "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17     KIND, either express or implied. See the License for the
18     specific language governing permissions and limitations
19     under the License.
20 */

21 package org.apache.derby.client.net;
22
23 import java.io.InputStream JavaDoc;
24 import java.io.Reader JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.ByteArrayInputStream JavaDoc;
28
29 import java.io.IOException JavaDoc;
30 import java.io.UnsupportedEncodingException JavaDoc;
31
32 import org.apache.derby.iapi.services.sanity.SanityManager;
33
34 /**
35  * Create an encoded stream from a <code>Reader</code>.
36  *
37  * This is an internal class, used to pass readers of characters as streams of
38  * bytes. The characters will be represented according to the specified
39  * encoding. It is up to the caller to ensure the specified encoding is
40  * available, and in general only encodings available as default from Java 1.3
41  * and up should be used.
42  *
43  * Currently, the encodings 'UTF8' and 'UTF-16BE' are used.
44  * Streams are obtained by calling the static methods of this class,
45  * for instance <code>createUTF8Stream</code>.
46  */

47 public final class EncodedInputStream extends InputStream JavaDoc {
48
49     /**
50      * Create a UTF-8 encoded stream from the given <code>Reader</code>.
51      *
52      * @param reader the <code>Reader</code> to read characters from.
53      * @return a byte-stream with UTF-8 encoded characters
54      */

55     public static EncodedInputStream createUTF8Stream(Reader JavaDoc reader) {
56         return new EncodedInputStream(reader,
57                                       "UTF8",
58                                       BUFFERED_CHAR_LEN,
59                                       BUFFERED_CHAR_LEN*3);
60     }
61
62     /**
63      * Create a UTF-16BE encoded stream from the given <code>Reader</code>.
64      *
65      * @param reader the <code>Reader</code> to read characters from.
66      * @return a byte-stream with UTF-16BE encoded characters
67      */

68     static EncodedInputStream createUTF16BEStream(Reader JavaDoc reader) {
69         return new EncodedInputStream(reader,
70                                       "UTF-16BE",
71                                       BUFFERED_CHAR_LEN,
72                                       BUFFERED_CHAR_LEN*2);
73     }
74     
75     private static final int BUFFERED_CHAR_LEN = 1024;
76     private static final ByteArrayInputStream JavaDoc suspendMarker = new ByteArrayInputStream JavaDoc( new byte[ 0 ] );
77
78     private Reader JavaDoc reader_;
79     private final char[] decodedBuffer_;
80     
81     private OutputStreamWriter JavaDoc encodedStreamWriter_;
82     private PublicBufferOutputStream encodedOutputStream_;
83     
84     private ByteArrayInputStream JavaDoc encodedInputStream_;
85     
86     /**
87      * Create an encoded stream for the specified <code>Reader</code>.
88      *
89      * @param reader the <code>Reader</code> to read characters from
90      * @param encoding the encoding to use in the encoded stream
91      * @param charBufferSize the size of the char buffer. This is the number
92      * of characters read at once from the <code>Reader</code>.
93      * @param initialByteBufferSize the initial size of the byte buffer.
94      * holding the encoded bytes
95      */

96     private EncodedInputStream(Reader JavaDoc reader,
97                                String JavaDoc encoding,
98                                int charBufferSize,
99                                int initialByteBufferSize) {
100     
101         reader_ = reader;
102         decodedBuffer_ = new char[charBufferSize];
103
104         encodedOutputStream_ = new PublicBufferOutputStream(
105                 initialByteBufferSize);
106         
107         try{
108             encodedStreamWriter_ = new OutputStreamWriter JavaDoc(encodedOutputStream_,
109                                                           encoding);
110             
111         }catch(UnsupportedEncodingException JavaDoc e){
112             // Should never happen. It is up to the caller to ensure the
113
// specified encoding is available.
114
if (SanityManager.DEBUG) {
115                 SanityManager.THROWASSERT("Unavailable encoding specified: " +
116                         encoding, e);
117             }
118         }
119     
120         encodedInputStream_ = suspendMarker;
121     
122     }
123
124
125     private ByteArrayInputStream JavaDoc reEncode(Reader JavaDoc reader)
126         throws IOException JavaDoc
127     {
128     
129         int count;
130         do{
131             count = reader.read(decodedBuffer_, 0, decodedBuffer_.length);
132             
133         }while(count == 0);
134             
135         if(count < 0)
136             return null;
137     
138         encodedOutputStream_.reset();
139         encodedStreamWriter_.write(decodedBuffer_,0,count);
140         encodedStreamWriter_.flush();
141
142         int encodedLength = encodedOutputStream_.size();
143     
144         return new ByteArrayInputStream JavaDoc(encodedOutputStream_.getBuffer(),
145                                         0,
146                                         encodedLength);
147     }
148     
149     
150     public int available()
151         throws IOException JavaDoc {
152         
153         if(encodedInputStream_ == suspendMarker)
154             encodedInputStream_ = reEncode(reader_);
155
156         if(encodedInputStream_ == null){
157             return 0;
158         }
159
160         return encodedInputStream_.available();
161     
162     }
163     
164
165     public void close()
166         throws IOException JavaDoc {
167     
168         if(encodedInputStream_ != null ){
169             encodedInputStream_.close();
170             encodedInputStream_ = null;
171         }
172
173         if(reader_ != null ){
174             reader_.close();
175             reader_ = null;
176         }
177
178         if(encodedStreamWriter_ != null){
179             encodedStreamWriter_.close();
180             encodedStreamWriter_ = null;
181         }
182     
183     }
184     
185     
186     public int read()
187         throws IOException JavaDoc {
188         
189         if(encodedInputStream_ == suspendMarker)
190             encodedInputStream_ = reEncode(reader_);
191
192         if(encodedInputStream_ == null){
193             return -1;
194         }
195     
196         int c = encodedInputStream_.read();
197
198         if(c > -1){
199             return c;
200         
201         }else{
202             encodedInputStream_ = reEncode(reader_);
203         
204             if(encodedInputStream_ == null){
205                 return -1;
206             }
207         
208             return encodedInputStream_.read();
209
210         }
211     
212     }
213     
214     
215     protected void finalize() throws IOException JavaDoc {
216         close();
217     }
218     
219     
220     static class PublicBufferOutputStream extends ByteArrayOutputStream JavaDoc{
221     
222         PublicBufferOutputStream(int size){
223             super(size);
224         }
225
226         public byte[] getBuffer(){
227             return buf;
228         }
229     
230     }
231 }
232
Popular Tags