| 1 |
// Copyright 2003, FreeHEP. |
| 2 |
package org.freehep.util.io; |
| 3 |
|
| 4 |
import java.io.IOException; |
| 5 |
import java.io.InputStream; |
| 6 |
import java.io.Reader; |
| 7 |
|
| 8 |
/** |
| 9 |
* The ReaderInputStream makes a Reader look like an InputStream so one can use |
| 10 |
* any of the filters. This is not without danger of loosing some data. |
| 11 |
* |
| 12 |
* IMPORTANT: inherits from InputStream rather than FilterInputStream so that |
| 13 |
* the correct read(byte[], int, int) method is used. |
| 14 |
* |
| 15 |
* @author Mark Donszelmann |
| 16 |
* @version $Id: ReaderInputStream.java 8584 2006-08-10 23:06:37Z duns $ |
| 17 |
*/ |
| 18 |
public class ReaderInputStream extends InputStream { |
| 19 |
|
| 20 |
private Reader reader; |
| 21 |
|
| 22 |
/** |
| 23 |
* Creates an InputStream from given Reader. |
| 24 |
* |
| 25 |
* @param reader reader to read from. |
| 26 |
*/ |
| 27 |
public ReaderInputStream(Reader reader) { |
| 28 |
this.reader = reader; |
| 29 |
} |
| 30 |
|
| 31 |
// Note: here we may loose Unicode data. |
| 32 |
public int read() throws IOException { |
| 33 |
return reader.read() & 0xFF; |
| 34 |
} |
| 35 |
|
| 36 |
// Note: here we may loose Unicode data. |
| 37 |
public int read(byte[] b) throws IOException { |
| 38 |
return read(b, 0, b.length); |
| 39 |
} |
| 40 |
|
| 41 |
// Note: here we may loose Unicode data. |
| 42 |
public int read(byte[] b, int off, int len) throws IOException { |
| 43 |
char c[] = new char[len]; |
| 44 |
int result = reader.read(c, 0, len); |
| 45 |
for (int i = 0; i < len; i++) { |
| 46 |
b[off + i] = (byte) (c[i] & 0xFF); |
| 47 |
} |
| 48 |
return result; |
| 49 |
} |
| 50 |
|
| 51 |
public int available() throws IOException { |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
public void close() throws IOException { |
| 56 |
reader.close(); |
| 57 |
} |
| 58 |
|
| 59 |
public void mark(int readAheadLimit) { |
| 60 |
// Note: IOException ignored |
| 61 |
try { |
| 62 |
reader.mark(readAheadLimit); |
| 63 |
} catch (IOException ioe) { |
| 64 |
// ignored |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public boolean markSupported() { |
| 69 |
return reader.markSupported(); |
| 70 |
} |
| 71 |
|
| 72 |
public void reset() throws IOException { |
| 73 |
reader.reset(); |
| 74 |
} |
| 75 |
|
| 76 |
public long skip(long n) throws IOException { |
| 77 |
return reader.skip(n); |
| 78 |
} |
| 79 |
} |