KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > io > ReaderInputStream


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.util.io;
20
21 import java.io.*;
22
23
24 /**
25 * This class convert Reader to InputStream. It works by converting
26 * the characters to the encoding specified in constructor parameter.
27 *
28 * @author Petr Hamernik, David Strupl
29 */

30 public class ReaderInputStream extends InputStream {
31     /** Input Reader class. */
32     private Reader reader;
33     private PipedOutputStream pos;
34     private PipedInputStream pis;
35     private OutputStreamWriter osw;
36
37     /** Creates new input stream from the given reader.
38      * Uses the platform default encoding.
39     * @param reader Input reader
40     */

41     public ReaderInputStream(Reader reader) throws IOException {
42         this.reader = reader;
43         pos = new PipedOutputStream();
44         pis = new PipedInputStream(pos);
45         osw = new OutputStreamWriter(pos);
46     }
47
48     /** Creates new input stream from the given reader and encoding.
49      * @param reader Input reader
50      * @param encoding
51      */

52     public ReaderInputStream(Reader reader, String JavaDoc encoding)
53     throws IOException {
54         this.reader = reader;
55         pos = new PipedOutputStream();
56         pis = new PipedInputStream(pos);
57         osw = new OutputStreamWriter(pos, encoding);
58     }
59
60     public int read() throws IOException {
61         if (pis.available() > 0) {
62             return pis.read();
63         }
64
65         int c = reader.read();
66
67         if (c == -1) {
68             return c;
69         }
70
71         osw.write(c);
72         osw.flush();
73         pos.flush();
74
75         return pis.read();
76     }
77
78     public int read(byte[] b, int off, int len) throws IOException {
79         int c = read();
80
81         if (c == -1) {
82             return -1;
83         }
84
85         b[off] = (byte) c;
86
87         int i = 1;
88
89         // Don't try to fill up the buffer if the reader is waiting.
90
for (; (i < len) && reader.ready(); i++) {
91             c = read();
92
93             if (c == -1) {
94                 return i;
95             }
96
97             b[off + i] = (byte) c;
98         }
99
100         return i;
101     }
102
103     public int available() throws IOException {
104         int i = pis.available();
105
106         if (i > 0) {
107             return i;
108         }
109
110         if (reader.ready()) {
111             // Char must produce at least one byte.
112
return 1;
113         } else {
114             return 0;
115         }
116     }
117
118     public void close() throws IOException {
119         reader.close();
120         osw.close();
121         pis.close();
122     }
123 }
124
Popular Tags