KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > StraightStreamReader


1 /*
2  * An InputStreamReader that does no character encoding translations.
3  * Copyright (C) 2001 Stephen Ostermiller <utils@Ostermiller.com>
4  *
5  * Changes: (2001 by Gerwin Klein <lsf@jflex.de>)
6  * - commented out package declaration for the example
7  * - original version at http://www.smo.f2s.com/utils/
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * See COPYING.TXT for details.
20  */

21  
22 // package com.Ostermiller.util;
23

24 import java.io.*;
25
26 /**
27  * A StraightStreamReader is a bridge from byte streams to character streams: It reads bytes
28  * and translates them into characters without using a character encoding. The characters
29  * that a StraightStreamReader returns may not be valid unicode characters but they are
30  * guaranteed to be in the 0x00 to 0xFF range.
31  * <P>
32  * Most of the time you want to do character encoding translation when translating bytes to
33  * characters. If you are planning on displaying the text, you should always do this and should
34  * use an InputStreamReader for the purpose. Sometimes it is useful to treat characters as bytes
35  * with some extra bits. In these cases you would want to use a StraightStreamReader.
36  * <P>
37  * For top efficiency, consider wrapping an StraightStreamReader within a BufferedReader. For example:<br>
38  * <code>BufferedReader in = new BufferedReader(new StraightStreamReader(System.in));</code>
39  */

