View Javadoc

1   package fr.in2p3.jsaga.impl.job.instance.stream;
2   
3   import org.ogf.saga.error.NoSuccessException;
4   
5   import java.io.*;
6   
7   /* ***************************************************
8   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
9   * ***             http://cc.in2p3.fr/             ***
10  * ***************************************************
11  * File:   GetterBufferedInputStream
12  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
13  * Date:   23 mai 2008
14  * ***************************************************
15  * Description:                                      */
16  /**
17   *
18   */
19  public class GetterBufferedInputStream extends InputStream implements Runnable {
20      private InputStream m_stream;
21      private ByteArrayOutputStream m_buffer;
22      private ByteArrayInputStream m_bufferedStream;
23      private IOException m_exception;
24      private boolean m_closed;
25  
26      public GetterBufferedInputStream(InputStream stdout) throws NoSuccessException {
27          m_stream = stdout;
28          m_buffer = new ByteArrayOutputStream();
29          m_bufferedStream = null;
30          m_exception = null;
31          m_closed = false;
32          new Thread(this).start();
33      }
34  
35      public synchronized int read() throws IOException {
36          if (m_exception != null) {
37              throw m_exception;
38          }
39          int c = this.stream().read();
40          if (m_exception != null) {
41              throw m_exception;
42          }
43          return c;
44      }
45  
46      public synchronized int read(byte[] bytes, int off, int len) throws IOException {
47          if (m_exception != null) {
48              throw m_exception;
49          }
50          int readlen = this.stream().read(bytes, off, len);
51          if (m_exception != null) {
52              throw m_exception;
53          }
54          return readlen;
55      }
56  
57      public void close() throws IOException {
58          if (m_exception != null) {
59              throw m_exception;
60          }
61          this.stream().close();
62          while (!m_closed) {
63              if (m_exception != null) {
64                  throw m_exception;
65              }
66              try {
67                  Thread.sleep(10);
68              } catch (InterruptedException e) {
69                  throw new IOException("InterruptedException: "+e.getMessage());
70              }
71          }
72      }
73  
74      public boolean isClosed() {
75          return m_closed;
76      }
77  
78      public void run() {
79          try {
80              byte[] bytes = new byte[1024];
81              for (int len; (len=m_stream.read(bytes))>-1; ) {
82                  m_buffer.write(bytes, 0, len);
83              }
84          } catch (Exception e) {
85              m_exception = new IOException(e.getClass()+": "+e.getMessage());
86          } finally {
87              try {
88                  // pipe must be closed to unlock read attempt
89                  m_stream.close();
90              } catch (IOException e) {
91                  m_exception = e;
92              }
93              m_closed = true;
94          }
95      }
96  
97      private InputStream stream() throws IOException {
98          if (m_bufferedStream == null) {
99              m_buffer.close();
100             m_bufferedStream = new ByteArrayInputStream(m_buffer.toByteArray());
101         }
102         return m_bufferedStream;
103     }
104 }