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