5eb1ff3fbe812d49ee69100c6701159c7dd4af62
[vamsas.git] / src / uk / ac / vamsas / client / AppDataOutputStream.java
1 /**
2  * 
3  */
4 package uk.ac.vamsas.client;
5
6 import java.io.DataOutputStream;
7 import java.io.IOException;
8 import java.io.OutputStream;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 /**
14  * @author jimp
15  *
16  */
17 public class AppDataOutputStream extends DataOutputStream {
18   private Log log = LogFactory.getLog(AppDataOutputStream.class);
19   private boolean isOpen=true;
20   /**
21    * @param out
22    */
23   public AppDataOutputStream(OutputStream out) {
24     super(out);
25     isOpen=true;
26   }
27   /* (non-Javadoc)
28    * @see java.io.DataOutputStream#flush()
29    */
30   public void flush() throws IOException {
31     if (isOpen)
32       super.flush();
33     else
34       log.warn("flush() called on closed AppDataOutputStream");
35   }
36   /* (non-Javadoc)
37    * @see java.io.DataOutputStream#write(byte[], int, int)
38    */
39   public synchronized void write(byte[] b, int off, int len) throws IOException {
40     if (isOpen) {
41       super.write(b, off, len);
42     } else {
43       log.debug("write(b,off,len) called on closed AppDataOutputStream");
44       throw new IOException("Attempt to write to closed AppDataOutputStream");
45     }
46   }
47   /* (non-Javadoc)
48    * @see java.io.DataOutputStream#write(int)
49    */
50   public synchronized void write(int b) throws IOException {
51     if (isOpen) {
52       super.write(b);
53     } else {
54       log.debug("write(b) called on closed AppDataOutputStream");
55       throw new IOException("Attempt to write to closed AppDataOutputStream");
56     }
57   }
58   /**
59    * Sets an internal flag preventing further write operations 
60    * to the AppData output stream and flushes any pending writes.
61    * @see java.io.FilterOutputStream#close()
62    */
63   public void close() throws IOException {
64     isOpen=false;
65     super.flush();
66     log.debug("AppDataOutputStream was closed.");
67   }
68   /* (non-Javadoc)
69    * @see java.io.FilterOutputStream#write(byte[])
70    */
71   public void write(byte[] b) throws IOException {
72     if (isOpen) {
73       super.write(b);
74     } else {
75       log.debug("write(b[]) called on closed AppDataOutputStream");
76       throw new IOException("Attempt to write to closed AppDataOutputStream");
77     }
78   }
79   /**
80    * @return true if stream is still Open.
81    */
82   public boolean isOpen() {
83     return isOpen;
84   }
85   
86 }