1 package fr.in2p3.jsaga.adaptor.data;
2
3 import fr.in2p3.jsaga.adaptor.Adaptor;
4 import org.ogf.saga.buffer.Buffer;
5 import org.ogf.saga.buffer.BufferFactory;
6 import org.ogf.saga.file.File;
7
8 import java.io.IOException;
9 import java.io.InputStream;
10
11
12
13
14
15
16
17
18
19
20
21
22
23 public class SagaInputStream extends InputStream {
24 private File m_file;
25
26
27 private String m_token;
28 private String m_srmPath;
29 private StreamCallback m_callback;
30
31 public SagaInputStream(File file, String token, String srmPath, StreamCallback callback) {
32 m_file = file;
33
34
35 m_token = token;
36 m_srmPath = srmPath;
37 m_callback = callback;
38 }
39
40 public int read() throws IOException {
41 try {
42 Buffer buffer = BufferFactory.createBuffer(Adaptor.JSAGA_FACTORY, 1);
43 if (m_file.read(buffer, 1) == 1) {
44 byte b = buffer.getData()[0];
45 return (int) b & 0x000000FF;
46 } else {
47 return -1;
48 }
49 } catch (Exception e) {
50 throw new IOException(e.getMessage());
51 }
52 }
53
54 public int read(byte[] bytes, int off, int len) throws IOException {
55 try {
56 Buffer buffer = BufferFactory.createBuffer(Adaptor.JSAGA_FACTORY, bytes);
57 return m_file.read(buffer, len);
58 } catch (Exception e) {
59 throw new IOException(e.getMessage());
60 }
61 }
62
63 public void close() throws IOException {
64 try {
65
66 m_file.close();
67
68
69 if (m_callback != null) {
70 m_callback.freeInputStream(m_token, m_srmPath);
71 m_callback = null;
72 }
73 } catch (Exception e) {
74 throw new IOException(e.getMessage());
75 }
76 }
77 }