View Javadoc

1   package fr.in2p3.jsaga.impl.job.instance.stream;
2   
3   import fr.in2p3.jsaga.adaptor.job.control.interactive.JobIOSetter;
4   import org.ogf.saga.error.NoSuccessException;
5   
6   import java.io.*;
7   
8   /* ***************************************************
9   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
10  * ***             http://cc.in2p3.fr/             ***
11  * ***************************************************
12  * File:   PipedStdout
13  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
14  * Date:   28 avr. 2008
15  * ***************************************************
16  * Description:                                      */
17  /**
18   *
19   */
20  public class PipedStdout extends PipedInputStream implements Runnable {
21      protected JobIOSetter m_ioHandler;
22      protected IOException m_exception;
23      protected boolean m_closed;
24      protected OutputStream m_out;
25  
26      public PipedStdout(JobIOSetter ioHandler) throws NoSuccessException {
27          m_ioHandler = ioHandler;
28          m_exception = null;
29          m_closed = false;
30          try {
31              // pipe must be connected before reading (else will throw exception)
32              m_out = new PipedOutputStream(this);
33          } catch (IOException e) {
34              throw new NoSuccessException(e);
35          }
36          new Thread(this).start();        
37      }
38  
39      public synchronized int read() throws IOException {
40          if (m_exception != null) {
41              throw m_exception;
42          }
43          int c = super.read();
44          if (m_exception != null) {
45              throw m_exception;
46          }
47          return c;
48      }
49  
50      public synchronized int read(byte[] bytes, int off, int len) throws IOException {
51          if (m_exception != null) {
52              throw m_exception;
53          }
54          int readlen = super.read(bytes, off, len);
55          if (m_exception != null) {
56              throw m_exception;
57          }
58          return readlen;
59      }
60  
61      public void close() throws IOException {
62          if (m_exception != null) {
63              throw m_exception;
64          }
65          super.close();
66          while (!m_closed) {
67              if (m_exception != null) {
68                  throw m_exception;
69              }
70              try {
71                  Thread.sleep(10);
72              } catch (InterruptedException e) {
73                  throw new IOException("InterruptedException: "+e.getMessage());
74              }
75          }
76      }
77  
78      public void run() {
79          try {
80              m_ioHandler.setStdout(m_out);
81          } catch (Exception e) {
82              m_exception = new IOException(e.getClass()+": "+e.getMessage());
83          } finally {
84              try {
85                  // pipe must be closed to unlock read attempt
86                  m_out.close();
87              } catch (IOException e) {
88                  m_exception = e;
89              }
90              m_closed = true;
91          }
92      }
93  }