Initial revision
[gcc.git] / libjava / java / io / PipedWriter.java
1 // PipedWriter.java - Piped character stream.
2
3 /* Copyright (C) 1998, 1999 Cygnus Solutions
4
5 This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
10
11 package java.io;
12
13 /**
14 * @author Tom Tromey <tromey@cygnus.com>
15 * @date September 25, 1998
16 */
17
18 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
19 * "The Java Language Specification", ISBN 0-201-63451-1
20 * Status: Complete to 1.1.
21 */
22
23 public class PipedWriter extends Writer
24 {
25 public void close () throws IOException
26 {
27 closed = true;
28 }
29
30 public void connect (PipedReader sink) throws IOException
31 {
32 if (closed)
33 throw new IOException ("already closed");
34 if (reader != null)
35 {
36 if (reader == sink)
37 return;
38 throw new IOException ("already connected");
39 }
40 try
41 {
42 reader = sink;
43 reader.connect(this);
44 }
45 catch (IOException e)
46 {
47 reader = null;
48 throw e;
49 }
50 }
51
52 public void flush () throws IOException
53 {
54 // We'll throw an exception if we're closed, but there's nothing
55 // else to do here.
56 if (closed)
57 throw new IOException ("closed");
58 }
59
60 public PipedWriter ()
61 {
62 super ();
63 closed = false;
64 }
65
66 public PipedWriter (PipedReader sink) throws IOException
67 {
68 super ();
69 closed = false;
70 connect (sink);
71 }
72
73 public void write (char buffer[], int offset, int count) throws IOException
74 {
75 if (closed)
76 throw new IOException ("closed");
77 reader.receive(buffer, offset, count);
78 }
79
80 boolean isClosed ()
81 {
82 return closed;
83 }
84
85 // The associated reader.
86 private PipedReader reader;
87 private boolean closed;
88 }