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:   OutputStreamContainer
10  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
11  * Date:   14 mai 2008
12  * ***************************************************
13  * Description:                                      */
14  /**
15   *
16   */
17  public class OutputStreamContainer extends OutputStream {
18      private boolean m_isClosed;
19  
20      public OutputStreamContainer() {
21          m_isClosed = false;
22      }
23  
24      public boolean isClosed() {
25          return m_isClosed;
26      }
27  
28      public InputStream getInputStream() throws InterruptedException {
29          while(!m_isClosed) {
30              Thread.currentThread().sleep(100);
31          }
32          if (m_buffer != null) {
33              byte[] buffer = m_buffer.toByteArray();
34              m_buffer.reset();
35              return new ByteArrayInputStream(buffer);
36          } else {
37              return new ByteArrayInputStream(new byte[]{});
38          }
39      }
40  
41      /////////////////////////////////// interface OutputStream ///////////////////////////////////
42  
43      public void write(int b) throws IOException {this.stream().write(b);}
44      public void write(byte[] b) throws IOException {this.stream().write(b);}
45      public void write(byte[] b, int off, int len) throws IOException {this.stream().write(b, off, len);}
46      public void flush() throws IOException {this.stream().flush();}
47      public void close() throws IOException {this.stream().close(); m_isClosed=true;}
48  
49      /////////////////////////////////////// private method ///////////////////////////////////////
50  
51      private ByteArrayOutputStream m_buffer;
52      private OutputStream stream() throws IOException {
53          if (m_buffer == null) {
54              m_buffer = new ByteArrayOutputStream();
55          }
56          return m_buffer;
57      }
58  }