backtrack
[vamsas.git] / src / org / vamsas / client / simpleclient / AppDataOutputStream.java
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;
+  }
+  
+}