KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > configuration > ReaderInputStream


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.configuration;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.Reader JavaDoc;
21
22
23 /**
24  * TODO: Refactor this class to support unicode convertion similar to native2ascii works
25  *
26  * @author Fyodor Kupolov
27  * @version 1.0
28  */

29 class ReaderInputStream extends InputStream JavaDoc {
30     private Reader JavaDoc reader;
31
32     public ReaderInputStream(Reader JavaDoc reader) {
33         this.reader = reader;
34     }
35
36     public int read() throws IOException JavaDoc {
37         return reader.read();
38     }
39
40     private char[] tmpBuf;//used to minimize memory allocations
41

42     public int read(final byte b[], final int off, final int len)
43             throws IOException JavaDoc {
44         char[] c;
45         if (tmpBuf != null && tmpBuf.length >= len) {
46             c = tmpBuf;
47         } else {
48             c = new char[len];
49         }
50
51         int n = reader.read(c, 0, len);
52         tmpBuf = c;
53
54         if (n > 0) {
55             for (int i = 0; i < n; i++) {
56                 b[off + i] = (byte) c[i];
57             }
58         }
59
60         return n;
61     }
62
63     public void close() throws IOException JavaDoc {
64         tmpBuf = null;
65         reader.close();
66     }
67 }
68
Popular Tags