001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.io;
019:
020: import org.apache.harmony.luni.util.Msg;
021:
022: /**
023: * PipedWriter is a class which places information on a communications pipe.
024: * When two threads want to pass data back and forth, one creates a piped writer
025: * and the other creates a piped reader.
026: *
027: * @see PipedReader
028: */
029: public class PipedWriter extends Writer {
030: /**
031: * The destination PipedReader
032: */
033: private PipedReader dest;
034:
035: private boolean closed;
036:
037: /**
038: * Constructs a new unconnected PipedWriter. The resulting Stream must be
039: * connected to a PipedReader before data may be written to it.
040: */
041: public PipedWriter() {
042: super ();
043: }
044:
045: /**
046: * Constructs a new PipedWriter connected to the PipedReader
047: * <code>dest</code>. Any data written to this Writer can be read from
048: * the <code>dest</code>.
049: *
050: * @param dest
051: * the PipedReader to connect to.
052: *
053: * @throws java.io.IOException
054: * if <code>dest</code> is already connected.
055: */
056: public PipedWriter(PipedReader dest) throws IOException {
057: super (dest);
058: connect(dest);
059: }
060:
061: /**
062: * Close this PipedWriter. Any data buffered in the corresponding
063: * PipedReader can be read, then -1 will be returned to the reader. If this
064: * Writer is not connected, this method does nothing.
065: *
066: * @throws java.io.IOException
067: * If an error occurs attempting to close this PipedWriter.
068: */
069: @Override
070: public void close() throws IOException {
071: synchronized (lock) {
072: /* Is the pipe connected? */
073: if (dest != null) {
074: dest.done();
075: dest = null;
076: }
077: closed = true;
078: }
079: }
080:
081: /**
082: * Connects this PipedWriter to a PipedReader. Any data written to this
083: * Writer becomes readable in the Reader.
084: *
085: * @param stream
086: * the destination PipedReader.
087: *
088: * @throws java.io.IOException
089: * If this Writer or the dest is already connected.
090: */
091: public void connect(PipedReader stream) throws IOException {
092: synchronized (lock) {
093: if (this .dest != null) {
094: throw new IOException(Msg.getString("K0079")); //$NON-NLS-1$
095: }
096: if (closed) {
097: throw new IOException(Msg.getString("K0078")); //$NON-NLS-1$
098: }
099: stream.establishConnection();
100: this .dest = stream;
101: }
102: }
103:
104: /**
105: * Notifies the readers on the PipedReader that characters can be read. This
106: * method does nothing if this Writer is not connected.
107: *
108: * @throws java.io.IOException
109: * If an IO error occurs during the flush.
110: */
111: @Override
112: public void flush() throws IOException {
113: if (dest != null) {
114: dest.flush();
115: }
116: }
117:
118: /**
119: * Writes <code>count</code> <code>chars</code> from the char array
120: * <code>buffer</code> starting at offset <code>index</code> to this
121: * PipedWriter. The written data can now be read from the destination
122: * PipedReader. Separate threads should be used for the reader of the
123: * PipedReader and the PipedWriter. There may be undesirable results if more
124: * than one Thread interacts a input or output pipe.
125: *
126: * @param buffer
127: * the buffer to be written
128: * @param offset
129: * offset in buffer to get chars
130: * @param count
131: * number of chars in buffer to write
132: *
133: * @throws java.io.IOException
134: * If the receiving thread was terminated without closing the
135: * pipe. This case is not currently handled correctly.
136: * @throws java.io.InterruptedIOException
137: * If the pipe is full and the current thread is interrupted
138: * waiting for space to write data. This case is not currently
139: * handled correctly.
140: * @throws java.lang.NullPointerException
141: * If the receiver has not been connected yet.
142: * @throws java.lang.IllegalArgumentException
143: * If any of the arguments are out of bounds.
144: */
145: @Override
146: public void write(char buffer[], int offset, int count)
147: throws IOException {
148: synchronized (lock) {
149: if (closed) {
150: throw new IOException(Msg.getString("K0078")); //$NON-NLS-1$
151: }
152: if (dest == null) {
153: throw new IOException(Msg.getString("K007b")); //$NON-NLS-1$
154: }
155: if (buffer == null) {
156: throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
157: }
158:
159: // avoid int overflow
160: if (offset < 0 || offset > buffer.length || count < 0
161: || count > buffer.length - offset) {
162: throw new IndexOutOfBoundsException();
163: }
164: dest.receive(buffer, offset, count);
165: }
166: }
167:
168: /**
169: * Writes the character <code>c</code> to this PipedWriter. The written
170: * data can now be read from the destination PipedReader. Separate threads
171: * should be used for the reader of the PipedReader and the PipedWriter.
172: * There may be undesirable results if more than one Thread interacts a
173: * input or output pipe.
174: *
175: * @param c
176: * the character to be written
177: *
178: * @throws java.io.IOException
179: * If the receiving thread was terminated without closing the
180: * pipe. This case is not currently handled correctly.
181: * @throws java.io.InterruptedIOException
182: * If the pipe is full and the current thread is interrupted
183: * waiting for space to write data. This case is not currently
184: * handled correctly.
185: * @throws java.lang.NullPointerException
186: * If the receiver has not been connected yet.
187: */
188: @Override
189: public void write(int c) throws IOException {
190: synchronized (lock) {
191: if (closed) {
192: throw new IOException(Msg.getString("K0078")); //$NON-NLS-1$
193: }
194: if (dest == null) {
195: throw new IOException(Msg.getString("K007b")); //$NON-NLS-1$
196: }
197: dest.receive((char) c);
198: }
199: }
200: }
|