Source Code Cross Referenced for PipedOutputStream.java in  » JDK-Core » io-nio » java » io » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. JDK Core
2. JDK Modules
3. JDK Modules com.sun
4. JDK Modules com.sun.java
5. JDK Modules Platform
6. JDK Modules sun
7. Open Source Build
8. Open Source Graphic Library
9. Open Source IDE Eclipse
10. Open Source J2EE
11. Open Source JDBC Driver
12. Open Source Library
13. Open Source Library Database
14. Open Source Net
15. Open Source Script
16. Science
17. Security
18. Sevlet Container
19. SUN GlassFish
20. Swing Library
21. Web Services apache cxf 2.0.1
22. Web Services AXIS2
23. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java Source Code / Java Documentation » JDK Core » io nio » java.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 1995-2006 Sun Microsystems, Inc.  All Rights Reserved.
003:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004:         *
005:         * This code is free software; you can redistribute it and/or modify it
006:         * under the terms of the GNU General Public License version 2 only, as
007:         * published by the Free Software Foundation.  Sun designates this
008:         * particular file as subject to the "Classpath" exception as provided
009:         * by Sun in the LICENSE file that accompanied this code.
010:         *
011:         * This code is distributed in the hope that it will be useful, but WITHOUT
012:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013:         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014:         * version 2 for more details (a copy is included in the LICENSE file that
015:         * accompanied this code).
016:         *
017:         * You should have received a copy of the GNU General Public License version
018:         * 2 along with this work; if not, write to the Free Software Foundation,
019:         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020:         *
021:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022:         * CA 95054 USA or visit www.sun.com if you need additional information or
023:         * have any questions.
024:         */
025:
026:        package java.io;
027:
028:        import java.io.*;
029:
030:        /**
031:         * A piped output stream can be connected to a piped input stream 
032:         * to create a communications pipe. The piped output stream is the 
033:         * sending end of the pipe. Typically, data is written to a 
034:         * <code>PipedOutputStream</code> object by one thread and data is 
035:         * read from the connected <code>PipedInputStream</code> by some 
036:         * other thread. Attempting to use both objects from a single thread 
037:         * is not recommended as it may deadlock the thread.
038:         * The pipe is said to be <a name=BROKEN> <i>broken</i> </a> if a
039:         * thread that was reading data bytes from the connected piped input 
040:         * stream is no longer alive.
041:         *
042:         * @author  James Gosling
043:         * @version 1.34, 05/05/07
044:         * @see     java.io.PipedInputStream
045:         * @since   JDK1.0
046:         */
047:        public class PipedOutputStream extends OutputStream {
048:
049:            /* REMIND: identification of the read and write sides needs to be
050:               more sophisticated.  Either using thread groups (but what about
051:               pipes within a thread?) or using finalization (but it may be a
052:               long time until the next GC). */
053:            private PipedInputStream sink;
054:
055:            /**
056:             * Creates a piped output stream connected to the specified piped 
057:             * input stream. Data bytes written to this stream will then be 
058:             * available as input from <code>snk</code>.
059:             *
060:             * @param      snk   The piped input stream to connect to.
061:             * @exception  IOException  if an I/O error occurs.
062:             */
063:            public PipedOutputStream(PipedInputStream snk) throws IOException {
064:                connect(snk);
065:            }
066:
067:            /**
068:             * Creates a piped output stream that is not yet connected to a 
069:             * piped input stream. It must be connected to a piped input stream, 
070:             * either by the receiver or the sender, before being used. 
071:             *
072:             * @see     java.io.PipedInputStream#connect(java.io.PipedOutputStream)
073:             * @see     java.io.PipedOutputStream#connect(java.io.PipedInputStream)
074:             */
075:            public PipedOutputStream() {
076:            }
077:
078:            /**
079:             * Connects this piped output stream to a receiver. If this object
080:             * is already connected to some other piped input stream, an 
081:             * <code>IOException</code> is thrown.
082:             * <p>
083:             * If <code>snk</code> is an unconnected piped input stream and 
084:             * <code>src</code> is an unconnected piped output stream, they may 
085:             * be connected by either the call:
086:             * <blockquote><pre>
087:             * src.connect(snk)</pre></blockquote>
088:             * or the call:
089:             * <blockquote><pre>
090:             * snk.connect(src)</pre></blockquote>
091:             * The two calls have the same effect.
092:             *
093:             * @param      snk   the piped input stream to connect to.
094:             * @exception  IOException  if an I/O error occurs.
095:             */
096:            public synchronized void connect(PipedInputStream snk)
097:                    throws IOException {
098:                if (snk == null) {
099:                    throw new NullPointerException();
100:                } else if (sink != null || snk.connected) {
101:                    throw new IOException("Already connected");
102:                }
103:                sink = snk;
104:                snk.in = -1;
105:                snk.out = 0;
106:                snk.connected = true;
107:            }
108:
109:            /**
110:             * Writes the specified <code>byte</code> to the piped output stream. 
111:             * <p>
112:             * Implements the <code>write</code> method of <code>OutputStream</code>.
113:             *
114:             * @param      b   the <code>byte</code> to be written.
115:             * @exception IOException if the pipe is <a href=#BROKEN> broken</a>,
116:             *		{@link #connect(java.io.PipedInputStream) unconnected},
117:             *		closed, or if an I/O error occurs.
118:             */
119:            public void write(int b) throws IOException {
120:                if (sink == null) {
121:                    throw new IOException("Pipe not connected");
122:                }
123:                sink.receive(b);
124:            }
125:
126:            /**
127:             * Writes <code>len</code> bytes from the specified byte array 
128:             * starting at offset <code>off</code> to this piped output stream. 
129:             * This method blocks until all the bytes are written to the output
130:             * stream.
131:             *
132:             * @param      b     the data.
133:             * @param      off   the start offset in the data.
134:             * @param      len   the number of bytes to write.
135:             * @exception IOException if the pipe is <a href=#BROKEN> broken</a>,
136:             *          {@link #connect(java.io.PipedInputStream) unconnected},
137:             *		closed, or if an I/O error occurs.
138:             */
139:            public void write(byte b[], int off, int len) throws IOException {
140:                if (sink == null) {
141:                    throw new IOException("Pipe not connected");
142:                } else if (b == null) {
143:                    throw new NullPointerException();
144:                } else if ((off < 0) || (off > b.length) || (len < 0)
145:                        || ((off + len) > b.length) || ((off + len) < 0)) {
146:                    throw new IndexOutOfBoundsException();
147:                } else if (len == 0) {
148:                    return;
149:                }
150:                sink.receive(b, off, len);
151:            }
152:
153:            /**
154:             * Flushes this output stream and forces any buffered output bytes 
155:             * to be written out. 
156:             * This will notify any readers that bytes are waiting in the pipe.
157:             *
158:             * @exception IOException if an I/O error occurs.
159:             */
160:            public synchronized void flush() throws IOException {
161:                if (sink != null) {
162:                    synchronized (sink) {
163:                        sink.notifyAll();
164:                    }
165:                }
166:            }
167:
168:            /**
169:             * Closes this piped output stream and releases any system resources 
170:             * associated with this stream. This stream may no longer be used for 
171:             * writing bytes.
172:             *
173:             * @exception  IOException  if an I/O error occurs.
174:             */
175:            public void close() throws IOException {
176:                if (sink != null) {
177:                    sink.receivedLast();
178:                }
179:            }
180:        }
w__w___w_.___j__a__va2_s._c__o___m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.