source: branches/0.22.x/abcl/src/org/armedbear/lisp/scripting/util/WriterOutputStream.java

Last change on this file was 11360, checked in by ehuelsmann, 16 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.5 KB
Line 
1package org.armedbear.lisp.scripting.util;
2
3import java.io.*;
4
5public class WriterOutputStream extends OutputStream {
6 
7    private final Reader reader;
8    private final Writer writer;
9    private final PipedOutputStream outPipe;
10 
11    public WriterOutputStream(Writer writer) throws IOException {
12        this(writer, null);
13    }
14 
15    public WriterOutputStream(final Writer writer, String encoding) throws IOException {
16        this.writer = writer;
17        outPipe = new PipedOutputStream();
18        InputStream inPipe = new PipedInputStream(outPipe);
19        reader = (encoding == null) ? new InputStreamReader(inPipe) : new InputStreamReader(inPipe, encoding);
20    }
21 
22  @Override
23  public void write(int b) throws IOException {
24    doWrite(b);
25    writer.flush();
26  }
27   
28    @Override
29  public void flush() throws IOException {
30    super.flush();
31  }
32
33  @Override
34  public void write(byte[] b, int off, int len) throws IOException {
35    super.write(b, off, len);
36  }
37
38  @Override
39  public void write(byte[] b) throws IOException {
40    super.write(b);
41  }
42
43  public synchronized void close() throws IOException {
44        close(reader);
45        close(writer);
46        close(outPipe);
47    }
48 
49    private static void close(Closeable cl) {
50        try {
51            cl.close();
52        } catch (IOException e) {
53            e.printStackTrace();
54        }
55    }
56 
57    private void doWrite(int n) throws IOException {
58      outPipe.write(n);
59      outPipe.flush();
60      n = reader.read();
61      writer.write(n);
62    }
63   
64}
Note: See TracBrowser for help on using the repository browser.