Source Code Cross Referenced for PipedWriter.java in  » Apache-Harmony-Java-SE » java-package » 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 » Apache Harmony Java SE » java package » java.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.