|
Last change
on this file was
11360,
checked in by ehuelsmann, 17 years ago
|
|
Initial import of ABCL scripting engine implementation.
Patch by: Alessio Stalla <alessiostalla at gmail dot com>
|
-
Property svn:eol-style set to
LF
|
|
File size:
1.8 KB
|
| Line | |
|---|
| 1 | package org.armedbear.lisp.scripting.util; |
|---|
| 2 | |
|---|
| 3 | import java.io.*; |
|---|
| 4 | |
|---|
| 5 | public class ReaderInputStream extends InputStream { |
|---|
| 6 | |
|---|
| 7 | private final Reader reader; |
|---|
| 8 | private final Writer writer; |
|---|
| 9 | private final PipedInputStream inPipe; |
|---|
| 10 | |
|---|
| 11 | public ReaderInputStream(Reader reader) throws IOException { |
|---|
| 12 | this(reader, null); |
|---|
| 13 | } |
|---|
| 14 | |
|---|
| 15 | public ReaderInputStream(final Reader reader, String encoding) throws IOException { |
|---|
| 16 | this.reader = reader; |
|---|
| 17 | inPipe = new PipedInputStream(); |
|---|
| 18 | OutputStream outPipe = new PipedOutputStream(inPipe); |
|---|
| 19 | writer = (encoding == null) ? new OutputStreamWriter(outPipe) : new OutputStreamWriter(outPipe, encoding); |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | public int read() throws IOException { |
|---|
| 23 | if(doRead()) { |
|---|
| 24 | return inPipe.read(); |
|---|
| 25 | } else { |
|---|
| 26 | return -1; |
|---|
| 27 | } |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | public int read(byte b[]) throws IOException { |
|---|
| 31 | return super.read(b); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | public int read(byte b[], int off, int len) throws IOException { |
|---|
| 35 | if(len <= 0) { |
|---|
| 36 | return 0; |
|---|
| 37 | } |
|---|
| 38 | int n = read(); |
|---|
| 39 | if(n == -1) { |
|---|
| 40 | return -1; |
|---|
| 41 | } else { |
|---|
| 42 | b[off] = (byte)n; |
|---|
| 43 | } |
|---|
| 44 | return 1; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | public long skip(long n) throws IOException { |
|---|
| 48 | return super.skip(n); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | public int available() throws IOException { |
|---|
| 52 | return 0; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | public synchronized void close() throws IOException { |
|---|
| 56 | close(reader); |
|---|
| 57 | close(writer); |
|---|
| 58 | close(inPipe); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | private static void close(Closeable cl) { |
|---|
| 62 | try { |
|---|
| 63 | cl.close(); |
|---|
| 64 | } catch (IOException e) { |
|---|
| 65 | e.printStackTrace(); |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | private boolean doRead() throws IOException { |
|---|
| 70 | int n = reader.read(); |
|---|
| 71 | if(n == -1) { |
|---|
| 72 | return false; |
|---|
| 73 | } |
|---|
| 74 | writer.write(n); |
|---|
| 75 | writer.flush(); |
|---|
| 76 | return true; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | } |
|---|
Note: See
TracBrowser
for help on using the repository browser.