Source Code Cross Referenced for SequenceInputStream.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 1994-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.InputStream;
029        import java.util.Enumeration;
030        import java.util.Vector;
031
032        /**
033         * A <code>SequenceInputStream</code> represents
034         * the logical concatenation of other input
035         * streams. It starts out with an ordered
036         * collection of input streams and reads from
037         * the first one until end of file is reached,
038         * whereupon it reads from the second one,
039         * and so on, until end of file is reached
040         * on the last of the contained input streams.
041         *
042         * @author  Author van Hoff
043         * @version 1.39, 05/05/07
044         * @since   JDK1.0
045         */
046        public class SequenceInputStream extends InputStream {
047            Enumeration e;
048            InputStream in;
049
050            /**
051             * Initializes a newly created <code>SequenceInputStream</code>
052             * by remembering the argument, which must
053             * be an <code>Enumeration</code>  that produces
054             * objects whose run-time type is <code>InputStream</code>.
055             * The input streams that are  produced by
056             * the enumeration will be read, in order,
057             * to provide the bytes to be read  from this
058             * <code>SequenceInputStream</code>. After
059             * each input stream from the enumeration
060             * is exhausted, it is closed by calling its
061             * <code>close</code> method.
062             *
063             * @param   e   an enumeration of input streams.
064             * @see     java.util.Enumeration
065             */
066            public SequenceInputStream(Enumeration<? extends InputStream> e) {
067                this .e = e;
068                try {
069                    nextStream();
070                } catch (IOException ex) {
071                    // This should never happen
072                    throw new Error("panic");
073                }
074            }
075
076            /**
077             * Initializes a newly
078             * created <code>SequenceInputStream</code>
079             * by remembering the two arguments, which
080             * will be read in order, first <code>s1</code>
081             * and then <code>s2</code>, to provide the
082             * bytes to be read from this <code>SequenceInputStream</code>.
083             *
084             * @param   s1   the first input stream to read.
085             * @param   s2   the second input stream to read.
086             */
087            public SequenceInputStream(InputStream s1, InputStream s2) {
088                Vector v = new Vector(2);
089
090                v.addElement(s1);
091                v.addElement(s2);
092                e = v.elements();
093                try {
094                    nextStream();
095                } catch (IOException ex) {
096                    // This should never happen
097                    throw new Error("panic");
098                }
099            }
100
101            /**
102             *  Continues reading in the next stream if an EOF is reached.
103             */
104            final void nextStream() throws IOException {
105                if (in != null) {
106                    in.close();
107                }
108
109                if (e.hasMoreElements()) {
110                    in = (InputStream) e.nextElement();
111                    if (in == null)
112                        throw new NullPointerException();
113                } else
114                    in = null;
115
116            }
117
118            /**
119             * Returns an estimate of the number of bytes that can be read (or
120             * skipped over) from the current underlying input stream without
121             * blocking by the next invocation of a method for the current
122             * underlying input stream. The next invocation might be
123             * the same thread or another thread.  A single read or skip of this
124             * many bytes will not block, but may read or skip fewer bytes.
125             * <p>
126             * This method simply calls {@code available} of the current underlying
127             * input stream and returns the result.
128             *
129             * @return an estimate of the number of bytes that can be read (or
130             *         skipped over) from the current underlying input stream
131             *         without blocking or {@code 0} if this input stream
132             *         has been closed by invoking its {@link #close()} method
133             * @exception  IOException  if an I/O error occurs.
134             *
135             * @since   JDK1.1
136             */
137            public int available() throws IOException {
138                if (in == null) {
139                    return 0; // no way to signal EOF from available()
140                }
141                return in.available();
142            }
143
144            /**
145             * Reads the next byte of data from this input stream. The byte is
146             * returned as an <code>int</code> in the range <code>0</code> to
147             * <code>255</code>. If no byte is available because the end of the
148             * stream has been reached, the value <code>-1</code> is returned.
149             * This method blocks until input data is available, the end of the
150             * stream is detected, or an exception is thrown.
151             * <p>
152             * This method
153             * tries to read one character from the current substream. If it
154             * reaches the end of the stream, it calls the <code>close</code>
155             * method of the current substream and begins reading from the next
156             * substream.
157             *
158             * @return     the next byte of data, or <code>-1</code> if the end of the
159             *             stream is reached.
160             * @exception  IOException  if an I/O error occurs.
161             */
162            public int read() throws IOException {
163                if (in == null) {
164                    return -1;
165                }
166                int c = in.read();
167                if (c == -1) {
168                    nextStream();
169                    return read();
170                }
171                return c;
172            }
173
174            /**
175             * Reads up to <code>len</code> bytes of data from this input stream
176             * into an array of bytes.  If <code>len</code> is not zero, the method
177             * blocks until at least 1 byte of input is available; otherwise, no
178             * bytes are read and <code>0</code> is returned.
179             * <p>
180             * The <code>read</code> method of <code>SequenceInputStream</code>
181             * tries to read the data from the current substream. If it fails to
182             * read any characters because the substream has reached the end of
183             * the stream, it calls the <code>close</code> method of the current
184             * substream and begins reading from the next substream.
185             *
186             * @param      b     the buffer into which the data is read.
187             * @param      off   the start offset in array <code>b</code>
188             *                   at which the data is written.
189             * @param      len   the maximum number of bytes read.
190             * @return     int   the number of bytes read.
191             * @exception  NullPointerException If <code>b</code> is <code>null</code>.
192             * @exception  IndexOutOfBoundsException If <code>off</code> is negative, 
193             * <code>len</code> is negative, or <code>len</code> is greater than 
194             * <code>b.length - off</code>
195             * @exception  IOException  if an I/O error occurs.
196             */
197            public int read(byte b[], int off, int len) throws IOException {
198                if (in == null) {
199                    return -1;
200                } else if (b == null) {
201                    throw new NullPointerException();
202                } else if (off < 0 || len < 0 || len > b.length - off) {
203                    throw new IndexOutOfBoundsException();
204                } else if (len == 0) {
205                    return 0;
206                }
207
208                int n = in.read(b, off, len);
209                if (n <= 0) {
210                    nextStream();
211                    return read(b, off, len);
212                }
213                return n;
214            }
215
216            /**
217             * Closes this input stream and releases any system resources
218             * associated with the stream.
219             * A closed <code>SequenceInputStream</code>
220             * cannot  perform input operations and cannot
221             * be reopened.
222             * <p>
223             * If this stream was created
224             * from an enumeration, all remaining elements
225             * are requested from the enumeration and closed
226             * before the <code>close</code> method returns.
227             *
228             * @exception  IOException  if an I/O error occurs.
229             */
230            public void close() throws IOException {
231                do {
232                    nextStream();
233                } while (in != null);
234            }
235        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.