View Javadoc

1   package fr.in2p3.jsaga.impl.job.instance.stream;
2   
3   import java.io.*;
4   
5   /* ***************************************************
6   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
7   * ***             http://cc.in2p3.fr/             ***
8   * ***************************************************
9   * File:   InputStreamContainer
10  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
11  * Date:   23 mai 2008
12  * ***************************************************
13  * Description:                                      */
14  /**
15   *
16   */
17  public class InputStreamContainer extends InputStream {
18      private ByteArrayOutputStream m_out;
19  
20      public InputStreamContainer() {
21          m_out = new ByteArrayOutputStream();
22      }
23  
24      public OutputStream getOutputStream() {
25          return m_out;
26      }
27  
28      public void finishWriting() throws IOException {
29          m_out.close();
30          m_buffer = new ByteArrayInputStream(m_out.toByteArray());
31      }
32  
33      /////////////////////////////////// interface InputStream ///////////////////////////////////
34  
35      public int read() throws IOException {return this.stream().read();}
36      public int read(byte[] b) throws IOException {return this.stream().read(b);}
37      public int read(byte[] b, int off, int len) throws IOException {return this.stream().read(b, off, len);}
38      public long skip(long n) throws IOException {return this.stream().skip(n);}
39      public void close() throws IOException {this.stream().close();}
40  
41      /////////////////////////////////////// private method ///////////////////////////////////////
42  
43      private ByteArrayInputStream m_buffer;
44      private InputStream stream() throws IOException {
45          if (m_buffer != null) {
46              return m_buffer;
47          } else {
48              return new ByteArrayInputStream(new byte[]{});
49          }
50      }
51  }