1 package fr.in2p3.jsaga.adaptor.data;
2
3 import fr.in2p3.jsaga.Base;
4 import fr.in2p3.jsaga.adaptor.data.impl.DataEmulatorConnectionAbstract;
5 import fr.in2p3.jsaga.adaptor.schema.data.emulator.File;
6 import org.ogf.saga.error.NoSuccessException;
7
8 import java.io.IOException;
9 import java.io.OutputStream;
10
11
12
13
14
15
16
17
18
19
20
21
22
23 public class EmulatorOutputStream extends OutputStream {
24 private DataEmulatorConnectionAbstract m_server;
25 private File m_file;
26
27 public EmulatorOutputStream(DataEmulatorConnectionAbstract server, File file) {
28 m_server = server;
29 m_file = file;
30 }
31
32 public void write(byte[] bytes, int off, int len) throws IOException {
33 StringBuffer b = new StringBuffer();
34 if (m_file.getContent() != null) {
35 b.append(m_file.getContent());
36 }
37 if (off==0 && len==bytes.length) {
38 b.append(new String(bytes));
39 } else {
40 byte[] array = new byte[len-off];
41 System.arraycopy(bytes, off, array, 0, len);
42 b.append(new String(array));
43 }
44 m_file.setContent(b.toString());
45 try {
46 if(Base.DEBUG) m_server.commit();
47 } catch (NoSuccessException e) {
48 throw new IOException("Failed to commit modification");
49 }
50 }
51
52 public void write(int i) throws IOException {
53 StringBuffer b = new StringBuffer();
54 if (m_file.getContent() != null) {
55 b.append(m_file.getContent());
56 }
57 b.append((char) i);
58 m_file.setContent(b.toString());
59
60
61
62
63
64
65
66 }
67
68 public void close() throws IOException {
69 try {
70 m_server.commit();
71 } catch (NoSuccessException e) {
72 throw new IOException("Failed to commit modification");
73 }
74 }
75 }