View Javadoc

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  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
13  * ***             http://cc.in2p3.fr/             ***
14  * ***************************************************
15  * File:   EmulatorOutputStream
16  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
17  * Date:   22 aout 2007
18  * ***************************************************
19  * Description:                                      */
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          try {
61              if(Base.DEBUG) m_server.commit();
62          } catch (NoSuccessException e) {
63              throw new IOException("Failed to commit modification");
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  }