1 21 package org.apache.derby.client.net; 22 23 import java.io.InputStream ; 24 import java.io.Reader ; 25 import java.io.OutputStreamWriter ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.ByteArrayInputStream ; 28 29 import java.io.IOException ; 30 import java.io.UnsupportedEncodingException ; 31 32 import org.apache.derby.iapi.services.sanity.SanityManager; 33 34 47 public final class EncodedInputStream extends InputStream { 48 49 55 public static EncodedInputStream createUTF8Stream(Reader reader) { 56 return new EncodedInputStream(reader, 57 "UTF8", 58 BUFFERED_CHAR_LEN, 59 BUFFERED_CHAR_LEN*3); 60 } 61 62 68 static EncodedInputStream createUTF16BEStream(Reader 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 suspendMarker = new ByteArrayInputStream ( new byte[ 0 ] ); 77 78 private Reader reader_; 79 private final char[] decodedBuffer_; 80 81 private OutputStreamWriter encodedStreamWriter_; 82 private PublicBufferOutputStream encodedOutputStream_; 83 84 private ByteArrayInputStream encodedInputStream_; 85 86 96 private EncodedInputStream(Reader reader, 97 String 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 (encodedOutputStream_, 109 encoding); 110 111 }catch(UnsupportedEncodingException e){ 112 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 reEncode(Reader reader) 126 throws IOException 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 (encodedOutputStream_.getBuffer(), 145 0, 146 encodedLength); 147 } 148 149 150 public int available() 151 throws IOException { 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 { 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 { 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 { 216 close(); 217 } 218 219 220 static class PublicBufferOutputStream extends ByteArrayOutputStream { 221 222 PublicBufferOutputStream(int size){ 223 super(size); 224 } 225 226 public byte[] getBuffer(){ 227 return buf; 228 } 229 230 } 231 } 232 | Popular Tags |