refactored org to uk
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / LockedFileOutputStream.java
diff --git a/src/uk/ac/vamsas/client/simpleclient/LockedFileOutputStream.java b/src/uk/ac/vamsas/client/simpleclient/LockedFileOutputStream.java
new file mode 100644 (file)
index 0000000..178c74d
--- /dev/null
@@ -0,0 +1,159 @@
+/**\r
+ * \r
+ */\r
+package uk.ac.vamsas.client.simpleclient;\r
+\r
+import java.io.File;\r
+import java.io.FileDescriptor;\r
+import java.io.FileNotFoundException;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.io.OutputStream;\r
+import java.nio.channels.FileChannel;\r
+\r
+import org.apache.commons.logging.LogFactory;\r
+\r
+/**\r
+ * @author Jim\r
+ *\r
+ */\r
+public class LockedFileOutputStream extends FileOutputStream {\r
+  private static org.apache.commons.logging.Log log = LogFactory.getLog(LockedFileOutputStream.class);\r
+  //FileOutputStream ostream=null;\r
+  boolean closed=true;\r
+  private void init() {\r
+    FileChannel ch = super.getChannel();\r
+    if (ch!=null) {\r
+      try { closed = !ch.isOpen();\r
+      } catch (Exception e) {\r
+        closed=true;\r
+        log.debug("Invalid LockedOutputStream marked closed.",e);\r
+      }\r
+    }\r
+  }\r
+  /**\r
+   * @param file\r
+   * @throws FileNotFoundException\r
+   */\r
+  public LockedFileOutputStream(File file) throws FileNotFoundException {\r
+    super(file); // super(file);\r
+    init();\r
+  }\r
+\r
+  /**\r
+   * @param file\r
+   * @param append\r
+   * @throws FileNotFoundException\r
+   */\r
+  public LockedFileOutputStream(File file, boolean append)\r
+      throws FileNotFoundException {\r
+    super(file, append);\r
+    init();\r
+  }\r
+\r
+  /**\r
+   * @param fdObj\r
+   */\r
+  public LockedFileOutputStream(FileDescriptor fdObj) {\r
+    super(fdObj);\r
+    init();\r
+    if (fdObj.valid())\r
+      closed=false;\r
+  }\r
+\r
+  /**\r
+   * @param name\r
+   * @throws FileNotFoundException\r
+   */\r
+  public LockedFileOutputStream(String name) throws FileNotFoundException {\r
+    super(name);\r
+    init();\r
+  }\r
+\r
+  /**\r
+   * @param name\r
+   * @param append\r
+   * @throws FileNotFoundException\r
+   */\r
+  public LockedFileOutputStream(String name, boolean append)\r
+      throws FileNotFoundException {\r
+    super(name, append);\r
+    init();\r
+  }\r
+  /**\r
+   * closes - actually just flushes the stream instead.\r
+   */\r
+  public void close() throws IOException {\r
+    if (!closed) {\r
+      super.flush();\r
+      super.getChannel().force(true);\r
+      log.debug("Marking Lockedoutputstream closed.");\r
+    } else\r
+      throw new IOException("Close on already closed FileOutputStream.");\r
+    closed=true;\r
+  }\r
+\r
+\r
+  /**\r
+   * @throws IOException\r
+   * @see java.io.OutputStream#flush()\r
+   */\r
+  public void flush() throws IOException {\r
+    if (!closed)\r
+      super.flush();\r
+    else\r
+      throw new IOException("flush on closed FileOutputStream");\r
+  }\r
+\r
+  /**\r
+   * @return\r
+   * @see java.io.FileOutputStream#getChannel()\r
+   */\r
+  public FileChannel getChannel() {\r
+    if (!closed)\r
+      return super.getChannel();\r
+    else\r
+      return null;\r
+  }\r
+\r
+\r
+\r
+  /**\r
+   * @param b\r
+   * @param off\r
+   * @param len\r
+   * @throws IOException\r
+   * @see java.io.FileOutputStream#write(byte[], int, int)\r
+   */\r
+  public void write(byte[] b, int off, int len) throws IOException {\r
+    if (!closed)\r
+      super.write(b, off, len);\r
+    else\r
+      throw new IOException("write on Closed FileOutputStream");\r
+  }\r
+\r
+  /**\r
+   * @param b\r
+   * @throws IOException\r
+   * @see java.io.FileOutputStream#write(byte[])\r
+   */\r
+  public void write(byte[] b) throws IOException {\r
+    if (!closed)\r
+      super.write(b);\r
+    else\r
+      throw new IOException("write on Closed FileOutputStream");\r
+  }\r
+\r
+  /**\r
+   * @param b\r
+   * @throws IOException\r
+   * @see java.io.FileOutputStream#write(int)\r
+   */\r
+  public void write(int b) throws IOException {\r
+    if (!closed)\r
+      super.write(b);\r
+    else\r
+      throw new IOException("write on Closed FileOutputStream");\r
+  }\r
+  \r
+}\r