SequenceInputStream.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* SequenceInputStream.java -- Reads multiple input streams in sequence
  2. Copyright (C) 1998, 1999, 2001, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.io;
  32. import java.util.Enumeration;
  33. /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
  34. * "The Java Language Specification", ISBN 0-201-63451-1
  35. * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
  36. * Status: Believed complete and correct.
  37. */
  38. /**
  39. * This class merges a sequence of multiple <code>InputStream</code>'s in
  40. * order to form a single logical stream that can be read by applications
  41. * that expect only one stream.
  42. * <p>
  43. * The streams passed to the constructor method are read in order until
  44. * they return -1 to indicate they are at end of stream. When a stream
  45. * reports end of stream, it is closed, then the next stream is read.
  46. * When the last stream is closed, the next attempt to read from this
  47. * stream will return a -1 to indicate it is at end of stream.
  48. * <p>
  49. * If this stream is closed prior to all subordinate streams being read
  50. * to completion, all subordinate streams are closed.
  51. *
  52. * @author Aaron M. Renn (arenn@urbanophile.com)
  53. * @author Warren Levy (warrenl@cygnus.com)
  54. */
  55. public class SequenceInputStream extends InputStream
  56. {
  57. /** The handle for the current input stream. */
  58. private InputStream in;
  59. /** Secondary input stream; not used if constructed w/ enumeration. */
  60. private InputStream in2;
  61. /**
  62. * The enumeration handle; not used if constructed w/ 2 explicit
  63. * input streams.
  64. */
  65. private Enumeration<? extends InputStream> e;
  66. /**
  67. * This method creates a new <code>SequenceInputStream</code> that obtains
  68. * its list of subordinate <code>InputStream</code>s from the specified
  69. * <code>Enumeration</code>
  70. *
  71. * @param e An <code>Enumeration</code> that will return a list of
  72. * <code>InputStream</code>s to read in sequence
  73. */
  74. public SequenceInputStream(Enumeration<? extends InputStream> e)
  75. {
  76. this.e = e;
  77. in = e.nextElement();
  78. in2 = null;
  79. }
  80. /**
  81. * This method creates a new <code>SequenceInputStream</code> that will read
  82. * the two specified subordinate <code>InputStream</code>s in sequence.
  83. *
  84. * @param s1 The first <code>InputStream</code> to read
  85. * @param s2 The second <code>InputStream</code> to read
  86. */
  87. public SequenceInputStream(InputStream s1, InputStream s2)
  88. {
  89. in = s1;
  90. in2 = s2;
  91. }
  92. /**
  93. * This method returns the number of bytes than can be read from the
  94. * currently being read subordinate stream before that stream could
  95. * block. Note that it is possible more bytes than this can actually
  96. * be read without the stream blocking. If a 0 is returned, then the
  97. * stream could block on the very next read.
  98. *
  99. * @return The number of bytes that can be read before blocking could occur
  100. *
  101. * @exception IOException If an error occurs
  102. */
  103. public int available() throws IOException
  104. {
  105. if (in == null)
  106. return 0;
  107. return in.available();
  108. }
  109. /**
  110. * Closes this stream. This will cause any remaining unclosed subordinate
  111. * <code>InputStream</code>'s to be closed as well. Subsequent attempts to
  112. * read from this stream may cause an exception.
  113. *
  114. * @exception IOException If an error occurs
  115. */
  116. public void close() throws IOException
  117. {
  118. while (in != null)
  119. {
  120. in.close();
  121. in = getNextStream ();
  122. }
  123. }
  124. /**
  125. * This method reads an unsigned byte from the input stream and returns it
  126. * as an int in the range of 0-255. This method also will return -1 if
  127. * the end of the stream has been reached. This will only happen when
  128. * all of the subordinate streams have been read.
  129. * <p>
  130. * This method will block until the byte can be read.
  131. *
  132. * @return The byte read, or -1 if end of stream
  133. *
  134. * @exception IOException If an error occurs
  135. */
  136. public int read() throws IOException
  137. {
  138. int ch = -1;
  139. while (in != null && (ch = in.read()) < 0)
  140. {
  141. in.close();
  142. in = getNextStream();
  143. }
  144. return ch;
  145. }
  146. /**
  147. * This method reads bytes from a stream and stores them into a caller
  148. * supplied buffer. It starts storing the data at index <code>offset</code>
  149. * into the buffer and attempts to read <code>len</code> bytes. This method
  150. * can return before reading the number of bytes requested. The actual number
  151. * of bytes read is returned as an int. A -1 is returend to indicate the
  152. * end of the stream. This will only happen when all of the subordinate
  153. * streams have been read.
  154. * <p>
  155. * This method will block until at least one byte can be read.
  156. *
  157. * @param b The array into which bytes read should be stored
  158. * @param off The offset into the array to start storing bytes
  159. * @param len The requested number of bytes to read
  160. *
  161. * @return The actual number of bytes read, or -1 if end of stream
  162. *
  163. * @exception IOException If an error occurs
  164. */
  165. public int read(byte[] b, int off, int len) throws IOException
  166. {
  167. int ch = -1;
  168. // The validity of the parameters will be checked by in.read so
  169. // don't bother doing it here.
  170. while (in != null && (ch = in.read(b, off, len)) < 0)
  171. {
  172. in.close();
  173. in = getNextStream();
  174. }
  175. return ch;
  176. }
  177. /**
  178. * This private method is used to get the next <code>InputStream</code> to
  179. * read from. Returns null when no more streams are available.
  180. */
  181. private InputStream getNextStream()
  182. {
  183. InputStream nextIn = null;
  184. if (e != null)
  185. {
  186. if (e.hasMoreElements())
  187. nextIn = e.nextElement();
  188. }
  189. else if (in2 != null)
  190. {
  191. nextIn = in2;
  192. in2 = null;
  193. }
  194. return nextIn;
  195. }
  196. }