View Javadoc

1   package fr.in2p3.jsaga.impl.file.stream;
2   
3   import fr.in2p3.jsaga.adaptor.data.read.FileReaderGetter;
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:   PipedInputStreamImpl
13  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
14  * Date:   21 mars 2008
15  * ***************************************************
16  * Description:                                      */
17  /**
18   *
19   */
20  public class PipedInputStreamImpl extends PipedInputStream implements Runnable {
21      private FileReaderGetter m_adaptor;
22      private String m_absolutePath;
23      private String m_additionalArgs;
24      private IOException m_exception;
25      private OutputStream m_out;
26      private Thread thread = new Thread(this);
27  
28      public PipedInputStreamImpl(FileReaderGetter adaptor, String absolutePath, String additionalArgs) throws NoSuccessException {
29          m_adaptor = adaptor;
30          m_absolutePath = absolutePath;
31          m_additionalArgs = additionalArgs;
32          m_exception = null;
33          try {
34              // pipe must be connected before reading (else will throw exception)
35              m_out = new PipedOutputStream(this);
36          } catch (IOException e) {
37              throw new NoSuccessException(e);
38          }
39          thread.start();
40      }
41  
42      public synchronized int read() throws IOException {
43          if (m_exception != null) {
44              throw m_exception;
45          }
46          int c = super.read();
47          if (m_exception != null) {
48              throw m_exception;
49          }
50          return c;
51      }
52  
53      public synchronized int read(byte[] bytes, int off, int len) throws IOException {
54          if (m_exception != null) {
55              throw m_exception;
56          }
57          int readlen = super.read(bytes, off, len);
58          if (m_exception != null) {
59              throw m_exception;
60          }
61          return readlen;
62      }
63  
64      public void close() throws IOException {
65          super.close();
66          try {
67              thread.join();
68          } catch (InterruptedException e) {
69              throw new IOException("InterruptedException: " + e.getMessage());
70          }
71          if (m_exception != null) {
72              throw m_exception;
73          }
74      }
75  
76      public void run() {
77          try {
78              m_adaptor.getToStream(
79                      m_absolutePath,
80                      m_additionalArgs,
81                      m_out);
82          } catch (Throwable e) {
83              m_exception = new IOException(e.getClass()+": "+e.getMessage());
84              m_exception.initCause(e);
85          } finally {
86              try {
87                  // pipe must be closed to unlock read attempt
88                  m_out.close();
89              } catch (IOException e) {
90                  m_exception = e;
91              }
92          }
93      }
94  }