X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Forg%2Fvamsas%2Fclient%2Fsimpleclient%2FAppDataOutputStream.java;fp=src%2Forg%2Fvamsas%2Fclient%2Fsimpleclient%2FAppDataOutputStream.java;h=db1365238abff9fc661303ba871cfbfbaad54023;hb=e15b38a5b95e0aa5b11707728abb539c073fe27c;hp=0000000000000000000000000000000000000000;hpb=bfd813ba38e1d432d49a311422ade5ba33203a43;p=vamsas.git diff --git a/src/org/vamsas/client/simpleclient/AppDataOutputStream.java b/src/org/vamsas/client/simpleclient/AppDataOutputStream.java new file mode 100644 index 0000000..db13652 --- /dev/null +++ b/src/org/vamsas/client/simpleclient/AppDataOutputStream.java @@ -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; + } + +}