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

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 geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » java.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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