View Javadoc

1   package twcsckernel.projectbase.io;
2   
3   import java.io.IOException;
4   import java.io.OutputStream;
5   import java.io.Serializable;
6   
7   import twcsckernel.projectbase.common.WriterFactory;
8   
9   public class RemoteFileOutputStream extends OutputStream implements Serializable {
10      
11      private static final long serialVersionUID = 1L;
12  
13      private final WriterFactory wf;
14  
15      private final int streamID;
16      private static final IOException streamNotFound=new IOException("Stream not found");
17  
18      public RemoteFileOutputStream(int streamID, WriterFactory wf) {
19          this.streamID = streamID;
20          this.wf = wf;
21      }
22      
23      @Override
24      public void write(int b) throws IOException {
25          try {
26              wf.write(streamID,b);
27          } catch (ItemNotFoundException e) {
28              throw streamNotFound;
29          }
30      }
31      
32      @Override
33      public void write(byte b[]) throws IOException {
34          write(b, 0, b.length);
35      }
36      
37      @Override
38      public void write(byte b[], int off, int len) throws IOException {
39          if (b == null) {
40              throw new NullPointerException();
41          } else if ((off < 0) || (off > b.length) || (len < 0)
42                  || ((off + len) > b.length) || ((off + len) < 0)) {
43              throw new IndexOutOfBoundsException();
44          } else if (len == 0) {
45              return;
46          }
47          byte[] writtenTable=null;
48          if (off==0 && len==b.length)
49              writtenTable=b;
50          else {
51              writtenTable=new byte[len];
52              for (int i=0;i<len;i++)
53                  writtenTable[i]=b[off+i];
54          }
55          try {
56              wf.write(streamID,writtenTable);
57          } catch (ItemNotFoundException e) {
58              throw streamNotFound;
59          }
60  
61      }
62  
63      @Override
64      public void flush() throws IOException {
65          try {
66              wf.flush(streamID);
67          } catch (ItemNotFoundException e) {
68              throw streamNotFound;
69          }
70      }
71  
72      @Override
73      public void close() throws IOException {
74          try {
75              wf.close(streamID);
76          } catch (ItemNotFoundException e) {
77              throw streamNotFound;
78          }
79      }
80  }