001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.vfs;
030:
031: import java.io.IOException;
032: import java.io.InterruptedIOException;
033:
034: /**
035: * Stream allowing two threads to read and write to each other.
036: */
037: public class PipeStream extends StreamImpl {
038: private PipeStream sibling;
039: private byte[] readBuffer;
040: private int readOffset;
041: private int readLength;
042:
043: private PipeStream() {
044: setPath(new NullPath("pipe"));
045: readBuffer = new byte[2 * TempBuffer.SIZE];
046: readOffset = 0;
047: readLength = 0;
048: }
049:
050: /**
051: * Creates a pipe pair. The first object is a ReadStream, the second
052: * is a WriteStream.
053: */
054: public static Object[] create() {
055: PipeStream a = new PipeStream();
056: PipeStream b = new PipeStream();
057:
058: a.sibling = b;
059: b.sibling = a;
060:
061: return new Object[] { new ReadStream(a, null),
062: new WriteStream(b) };
063: }
064:
065: /**
066: * PipeStreams can read
067: */
068: public boolean canRead() {
069: return true;
070: }
071:
072: /**
073: * Reads the available bytes if any, otherwise block.
074: */
075: public int read(byte[] buf, int offset, int length)
076: throws IOException {
077: if (readBuffer == null)
078: return 0;
079:
080: synchronized (this ) {
081: try {
082: if (readOffset >= readLength) {
083: // Sibling has closed
084: if (sibling.readBuffer == null)
085: return 0;
086:
087: notifyAll();
088: wait();
089: }
090:
091: int sublen = readLength - readOffset;
092: if (sublen <= 0)
093: return 0;
094:
095: if (length < sublen)
096: sublen = length;
097:
098: System.arraycopy(readBuffer, readOffset, buf, offset,
099: sublen);
100: readOffset += sublen;
101:
102: return sublen;
103: } catch (InterruptedException e) {
104: throw new InterruptedIOException(e.getMessage());
105: }
106: }
107: }
108:
109: /**
110: * Return the available bytes.
111: */
112: public int getAvailable() throws IOException {
113: synchronized (this ) {
114: return readLength - readOffset;
115: }
116: }
117:
118: /**
119: * The pipe stream can write.
120: */
121: public boolean canWrite() {
122: return true;
123: }
124:
125: /**
126: * Implementation of the pipe write.
127: *
128: * @param buf byte buffer containing the bytes
129: * @param offset offset where to start writing
130: * @param length number of bytes to write
131: * @param isEnd true when the write is flushing a close.
132: */
133: public void write(byte[] buf, int offset, int length, boolean isEnd)
134: throws IOException {
135: while (length > 0) {
136: synchronized (sibling) {
137: if (sibling.readBuffer == null)
138: return;
139:
140: if (sibling.readLength == sibling.readBuffer.length) {
141: if (sibling.readOffset < sibling.readLength) {
142: try {
143: sibling.wait();
144: } catch (InterruptedException e) {
145: throw new InterruptedIOException(e
146: .getMessage());
147: }
148: }
149: sibling.readOffset = 0;
150: sibling.readLength = 0;
151: }
152:
153: if (sibling.readOffset == sibling.readLength) {
154: sibling.readOffset = 0;
155: sibling.readLength = 0;
156: }
157:
158: if (sibling.readBuffer == null)
159: return;
160:
161: int sublen = sibling.readBuffer.length
162: - sibling.readLength;
163: if (length < sublen)
164: sublen = length;
165:
166: System.arraycopy(buf, offset, sibling.readBuffer,
167: sibling.readLength, sublen);
168:
169: sibling.readLength += sublen;
170:
171: length -= sublen;
172: offset += sublen;
173:
174: sibling.notifyAll();
175: }
176: }
177: }
178:
179: public void close() throws IOException {
180: if (readBuffer == null)
181: return;
182:
183: synchronized (this ) {
184: readBuffer = null;
185: readLength = 0;
186: readOffset = 0;
187:
188: notifyAll();
189: }
190:
191: synchronized (sibling) {
192: sibling.notifyAll();
193: }
194: }
195: }
|