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.FileReaderStreamFactory;
5 import fr.in2p3.jsaga.impl.url.URLHelper;
6 import org.ogf.saga.SagaObject;
7 import org.ogf.saga.error.*;
8 import org.ogf.saga.session.Session;
9 import org.ogf.saga.url.URL;
10
11 import java.io.IOException;
12 import java.io.InputStream;
13
14
15
16
17
18
19
20
21
22
23
24
25
26 public class FileInputStreamImpl extends AbstractAsyncFileInputStreamImpl {
27 private DataAdaptor m_connection;
28 private InputStream m_inStream;
29
30
31 FileInputStreamImpl(Session session, URL url, FileReaderStreamFactory adaptor, boolean disconnectable) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, DoesNotExistException, TimeoutException, NoSuccessException {
32 super(session);
33
34
35 m_connection = (disconnectable ? adaptor : null);
36
37
38 URL fileUrl = URLHelper.toFileURL(url);
39 try {
40 m_inStream = adaptor.getInputStream(
41 fileUrl.getPath(),
42 fileUrl.getQuery());
43 } catch(DoesNotExistException e) {
44 throw new DoesNotExistException("File does not exist: "+fileUrl, e.getCause());
45 }
46 if (m_inStream == null) {
47 throw new NoSuccessException("[ADAPTOR ERROR] Method getInputStream() must not return 'null'", this);
48 }
49 }
50
51
52 public SagaObject clone() throws CloneNotSupportedException {
53 FileInputStreamImpl clone = (FileInputStreamImpl) super.clone();
54 clone.m_connection = m_connection;
55 clone.m_inStream = m_inStream;
56 return clone;
57 }
58
59 public void close() throws IOException {
60
61 m_inStream.close();
62
63
64 if (m_connection != null) {
65 try {
66 m_connection.disconnect();
67 } catch (NoSuccessException e) {
68 throw new IOException(e.getMessage());
69 }
70 }
71 }
72
73
74
75 public int read() throws IOException {return m_inStream.read();}
76 public int read(byte[] b) throws IOException {return m_inStream.read(b);}
77 public int read(byte[] b, int off, int len) throws IOException {return m_inStream.read(b, off, len);}
78 public long skip(long n) throws IOException {return m_inStream.skip(n);}
79 public int available() throws IOException {return m_inStream.available();}
80 public synchronized void mark(int readlimit) {m_inStream.mark(readlimit);}
81 public synchronized void reset() throws IOException {m_inStream.reset();}
82 public boolean markSupported() {return m_inStream.markSupported();}
83 }