KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > drda > ReEncodedInputStream


1 /*
2  
3 Derby - Class org.apache.derby.impl.drda.ReEncodedInputStream
4
5 Licensed to the Apache Software Foundation (ASF) under one or more
6 contributor license agreements. See the NOTICE file distributed with
7 this work for additional information regarding copyright ownership.
8 The ASF licenses this file to You under the Apache License, Version 2.0
9 (the "License"); you may not use this file except in compliance with
10 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, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 */

21 package org.apache.derby.impl.drda;
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  *
36  * ReEncodedInputStream passes
37  * stream from Reader, which is stream of decoded style,
38  * to user of this subclass of InputStream, which is stream of encoded style.
39  *
40  * The encoding of stream passed to user is limited to UTF8.
41  *
42  * This class will be used to pass stream, which is served as a Reader,
43  * as a InputStream of a arbitrary encoding.
44  *
45  */

46 public class ReEncodedInputStream extends InputStream JavaDoc {
47
48     private static final int BUFFERED_CHAR_LEN = 1024;
49
50     private Reader JavaDoc reader_;
51     private char[] decodedBuffer_;
52     
53     private OutputStreamWriter JavaDoc encodedStreamWriter_;
54     private PublicBufferOutputStream encodedOutputStream_;
55     
56     private ByteArrayInputStream JavaDoc encodedInputStream_;
57     
58     public ReEncodedInputStream(Reader JavaDoc reader)
59     throws IOException JavaDoc {
60     
61     reader_ = reader;
62     decodedBuffer_ = new char[BUFFERED_CHAR_LEN];
63
64     encodedOutputStream_ = new PublicBufferOutputStream( BUFFERED_CHAR_LEN * 3 );
65     encodedStreamWriter_ = new OutputStreamWriter JavaDoc(encodedOutputStream_,"UTF8");
66     
67     encodedInputStream_ = reEncode(reader_);
68     
69     }
70
71
72     private ByteArrayInputStream JavaDoc reEncode(Reader JavaDoc reader)
73     throws IOException JavaDoc
74     {
75     
76         int count;
77         do{
78             count = reader.read(decodedBuffer_, 0, BUFFERED_CHAR_LEN);
79             
80         }while(count == 0);
81             
82         if(count < 0)
83             return null;
84     
85     encodedOutputStream_.reset();
86     encodedStreamWriter_.write(decodedBuffer_,0,count);
87     encodedStreamWriter_.flush();
88
89     int encodedLength = encodedOutputStream_.size();
90     
91     return new ByteArrayInputStream JavaDoc(encodedOutputStream_.getBuffer(),
92                     0,
93                     encodedLength);
94     }
95     
96     
97     public int available()
98     throws IOException JavaDoc {
99     
100     if(encodedInputStream_ == null){
101         return 0;
102     }
103
104     return encodedInputStream_.available();
105     
106     }
107     
108
109     public void close()
110     throws IOException JavaDoc {
111     
112     if(encodedInputStream_ != null ){
113         encodedInputStream_.close();
114         encodedInputStream_ = null;
115     }
116
117     if(reader_ != null ){
118         reader_.close();
119         reader_ = null;
120     }
121
122     if(encodedStreamWriter_ != null){
123         encodedStreamWriter_.close();
124         encodedStreamWriter_ = null;
125     }
126     
127     }
128     
129     
130     public int read()
131     throws IOException JavaDoc {
132     
133     if(encodedInputStream_ == null){
134         return -1;
135     }
136     
137     int c = encodedInputStream_.read();
138
139     if(c > -1){
140         return c;
141         
142     }else{
143         encodedInputStream_ = reEncode(reader_);
144         
145         if(encodedInputStream_ == null){
146         return -1;
147         }
148         
149         return encodedInputStream_.read();
150
151     }
152     
153     }
154     
155     
156     protected void finalize() throws IOException JavaDoc {
157     close();
158     }
159     
160     
161     static class PublicBufferOutputStream extends ByteArrayOutputStream JavaDoc{
162     
163     PublicBufferOutputStream(int size){
164         super(size);
165     }
166
167     public byte[] getBuffer(){
168         return buf;
169     }
170     
171     }
172
173 }
174
175
176
Popular Tags