2
* An InputStreamReader that does no character encoding translations.
3
* Copyright (C) 2001 Stephen Ostermiller <utils@Ostermiller.com>
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/
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.
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.
19
* See COPYING.TXT for details.
22
// package com.Ostermiller.util;
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.
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.
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>
40
public class StraightStreamReader extends Reader{
43
* The input stream from which all methods in this class read.
45
private InputStream in;
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
54
private byte[] buffer;
57
* Create a StraightStreamReader from an InputStream
59
* @param in InputStream to wrap a Reader around.
61
public StraightStreamReader(InputStream in) {
68
* @throws IOException If an I/O error occurs
70
public void close() throws IOException {
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.
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
84
public void mark(int readAheadLimit) throws IOException {
85
in.mark(readAheadLimit);
89
* Tell whether this stream supports the mark() operation.
91
* @return true if and only if this stream supports the mark operation.
93
public boolean markSupported(){
94
return in.markSupported();
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.
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
105
public int read() throws IOException {
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.
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
117
public int read(char[] cbuf) throws IOException {
118
return read(cbuf, 0, cbuf.length);
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.
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
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];
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]);
146
* Tell whether this stream is ready to be read.
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
152
public boolean ready() throws IOException {
153
return (in.available() > 0);
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().
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
166
public void reset() throws IOException {
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.
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
179
public long skip(long n) throws IOException {
184
* Regression test for this class. If this class is working, this should
185
* run and print no errors.
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.
191
* @param args command line arguments (ignored)
193
private static void main(String[] args){
195
File f = new File("test.txt");
197
throw new IOException(f + " already exists. I don't want to overwrite it.");
199
StraightStreamReader in;
200
char[] cbuf = new char[0x1000];
204
// write a file with all possible values of bytes
205
FileOutputStream out = new FileOutputStream(f);
206
for (int i=0x00; i<0x100; i++){
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++){
216
System.err.println("Error: " + i + " read as " + read);
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));
227
for (int i=0x00; i<totRead; i++){
229
System.err.println("Error: 0x" + i + " read as 0x" + cbuf[i]);
234
// read it back using buffer read method.
235
in = new StraightStreamReader(new FileInputStream(f));
237
while (totRead <= 0x100 && (read = in.read(cbuf, totRead, 0x100 - totRead)) > 0){
240
if (totRead != 0x100){
241
System.err.println("Not enough read. Bytes read: " + Integer.toHexString(totRead));
243
for (int i=0x00; i<totRead; i++){
245
System.err.println("Error: 0x" + i + " read as 0x" + cbuf[i]);
250
// read it back using an offset buffer read method.
251
in = new StraightStreamReader(new FileInputStream(f));
253
while (totRead <= 0x100 && (read = in.read(cbuf, totRead+0x123, 0x100 - totRead)) > 0){
256
if (totRead != 0x100){
257
System.err.println("Not enough read. Bytes read: " + Integer.toHexString(totRead));
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]);
266
// read it back using a partial offset buffer read method.
267
in = new StraightStreamReader(new FileInputStream(f));
269
while (totRead <= 0x100 && (read = in.read(cbuf, totRead+0x123, 7)) > 0){
272
if (totRead != 0x100){
273
System.err.println("Not enough read. Bytes read: " + Integer.toHexString(totRead));
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]);
283
} catch (IOException x){
284
System.err.println(x.getMessage());