View Javadoc

1   package fr.in2p3.jsaga.impl.file.stream;
2   
3   import fr.in2p3.jsaga.adaptor.data.write.FileWriterPutter;
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:   PipedOutputStreamImpl
13   * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
14   * Date:   21 mars 2008
15   * ***************************************************
16   * Description:                                      */
17  /**
18   *
19   */
20  public class PipedOutputStreamImpl extends PipedOutputStream implements Runnable {
21  
22      private FileWriterPutter m_adaptor;
23      private String m_absolutePath;
24      private String m_additionalArgs;
25      private boolean m_append;
26      private IOException m_exception;
27      private InputStream m_in;
28      private Thread thread = new Thread(this);
29  
30      public PipedOutputStreamImpl(FileWriterPutter adaptor, String absolutePath, String additionalArgs, boolean append) throws NoSuccessException {
31          m_adaptor = adaptor;
32          m_absolutePath = absolutePath;
33          m_additionalArgs = additionalArgs;
34          m_append = append;
35          m_exception = null;
36  
37          try {
38              // pipe must be connected before writing (else will hang on 2nd test case)
39              if (adaptor.getBufferSize() > 0) {
40                  m_in = new PipedInputStream(this, adaptor.getBufferSize());
41              } else {
42                  m_in = new PipedInputStream(this);
43              }
44          } catch (IOException e) {
45              throw new NoSuccessException(e);
46          }
47          thread.start();
48      }
49  
50      public void write(int b) throws IOException {
51          if (m_exception != null) {
52              throw m_exception;
53          }
54          super.write(b);
55      }
56  
57      public void write(byte[] bytes, int off, int len) throws IOException {
58          if (m_exception != null) {
59              throw m_exception;
60          }
61          super.write(bytes, off, len);
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.putFromStream(
79                      m_absolutePath,
80                      m_append,
81                      m_additionalArgs,
82                      m_in);
83          } catch (Throwable e) {
84              m_exception = new IOException(e.getClass() + ": " + e.getMessage());
85              m_exception.initCause(e);
86          } finally {
87              try {
88                  m_in.close();
89              } catch (IOException e) {
90                  m_exception = e;
91              }
92          }
93      }
94  }