178c74d1bf5895ec8fbbaf2417dee89afbfb1326
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / LockedFileOutputStream.java
1 /**\r
2  * \r
3  */\r
4 package uk.ac.vamsas.client.simpleclient;\r
5 \r
6 import java.io.File;\r
7 import java.io.FileDescriptor;\r
8 import java.io.FileNotFoundException;\r
9 import java.io.FileOutputStream;\r
10 import java.io.IOException;\r
11 import java.io.OutputStream;\r
12 import java.nio.channels.FileChannel;\r
13 \r
14 import org.apache.commons.logging.LogFactory;\r
15 \r
16 /**\r
17  * @author Jim\r
18  *\r
19  */\r
20 public class LockedFileOutputStream extends FileOutputStream {\r
21   private static org.apache.commons.logging.Log log = LogFactory.getLog(LockedFileOutputStream.class);\r
22   //FileOutputStream ostream=null;\r
23   boolean closed=true;\r
24   private void init() {\r
25     FileChannel ch = super.getChannel();\r
26     if (ch!=null) {\r
27       try { closed = !ch.isOpen();\r
28       } catch (Exception e) {\r
29         closed=true;\r
30         log.debug("Invalid LockedOutputStream marked closed.",e);\r
31       }\r
32     }\r
33   }\r
34   /**\r
35    * @param file\r
36    * @throws FileNotFoundException\r
37    */\r
38   public LockedFileOutputStream(File file) throws FileNotFoundException {\r
39     super(file); // super(file);\r
40     init();\r
41   }\r
42 \r
43   /**\r
44    * @param file\r
45    * @param append\r
46    * @throws FileNotFoundException\r
47    */\r
48   public LockedFileOutputStream(File file, boolean append)\r
49       throws FileNotFoundException {\r
50     super(file, append);\r
51     init();\r
52   }\r
53 \r
54   /**\r
55    * @param fdObj\r
56    */\r
57   public LockedFileOutputStream(FileDescriptor fdObj) {\r
58     super(fdObj);\r
59     init();\r
60     if (fdObj.valid())\r
61       closed=false;\r
62   }\r
63 \r
64   /**\r
65    * @param name\r
66    * @throws FileNotFoundException\r
67    */\r
68   public LockedFileOutputStream(String name) throws FileNotFoundException {\r
69     super(name);\r
70     init();\r
71   }\r
72 \r
73   /**\r
74    * @param name\r
75    * @param append\r
76    * @throws FileNotFoundException\r
77    */\r
78   public LockedFileOutputStream(String name, boolean append)\r
79       throws FileNotFoundException {\r
80     super(name, append);\r
81     init();\r
82   }\r
83   /**\r
84    * closes - actually just flushes the stream instead.\r
85    */\r
86   public void close() throws IOException {\r
87     if (!closed) {\r
88       super.flush();\r
89       super.getChannel().force(true);\r
90       log.debug("Marking Lockedoutputstream closed.");\r
91     } else\r
92       throw new IOException("Close on already closed FileOutputStream.");\r
93     closed=true;\r
94   }\r
95 \r
96 \r
97   /**\r
98    * @throws IOException\r
99    * @see java.io.OutputStream#flush()\r
100    */\r
101   public void flush() throws IOException {\r
102     if (!closed)\r
103       super.flush();\r
104     else\r
105       throw new IOException("flush on closed FileOutputStream");\r
106   }\r
107 \r
108   /**\r
109    * @return\r
110    * @see java.io.FileOutputStream#getChannel()\r
111    */\r
112   public FileChannel getChannel() {\r
113     if (!closed)\r
114       return super.getChannel();\r
115     else\r
116       return null;\r
117   }\r
118 \r
119 \r
120 \r
121   /**\r
122    * @param b\r
123    * @param off\r
124    * @param len\r
125    * @throws IOException\r
126    * @see java.io.FileOutputStream#write(byte[], int, int)\r
127    */\r
128   public void write(byte[] b, int off, int len) throws IOException {\r
129     if (!closed)\r
130       super.write(b, off, len);\r
131     else\r
132       throw new IOException("write on Closed FileOutputStream");\r
133   }\r
134 \r
135   /**\r
136    * @param b\r
137    * @throws IOException\r
138    * @see java.io.FileOutputStream#write(byte[])\r
139    */\r
140   public void write(byte[] b) throws IOException {\r
141     if (!closed)\r
142       super.write(b);\r
143     else\r
144       throw new IOException("write on Closed FileOutputStream");\r
145   }\r
146 \r
147   /**\r
148    * @param b\r
149    * @throws IOException\r
150    * @see java.io.FileOutputStream#write(int)\r
151    */\r
152   public void write(int b) throws IOException {\r
153     if (!closed)\r
154       super.write(b);\r
155     else\r
156       throw new IOException("write on Closed FileOutputStream");\r
157   }\r
158   \r
159 }\r