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
17
18
19
20
21
22
23
24
25
26
27 public class FileOutputStreamImpl extends AbstractAsyncFileOutputStreamImpl {
28 private DataAdaptor m_connection;
29 private OutputStream m_outStream;
30
31
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
36 m_connection = (disconnectable ? adaptor : null);
37
38
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
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
73 try {
74 m_outStream.close();
75 } finally {
76
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
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 }