001: /*
002:
003: Derby - Class org.apache.derby.impl.drda.ReEncodedInputStream
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021: package org.apache.derby.impl.drda;
022:
023: import java.io.InputStream;
024: import java.io.Reader;
025: import java.io.OutputStreamWriter;
026: import java.io.ByteArrayOutputStream;
027: import java.io.ByteArrayInputStream;
028:
029: import java.io.IOException;
030: import java.io.UnsupportedEncodingException;
031:
032: import org.apache.derby.iapi.services.sanity.SanityManager;
033:
034: /**
035: *
036: * ReEncodedInputStream passes
037: * stream from Reader, which is stream of decoded style,
038: * to user of this subclass of InputStream, which is stream of encoded style.
039: *
040: * The encoding of stream passed to user is limited to UTF8.
041: *
042: * This class will be used to pass stream, which is served as a Reader,
043: * as a InputStream of a arbitrary encoding.
044: *
045: */
046: public class ReEncodedInputStream extends InputStream {
047:
048: private static final int BUFFERED_CHAR_LEN = 1024;
049:
050: private Reader reader_;
051: private char[] decodedBuffer_;
052:
053: private OutputStreamWriter encodedStreamWriter_;
054: private PublicBufferOutputStream encodedOutputStream_;
055:
056: private ByteArrayInputStream encodedInputStream_;
057:
058: public ReEncodedInputStream(Reader reader) throws IOException {
059:
060: reader_ = reader;
061: decodedBuffer_ = new char[BUFFERED_CHAR_LEN];
062:
063: encodedOutputStream_ = new PublicBufferOutputStream(
064: BUFFERED_CHAR_LEN * 3);
065: encodedStreamWriter_ = new OutputStreamWriter(
066: encodedOutputStream_, "UTF8");
067:
068: encodedInputStream_ = reEncode(reader_);
069:
070: }
071:
072: private ByteArrayInputStream reEncode(Reader reader)
073: throws IOException {
074:
075: int count;
076: do {
077: count = reader.read(decodedBuffer_, 0, BUFFERED_CHAR_LEN);
078:
079: } while (count == 0);
080:
081: if (count < 0)
082: return null;
083:
084: encodedOutputStream_.reset();
085: encodedStreamWriter_.write(decodedBuffer_, 0, count);
086: encodedStreamWriter_.flush();
087:
088: int encodedLength = encodedOutputStream_.size();
089:
090: return new ByteArrayInputStream(encodedOutputStream_
091: .getBuffer(), 0, encodedLength);
092: }
093:
094: public int available() throws IOException {
095:
096: if (encodedInputStream_ == null) {
097: return 0;
098: }
099:
100: return encodedInputStream_.available();
101:
102: }
103:
104: public void close() throws IOException {
105:
106: if (encodedInputStream_ != null) {
107: encodedInputStream_.close();
108: encodedInputStream_ = null;
109: }
110:
111: if (reader_ != null) {
112: reader_.close();
113: reader_ = null;
114: }
115:
116: if (encodedStreamWriter_ != null) {
117: encodedStreamWriter_.close();
118: encodedStreamWriter_ = null;
119: }
120:
121: }
122:
123: public int read() throws IOException {
124:
125: if (encodedInputStream_ == null) {
126: return -1;
127: }
128:
129: int c = encodedInputStream_.read();
130:
131: if (c > -1) {
132: return c;
133:
134: } else {
135: encodedInputStream_ = reEncode(reader_);
136:
137: if (encodedInputStream_ == null) {
138: return -1;
139: }
140:
141: return encodedInputStream_.read();
142:
143: }
144:
145: }
146:
147: protected void finalize() throws IOException {
148: close();
149: }
150:
151: static class PublicBufferOutputStream extends ByteArrayOutputStream {
152:
153: PublicBufferOutputStream(int size) {
154: super (size);
155: }
156:
157: public byte[] getBuffer() {
158: return buf;
159: }
160:
161: }
162:
163: }
|