applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / client / AppDataOutputStream.java
1 /*\r
2  * This file is part of the Vamsas Client version 0.1. \r
3  * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
4  *  Andrew Waterhouse and Dominik Lindner.\r
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \r
9  * The Vamsas Client is free software: you can redistribute it and/or modify\r
10  * it under the terms of the GNU Lesser General Public License as published by\r
11  * the Free Software Foundation, either version 3 of the License, or\r
12  * (at your option) any later version.\r
13  *  \r
14  * The Vamsas Client is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU Lesser General Public License for more details.\r
18  * \r
19  * You should have received a copy of the GNU Lesser General Public License\r
20  * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
21  */\r
22 package uk.ac.vamsas.client;\r
23 \r
24 import java.io.DataOutputStream;\r
25 import java.io.IOException;\r
26 import java.io.OutputStream;\r
27 \r
28 import org.apache.commons.logging.Log;\r
29 import org.apache.commons.logging.LogFactory;\r
30 \r
31 /**\r
32  * @author jimp\r
33  * \r
34  */\r
35 public class AppDataOutputStream extends DataOutputStream {\r
36   private Log log = LogFactory.getLog(AppDataOutputStream.class);\r
37 \r
38   private boolean isOpen = true;\r
39 \r
40   /**\r
41    * @param out\r
42    */\r
43   public AppDataOutputStream(OutputStream out) {\r
44     super(out);\r
45     isOpen = true;\r
46   }\r
47 \r
48   /*\r
49    * (non-Javadoc)\r
50    * \r
51    * @see java.io.DataOutputStream#flush()\r
52    */\r
53   public void flush() throws IOException {\r
54     if (isOpen)\r
55       super.flush();\r
56     else\r
57       log.warn("flush() called on closed AppDataOutputStream");\r
58   }\r
59 \r
60   /*\r
61    * (non-Javadoc)\r
62    * \r
63    * @see java.io.DataOutputStream#write(byte[], int, int)\r
64    */\r
65   public synchronized void write(byte[] b, int off, int len) throws IOException {\r
66     if (isOpen) {\r
67       super.write(b, off, len);\r
68     } else {\r
69       log.debug("write(b,off,len) called on closed AppDataOutputStream");\r
70       throw new IOException("Attempt to write to closed AppDataOutputStream");\r
71     }\r
72   }\r
73 \r
74   /*\r
75    * (non-Javadoc)\r
76    * \r
77    * @see java.io.DataOutputStream#write(int)\r
78    */\r
79   public synchronized void write(int b) throws IOException {\r
80     if (isOpen) {\r
81       super.write(b);\r
82     } else {\r
83       log.debug("write(b) called on closed AppDataOutputStream");\r
84       throw new IOException("Attempt to write to closed AppDataOutputStream");\r
85     }\r
86   }\r
87 \r
88   /**\r
89    * Sets an internal flag preventing further write operations to the AppData\r
90    * output stream and flushes any pending writes.\r
91    * \r
92    * @see java.io.FilterOutputStream#close()\r
93    */\r
94   public void close() throws IOException {\r
95     isOpen = false;\r
96     super.flush();\r
97     log.debug("AppDataOutputStream was closed.");\r
98   }\r
99 \r
100   /*\r
101    * (non-Javadoc)\r
102    * \r
103    * @see java.io.FilterOutputStream#write(byte[])\r
104    */\r
105   public void write(byte[] b) throws IOException {\r
106     if (isOpen) {\r
107       super.write(b);\r
108     } else {\r
109       log.debug("write(b[]) called on closed AppDataOutputStream");\r
110       throw new IOException("Attempt to write to closed AppDataOutputStream");\r
111     }\r
112   }\r
113 \r
114   /**\r
115    * @return true if stream is still Open.\r
116    */\r
117   public boolean isOpen() {\r
118     return isOpen;\r
119   }\r
120 \r
121 }\r