KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > StraightStreamReader


1 /*
2  * An InputStreamReader that does no character encoding translations.
3  * Copyright (C) 2001 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18
19 package com.Ostermiller.util;
20
21 import java.io.*;
22
23 /**
24  * A StraightStreamReader is a bridge from byte streams to character streams: It reads bytes
25  * and translates them into characters without using a character encoding. The characters
26  * that a StraightStreamReader returns may not be valid Unicode characters but they are
27  * guaranteed to be in the 0x00 to 0xFF range.
28  * More information about this class is available from <a target="_top" HREF=
29  * "http://ostermiller.org/utils/StraightStreamReader.html">ostermiller.org</a>.
30  * <P>
31  * Most of the time you want to do character encoding translation when translating bytes to
32  * characters. If you are planning on displaying the text, you should always do this and should
33  * use an InputStreamReader for the purpose. Sometimes it is useful to treat characters as bytes
34  * with some extra bits. In these cases you would want to use a StraightStreamReader.
35  * <P>
36  * For top efficiency, consider wrapping an StraightStreamReader within a BufferedReader. For example:<br>
37  * <code>BufferedReader in = new BufferedReader(new StraightStreamReader(System.in));</code>
38  *
39  * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
40  * @since ostermillerutils 1.00.00
41  */

42 public class StraightStreamReader extends Reader{
43
44     /**
45      * The input stream from which all methods in this class read.
46      *
47      * @since ostermillerutils 1.00.00
48      */

49     private InputStream in;
50
51     /**
52      * A byte array to be used for calls to the InputStream. This
53      * is cached as a class variable to avoid object creation and
54      * deletion each time a read is called. This buffer may be
55      * null and may not be large enough. Make sure to check i
56      * before using it.
57      *
58      * @since ostermillerutils 1.00.00
59      */

60     private byte[] buffer;
61
62     /**
63      * Create a StraightStreamReader from an InputStream
64      *
65      * @param in InputStream to wrap a Reader around.
66      *
67      * @since ostermillerutils 1.00.00
68      */

69     public StraightStreamReader(InputStream in) {
70         this.in = in;
71     }
72
73     /**
74      * Close the stream.
75      *
76      * @throws IOException If an I/O error occurs
77      *
78      * @since ostermillerutils 1.00.00
79      */

80     public void close() throws IOException {
81         in.close();
82     }
83
84     /**
85      * Mark the present position in the stream. Subsequent calls to reset()
86      * will attempt to reposition the stream to this point. Not all
87      * character-input streams support the mark() operation.
88      *
89      * @param readAheadLimit Limit on the number of characters that may be read
90      * while still preserving the mark. After reading this many characters,
91      * attempting to reset the stream may fail.
92      * @throws IOException If the stream does not support mark(), or if some other I/O error occurs
93      *
94      * @since ostermillerutils 1.00.00
95      */

96     public void mark(int readAheadLimit) throws IOException {
97         in.mark(readAheadLimit);
98     }
99
100     /**
101      * Tell whether this stream supports the mark() operation.
102      *
103      * @return true if and only if this stream supports the mark operation.
104      *
105      * @since ostermillerutils 1.00.00
106      */

107     public boolean markSupported(){
108         return in.markSupported();
109     }
110
111     /**
112      * Read a single character. This method will block until a character is available, an
113      * I/O error occurs, or the end of the stream is reached.
114      *
115      * @return The character read, as an integer in the range 0 to 256 (0x00-0xff), or -1 if
116      * the end of the stream has been reached
117      * @throws IOException If an I/O error occurs
118      *
119      * @since ostermillerutils 1.00.00
120      */

121     public int read() throws IOException {
122         return in.read();
123     }
124
125     /**
126      * Read characters into an array. This method will block until some input is available,
127      * an I/O error occurs, or the end of the stream is reached.
128      *
129      * @param cbuf Destination buffer
130      * @return The number of bytes read, or -1 if the end of the stream has been reached
131      * @throws IOException If an I/O error occurs
132      *
133      * @since ostermillerutils 1.00.00
134      */

135     public int read(char[] cbuf) throws IOException {
136         return read(cbuf, 0, cbuf.length);
137     }
138
139     /**
140      * Read characters into an array. This method will block until some input is available,
141      * an I/O error occurs, or the end of the stream is reached.
142      *
143      * @param cbuf Destination buffer
144      * @param off Offset at which to start storing characters
145      * @param len Maximum number of characters to read
146      * @return The number of bytes read, or -1 if the end of the stream has been reached
147      * @throws IOException If an I/O error occurs
148      *
149      * @since ostermillerutils 1.00.00
150      */

151     public int read(char[] cbuf, int off, int len) throws IOException {
152         // ensure the capacity of the buffer that we will be using
153
// to read from the input stream
154
if (buffer == null || buffer.length < len){
155             buffer = new byte[len];
156         }
157         // read from the input stream and copy it to the character array
158
int length = in.read(buffer, 0, len);
159         for (int i=0; i<length; i++){
160             cbuf[off+i] = (char)(0xFF & buffer[i]);
161         }
162         return length;
163     }
164
165     /**
166      * Tell whether this stream is ready to be read.
167      *
168      * @return True if the next read() is guaranteed not to block for input, false otherwise.
169      * Note that returning false does not guarantee that the next read will block.
170      * @throws IOException If an I/O error occurs
171      *
172      * @since ostermillerutils 1.00.00
173      */

174     public boolean ready() throws IOException {
175         return (in.available() > 0);
176     }
177
178     /**
179      * Reset the stream. If the stream has been marked, then attempt to reposition it at the mark.
180      * If the stream has not been marked, then attempt to reset it in some way appropriate to the
181      * particular stream, for example by repositioning it to its starting point. Not all
182      * character-input streams support the reset() operation, and some support reset()
183      * without supporting mark().
184      *
185      * @throws IOException If the stream has not been marked, or if the mark has been invalidated,
186      * or if the stream does not support reset(), or if some other I/O error occurs
187      *
188      * @since ostermillerutils 1.00.00
189      */

190     public void reset() throws IOException {
191         in.reset();
192     }
193
194     /**
195      * Skip characters. This method will block until some characters are available,
196      * an I/O error occurs, or the end of the stream is reached.
197      *
198      * @param n The number of characters to skip
199      * @return The number of characters actually skipped
200      * @throws IllegalArgumentException If n is negative
201      * @throws IOException If an I/O error occurs
202      *
203      * @since ostermillerutils 1.00.00
204      */

205     public long skip(long n) throws IOException {
206         return in.skip(n);
207     }
208
209     /**
210      * Regression test for this class. If this class is working, this should
211      * run and print no errors.
212      * <P>
213      * This method creates a temporary file in the working directory called "test.txt".
214      * This file should not exist before hand, and the program should have create,
215      * read, write, and delete access to this file.
216      *
217      * @param args command line arguments (ignored)
218      *
219      * @since ostermillerutils 1.00.00
220      */

221     private static void main(String JavaDoc[] args){
222         try {
223             File f = new File("test.txt");
224             if (f.exists()){
225                 throw new IOException(f + " already exists. I don't want to overwrite it.");
226             }
227             StraightStreamReader in;
228             char[] cbuf = new char[0x1000];
229             int read;
230             int totRead;
231
232             // write a file with all possible values of bytes
233
FileOutputStream out = new FileOutputStream(f);
234             for (int i=0x00; i<0x100; i++){
235                 out.write(i);
236             }
237             out.close();
238
239             // read it back using the read single character method
240
in = new StraightStreamReader(new FileInputStream(f));
241             for (int i=0x00; i<0x100; i++){
242                 read = in.read();
243                 if (read != i){
244                     System.err.println("Error: " + i + " read as " + read);
245                 }
246             }
247             in.close();
248
249             // read as much of it back as possible with one simple buffer read.
250
in = new StraightStreamReader(new FileInputStream(f));
251             totRead = in.read(cbuf);
252             if (totRead != 0x100){
253                 System.err.println("Simple buffered read did not read the full amount: 0x" + Integer.toHexString(totRead));
254             }
255             for (int i=0x00; i<totRead; i++){
256                  if (cbuf[i] != i){
257                     System.err.println("Error: 0x" + i + " read as 0x" + cbuf[i]);
258                 }
259             }
260             in.close();
261
262             // read it back using buffer read method.
263
in = new StraightStreamReader(new FileInputStream(f));
264             totRead = 0;
265             while (totRead <= 0x100 && (read = in.read(cbuf, totRead, 0x100 - totRead)) > 0){
266                 totRead += read;
267             }
268             if (totRead != 0x100){
269                 System.err.println("Not enough read. Bytes read: " + Integer.toHexString(totRead));
270             }
271             for (int i=0x00; i<totRead; i++){
272                  if (cbuf[i] != i){
273                     System.err.println("Error: 0x" + i + " read as 0x" + cbuf[i]);
274                 }
275             }
276             in.close();
277
278             // read it back using an offset buffer read method.
279
in = new StraightStreamReader(new FileInputStream(f));
280             totRead = 0;
281             while (totRead <= 0x100 && (read = in.read(cbuf, totRead+0x123, 0x100 - totRead)) > 0){
282                 totRead += read;
283             }
284             if (totRead != 0x100){
285                 System.err.println("Not enough read. Bytes read: " + Integer.toHexString(totRead));
286             }
287             for (int i=0x00; i<totRead; i++){
288                  if (cbuf[i+0x123] != i){
289                     System.err.println("Error: 0x" + i + " read as 0x" + cbuf[i+0x123]);
290                 }
291             }
292             in.close();
293
294             // read it back using a partial offset buffer read method.
295
in = new StraightStreamReader(new FileInputStream(f));
296             totRead = 0;
297             while (totRead <= 0x100 && (read = in.read(cbuf, totRead+0x123, 7)) > 0){
298                 totRead += read;
299             }
300             if (totRead != 0x100){
301                 System.err.println("Not enough read. Bytes read: " + Integer.toHexString(totRead));
302             }
303             for (int i=0x00; i<totRead; i++){
304                  if (cbuf[i+0x123] != i){
305                     System.err.println("Error: 0x" + i + " read as 0x" + cbuf[i+0x123]);
306                 }
307             }
308             in.close();
309
310             f.delete();
311         } catch (IOException x){
312             System.err.println(x.getMessage());
313         }
314     }
315 }
316
Popular Tags