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

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 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        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.