40 public class StraightStreamReader extends Reader{
41
42     /**
43      * The input stream from which all methods in this class read.
44      */

45     private InputStream in;
46
47     /**
48      * A byte array to be used for calls to the InputStream. This
49      * is cached as a class variable to avoid object creation and
50      * deletion each time a read is called. This buffer may be
51      * null and may not be large enough. Make sure to check it
52      * before using it.
53      */

54     private byte[] buffer;
55
56     /**
57      * Create a StraightStreamReader from an InputStream
58      *
59      * @param in InputStream to wrap a Reader around.
60      */

61     public StraightStreamReader(InputStream in) {
62         this.in = in;
63     }
64
65     /**
66      * Close the stream.
67      *
68      * @throws IOException If an I/O error occurs
69      */

70     public void close() throws IOException {
71         in.close();
72     }
73
74     /**
75      * Mark the present position in the stream. Subsequent calls to reset()
76      * will attempt to reposition the stream to this point. Not all
77      * character-input streams support the mark() operation.
78      *
79      * @param readAheadLimit Limit on the number of characters that may be read
80      * while still preserving the mark. After reading this many characters,
81      * attempting to reset the stream may fail.
82      * @throws IOException If the stream does not support mark(), or if some other I/O error occurs
83      */

84     public void mark(int readAheadLimit) throws IOException {
85         in.mark(readAheadLimit);
86     }
87
88     /**
89      * Tell whether this stream supports the mark() operation.
90      *
91      * @return true if and only if this stream supports the mark operation.
92      */

93     public boolean markSupported(){
94         return in.markSupported();
95     }
96
97     /**
98      * Read a single character. This method will block until a character is available, an
99      * I/O error occurs, or the end of the stream is reached.
100      *
101      * @return The character read, as an integer in the range 0 to 256 (0x00-0xff), or -1 if
102      * the end of the stream has been reached
103      * @throws IOException If an I/O error occurs
104      */

105     public int read() throws IOException {
106         return in.read();
107     }
108
109     /**
110      * Read characters into an array. This method will block until some input is available,
111      * an I/O error occurs, or the end of the stream is reached.
112      *
113      * @param cbuf Destination buffer
114      * @return The number of bytes read, or -1 if the end of the stream has been reached
115      * @throws IOException If an I/O error occurs
116      */

117     public int read(char[] cbuf) throws IOException {
118         return read(cbuf, 0, cbuf.length);
119     }
120     
121     /**
122      * Read characters into an array. This method will block until some input is available,
123      * an I/O error occurs, or the end of the stream is reached.
124      *
125      * @param cbuf Destination buffer
126      * @param off Offset at which to start storing characters
127      * @param len Maximum number of characters to read
128      * @return The number of bytes read, or -1 if the end of the stream has been reached
129      * @throws IOException If an I/O error occurs
130      */

131     public int read(char[] cbuf, int off, int len) throws IOException {
132         // ensure the capacity of the buffer that we will be using
133
// to read from the input stream
134
if (buffer == null || buffer.length < len){
135             buffer = new byte[len];
136         }
137         // read from the input stream and copy it to the character array
138
int length = in.read(buffer, 0, len);
139         for (int i=0; i<length; i++){
140             cbuf[off+i] = (char)(0xFF & buffer[i]);
141         }
142         return length;
143     }
144
145     /**
146      * Tell whether this stream is ready to be read.
147      *
148      * @return True if the next read() is guaranteed not to block for input, false otherwise.
149      * Note that returning false does not guarantee that the next read will block.
150      * @throws IOException If an I/O error occurs
151      */

152     public boolean ready() throws IOException {
153         return (in.available() > 0);
154     }
155
156     /**
157      * Reset the stream. If the stream has been marked, then attempt to reposition it at the mark.
158      * If the stream has not been marked, then attempt to reset it in some way appropriate to the
159      * particular stream, for example by repositioning it to its starting point. Not all
160      * character-input streams support the reset() operation, and some support reset()
161      * without supporting mark().
162      *
163      * @throws IOException If the stream has not been marked, or if the mark has been invalidated,
164      * or if the stream does not support reset(), or if some other I/O error occurs
165      */

166     public void reset() throws IOException {
167         in.reset();
168     }
169
170     /**
171      * Skip characters. This method will block until some characters are available,
172      * an I/O error occurs, or the end of the stream is reached.
173      *
174      * @param n The number of characters to skip
175      * @return The number of characters actually skipped
176      * @throws IllegalArgumentException If n is negative
177      * @throws IOException If an I/O error occurs
178      */

179     public long skip(long n) throws IOException {
180         return in.skip(n);
181     }
182
183     /**
184      * Regression test for this class. If this class is working, this should
185      * run and print no errors.
186      * <P>
187      * This method creates a tempory file in the working directory called "test.txt".
188      * This file should not exist before hand, and the program should have create,
189      * read, write, and delete access to this file.
190      *
191      * @param args command line arguments (ignored)
192      */

193     private static void main(String JavaDoc[] args){
194         try {
195             File f = new File("test.txt");
196             if (f.exists()){
197                 throw new IOException(f + " already exists. I don't want to overwrite it.");
198             }
199             StraightStreamReader in;
200             char[] cbuf = new char[0x1000];
201             int read;
202             int totRead;
203
204             // write a file with all possible values of bytes
205
FileOutputStream out = new FileOutputStream(f);
206             for (int i=0x00; i<0x100; i++){
207                 out.write(i);
208             }
209             out.close();
210
211             // read it back using the read single character method
212
in = new StraightStreamReader(new FileInputStream(f));
213             for (int i=0x00; i<0x100; i++){
214                 read = in.read();
215                 if (read != i){
216                     System.err.println("Error: " + i + " read as " + read);
217                 }
218             }
219             in.close();
220
221             // read as much of it back as possible with one simple buffer read.
222
in = new StraightStreamReader(new FileInputStream(f));
223             totRead = in.read(cbuf);
224             if (totRead != 0x100){
225                 System.err.println("Simple buffered read did not read the full amount: 0x" + Integer.toHexString(totRead));
226             }
227             for (int i=0x00; i<totRead; i++){
228                if (cbuf[i] != i){
229                     System.err.println("Error: 0x" + i + " read as 0x" + cbuf[i]);
230                 }
231             }
232             in.close();
233
234             // read it back using buffer read method.
235
in = new StraightStreamReader(new FileInputStream(f));
236             totRead = 0;
237             while (totRead <= 0x100 && (read = in.read(cbuf, totRead, 0x100 - totRead)) > 0){
238                 totRead += read;
239             }
240             if (totRead != 0x100){
241                 System.err.println("Not enough read. Bytes read: " + Integer.toHexString(totRead));
242             }
243             for (int i=0x00; i<totRead; i++){
244                if (cbuf[i] != i){
245                     System.err.println("Error: 0x" + i + " read as 0x" + cbuf[i]);
246                 }
247             }
248             in.close();
249
250             // read it back using an offset buffer read method.
251
in = new StraightStreamReader(new FileInputStream(f));
252             totRead = 0;
253             while (totRead <= 0x100 && (read = in.read(cbuf, totRead+0x123, 0x100 - totRead)) > 0){
254                 totRead += read;
255             }
256             if (totRead != 0x100){
257                 System.err.println("Not enough read. Bytes read: " + Integer.toHexString(totRead));
258             }
259             for (int i=0x00; i<totRead; i++){
260                if (cbuf[i+0x123] != i){
261                     System.err.println("Error: 0x" + i + " read as 0x" + cbuf[i+0x123]);
262                 }
263             }
264             in.close();
265
266             // read it back using a partial offset buffer read method.
267
in = new StraightStreamReader(new FileInputStream(f));
268             totRead = 0;
269             while (totRead <= 0x100 && (read = in.read(cbuf, totRead+0x123, 7)) > 0){
270                 totRead += read;
271             }
272             if (totRead != 0x100){
273                 System.err.println("Not enough read. Bytes read: " + Integer.toHexString(totRead));
274             }
275             for (int i=0x00; i<totRead; i++){
276                if (cbuf[i+0x123] != i){
277                     System.err.println("Error: 0x" + i + " read as 0x" + cbuf[i+0x123]);
278                 }
279             }
280             in.close();
281
282             f.delete();
283         } catch (IOException x){
284             System.err.println(x.getMessage());
285         }
286     }
287 }
288
Popular Tags