updated test cases for spring 2006 schema, and major debug of locking system
[vamsas.git] / src / org / vamsas / client / simpleclient / LockedFileOutputStream.java
index 92d9d0c..1d59c8a 100644 (file)
@@ -8,19 +8,36 @@ import java.io.FileDescriptor;
 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
-  \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);\r
+    super(file); // super(file);\r
+    init();\r
   }\r
 \r
   /**\r
@@ -31,6 +48,7 @@ public class LockedFileOutputStream extends FileOutputStream {
   public LockedFileOutputStream(File file, boolean append)\r
       throws FileNotFoundException {\r
     super(file, append);\r
+    init();\r
   }\r
 \r
   /**\r
@@ -38,6 +56,9 @@ public class LockedFileOutputStream extends FileOutputStream {
    */\r
   public LockedFileOutputStream(FileDescriptor fdObj) {\r
     super(fdObj);\r
+    init();\r
+    if (fdObj.valid())\r
+      closed=false;\r
   }\r
 \r
   /**\r
@@ -46,6 +67,7 @@ public class LockedFileOutputStream extends FileOutputStream {
    */\r
   public LockedFileOutputStream(String name) throws FileNotFoundException {\r
     super(name);\r
+    init();\r
   }\r
 \r
   /**\r
@@ -56,14 +78,82 @@ public class LockedFileOutputStream extends FileOutputStream {
   public LockedFileOutputStream(String name, boolean append)\r
       throws FileNotFoundException {\r
     super(name, append);\r
-    // TODO Auto-generated constructor stub\r
+    init();\r
   }\r
   /**\r
    * closes - actually just flushes the stream instead.\r
    */\r
   public void close() throws IOException {\r
-    // TODO Auto-generated method stub\r
-    super.flush();\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