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.ParentDoesNotExist;
5   import fr.in2p3.jsaga.adaptor.data.write.FileWriterStreamFactory;
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  import java.io.OutputStream;
14  
15  /* ***************************************************
16  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
17  * ***             http://cc.in2p3.fr/             ***
18  * ***************************************************
19  * File:   FileOutputStreamImpl
20  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
21  * Date:   20 mars 2008
22  * ***************************************************
23  * Description:                                      */
24  /**
25   *
26   */
27  public class FileOutputStreamImpl extends AbstractAsyncFileOutputStreamImpl {
28      private DataAdaptor m_connection;
29      private OutputStream m_outStream;
30  
31      /** constructor */
32      FileOutputStreamImpl(Session session, URL url, FileWriterStreamFactory adaptor, boolean disconnectable, boolean append, boolean exclusive) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
33          super(session);
34  
35          // save connection
36          m_connection = (disconnectable ? adaptor : null);
37  
38          // open stream
39          URL fileUrl = URLHelper.toFileURL(url);
40          URL parent = URLHelper.getParentURL(fileUrl);
41          String fileName = URLHelper.getName(fileUrl);
42          if (exclusive && append) {
43              throw new BadParameterException("Incompatible flags: EXCL and APPEND");
44          }
45          try {
46              m_outStream = adaptor.getOutputStream(
47                      parent.getPath(),
48                      fileName,
49                      exclusive,
50                      append,
51                      fileUrl.getQuery());
52          } catch(ParentDoesNotExist e) {
53              throw new DoesNotExistException("Parent directory does not exist: "+parent, e.getCause());
54          } catch(AlreadyExistsException e) {
55              throw new AlreadyExistsException("File already exists: "+ fileUrl, e.getCause());
56          }
57          if (m_outStream == null) {
58              throw new NoSuccessException("[ADAPTOR ERROR] Method getOutputStream() must not return 'null'", this);
59          }
60      }
61  
62      /** clone */
63      public SagaObject clone() throws CloneNotSupportedException {
64          FileOutputStreamImpl clone = (FileOutputStreamImpl) super.clone();
65          clone.m_connection = m_connection;
66          clone.m_outStream = m_outStream;
67          return clone;
68      }
69  
70  
71      public void close() throws IOException {
72          // close stream
73          try { 
74             m_outStream.close();
75          } finally {
76             // close connection
77             if (m_connection != null) {
78                 try {
79                     m_connection.disconnect();
80                 } catch (NoSuccessException e) {
81                     throw new IOException(e.getMessage());
82                 }
83             }
84          }
85      }
86  
87      /////////////////////////////////// interface OutputStream ///////////////////////////////////
88  
89      public void write(int b) throws IOException {m_outStream.write(b);}
90      public void write(byte[] b) throws IOException {m_outStream.write(b);}
91      public void write(byte[] b, int off, int len) throws IOException {m_outStream.write(b, off, len);}
92      public void flush() throws IOException {m_outStream.flush();}
93  }