wrapper class to prevent client from closing JarOutputStream when it closes the AppDa...
authorjprocter <jprocter@compbio.dundee.ac.uk>
Wed, 30 Nov 2005 18:06:04 +0000 (18:06 +0000)
committerjprocter <jprocter@compbio.dundee.ac.uk>
Wed, 30 Nov 2005 18:06:04 +0000 (18:06 +0000)
git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@102 be28352e-c001-0410-b1a7-c7978e42abec

src/org/vamsas/client/simpleclient/AppDataOutputStream.java [new file with mode: 0644]

diff --git a/src/org/vamsas/client/simpleclient/AppDataOutputStream.java b/src/org/vamsas/client/simpleclient/AppDataOutputStream.java
new file mode 100644 (file)
index 0000000..db13652
--- /dev/null
@@ -0,0 +1,86 @@
+/**
+ * 
+ */
+package org.vamsas.client.simpleclient;
+
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * @author jimp
+ *
+ */
+public class AppDataOutputStream extends DataOutputStream {
+  private Log log = LogFactory.getLog(AppDataOutputStream.class);
+  private boolean isOpen=true;
+  /**
+   * @param out
+   */
+  public AppDataOutputStream(OutputStream out) {
+    super(out);
+    isOpen=true;
+  }
+  /* (non-Javadoc)
+   * @see java.io.DataOutputStream#flush()
+   */
+  public void flush() throws IOException {
+    if (isOpen)
+      super.flush();
+    else
+      log.warn("flush() called on closed AppDataOutputStream");
+  }
+  /* (non-Javadoc)
+   * @see java.io.DataOutputStream#write(byte[], int, int)
+   */
+  public synchronized void write(byte[] b, int off, int len) throws IOException {
+    if (isOpen) {
+      super.write(b, off, len);
+    } else {
+      log.debug("write(b,off,len) called on closed AppDataOutputStream");
+      throw new IOException("Attempt to write to closed AppDataOutputStream");
+    }
+  }
+  /* (non-Javadoc)
+   * @see java.io.DataOutputStream#write(int)
+   */
+  public synchronized void write(int b) throws IOException {
+    if (isOpen) {
+      super.write(b);
+    } else {
+      log.debug("write(b) called on closed AppDataOutputStream");
+      throw new IOException("Attempt to write to closed AppDataOutputStream");
+    }
+  }
+  /**
+   * Sets an internal flag preventing further write operations 
+   * to the AppData output stream and flushes any pending writes.
+   * @see java.io.FilterOutputStream#close()
+   */
+  public void close() throws IOException {
+    isOpen=false;
+    super.flush();
+    log.debug("AppDataOutputStream was closed.");
+  }
+  /* (non-Javadoc)
+   * @see java.io.FilterOutputStream#write(byte[])
+   */
+  public void write(byte[] b) throws IOException {
+    if (isOpen) {
+      super.write(b);
+    } else {
+      log.debug("write(b[]) called on closed AppDataOutputStream");
+      throw new IOException("Attempt to write to closed AppDataOutputStream");
+    }
+  }
+  /**
+   * @return true if stream is still Open.
+   */
+  public boolean isOpen() {
+    return isOpen;
+  }
+  
+}