View Javadoc

1   package fr.in2p3.jsaga.impl.file.stream;
2   
3   import fr.in2p3.jsaga.adaptor.data.DataAdaptor;
4   import fr.in2p3.jsaga.adaptor.data.read.DataReaderAdaptor;
5   import fr.in2p3.jsaga.adaptor.data.write.FileWriterPutter;
6   import fr.in2p3.jsaga.impl.url.URLHelper;
7   import org.ogf.saga.SagaObject;
8   import org.ogf.saga.error.*;
9   import org.ogf.saga.session.Session;
10  import org.ogf.saga.url.URL;
11  
12  import java.io.IOException;
13  
14  /* ***************************************************
15  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
16  * ***             http://cc.in2p3.fr/             ***
17  * ***************************************************
18  * File:   FileOutputStreamPipedImpl
19  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
20  * Date:   21 mars 2008
21  * ***************************************************
22  * Description:                                      */
23  /**
24   *
25   */
26  public class FileOutputStreamPipedImpl extends AbstractAsyncFileOutputStreamImpl {
27      private DataAdaptor m_connection;
28      private PipedOutputStreamImpl m_outStream;
29  
30      /** constructor */
31      FileOutputStreamPipedImpl(Session session, URL url, FileWriterPutter adaptor, boolean disconnectable, boolean append, boolean exclusive) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
32          super(session);
33  
34          // save connection
35          m_connection = (disconnectable ? adaptor : null);
36  
37          // open stream
38          URL fileUrl = URLHelper.toFileURL(url);
39          URL parent = URLHelper.getParentURL(fileUrl);
40          if (exclusive && append) {
41              throw new BadParameterException("Incompatible flags: EXCL and APPEND");
42          } else if (adaptor instanceof DataReaderAdaptor) {
43              DataReaderAdaptor a = (DataReaderAdaptor) adaptor;
44              if (exclusive && a.exists(fileUrl.getPath(), fileUrl.getQuery())) {
45                  // need to check existence explicitely, else exception is never thrown
46                  throw new AlreadyExistsException("File already exists: "+fileUrl);
47              } else {
48                  // need to check existence explicitely, else exception is thrown to late (when writing bytes)
49                  boolean parentExists;
50                  try {
51                      parentExists = a.exists(parent.getPath(), parent.getQuery());
52                  } catch (PermissionDeniedException e) {
53                      parentExists = true;
54                  }
55                  if (!parentExists) {
56                      throw new DoesNotExistException("Parent directory does not exist: "+parent);
57                  }
58              }
59          }
60          m_outStream = new PipedOutputStreamImpl(
61                  adaptor,
62                  fileUrl.getPath(),
63                  fileUrl.getQuery(),
64                  append);
65      }
66  
67      /** clone */
68      public SagaObject clone() throws CloneNotSupportedException {
69          FileOutputStreamPipedImpl clone = (FileOutputStreamPipedImpl) super.clone();
70          clone.m_connection = m_connection;
71          clone.m_outStream = m_outStream;
72          return clone;
73      }
74  
75  
76      public void close() throws IOException {
77          // close stream
78          m_outStream.close();
79  
80          // close connection
81          if (m_connection != null) {
82              try {
83                  m_connection.disconnect();
84              } catch (NoSuccessException e) {
85                  throw new IOException(e.getMessage());
86              }
87          }
88      }
89  
90      /////////////////////////////////// interface OutputStream ///////////////////////////////////
91  
92      public void write(int b) throws IOException {m_outStream.write(b);}
93      public void write(byte[] b) throws IOException {m_outStream.write(b);}
94      public void write(byte[] b, int off, int len) throws IOException {m_outStream.write(b, off, len);}
95      public void flush() throws IOException {m_outStream.flush();}
96  }