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
16
17
18
19
20
21
22
23
24
25
26 public class FileOutputStreamPipedImpl extends AbstractAsyncFileOutputStreamImpl {
27 private DataAdaptor m_connection;
28 private PipedOutputStreamImpl m_outStream;
29
30
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
35 m_connection = (disconnectable ? adaptor : null);
36
37
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
46 throw new AlreadyExistsException("File already exists: "+fileUrl);
47 } else {
48
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
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
78 m_outStream.close();
79
80
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
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 }