User: user57  
  Date: 02/02/14 22:14:24

  Added:       src/main/org/jboss/util/stream AppendObjectOutputStream.java
                        AppendingObjectOutputStream.java
                        AutoResetObjectOutputStream.java
                        CRLFPrintWriter.java NullInputStream.java
                        NullOutputStream.java
                        ObjectOutputStreamAdapter.java Printable.java
                        Streams.java package.html
  Log:
   o importing the useful bits from the bliss cl.
   o plus some bits from server/*/util/* have been moved to util.jmx
  
  Revision  Changes    Path
  1.1                  
jboss-common/src/main/org/jboss/util/stream/AppendObjectOutputStream.java
  
  Index: AppendObjectOutputStream.java
  ===================================================================
  /***************************************
   *                                     *
   *  JBoss: The OpenSource J2EE WebOS   *
   *                                     *
   *  Distributable under LGPL license.  *
   *  See terms of license at gnu.org.   *
   *                                     *
   ***************************************/
  
  package org.jboss.util.stream;
  
  import java.io.IOException;
  import java.io.ObjectOutputStream;
  import java.io.OutputStream;
  
  /**
   * An <code>ObjectOutputStream</code> that is meant for appending onto an
   * existing stream written to by a non <code>AppendObjectOutputStream</code>
   *
   * @version <tt>$Revision: 1.1 $</tt>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
   */
  public class AppendObjectOutputStream
     extends ObjectOutputStream
  {
     /**
      * Construct a new AppendObjectOutputStream.
      *
      * @param out     An output stream.
      *
      * @throws IOException  Any exception thrown by the underlying OutputStream.
      */
     public AppendObjectOutputStream(OutputStream out) throws IOException {
        super(out);
     }
  
     /**
      * Reset the stream, does not write headers.
      *
      * @throws IOException  Any exception thrown by the underlying OutputStream.
      */
     protected void writeStreamHeader() throws IOException {
        this.reset();
     }
  }
  
  
  
  1.1                  
jboss-common/src/main/org/jboss/util/stream/AppendingObjectOutputStream.java
  
  Index: AppendingObjectOutputStream.java
  ===================================================================
  /***************************************
   *                                     *
   *  JBoss: The OpenSource J2EE WebOS   *
   *                                     *
   *  Distributable under LGPL license.  *
   *  See terms of license at gnu.org.   *
   *                                     *
   ***************************************/
  
  package org.jboss.util.stream;
  
  import java.io.IOException;
  import java.io.ObjectOutputStream;
  import java.io.OutputStream;
  
  /**
   * An <tt>ObjectOutputStream</tt> that can conditionally be put into
   * <i>appending</i> mode.
   *
   * <dl>
   * <dt><b>Concurrency: </b></dt>
   * <dd>This class is <b>not</b> synchronized.</dd>
   * </dl>
   *
   * @version <tt>$Revision: 1.1 $</tt>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
   */
  public class AppendingObjectOutputStream
     extends ObjectOutputStreamAdapter
  {
     /**
      * Construct an <tt>AppendingObjectOutputStream</tt>.
      *
      * @param out     An <tt>OutputStream</tt> stream.
      * @param append  <tt>True</tt> to append written objects; <tt>false</tt>
      *                to use default action (writes stream header).
      *
      * @throws IOException                 Any exception thrown by
      *                                     the underlying <tt>OutputStream</tt>.
      */
     public AppendingObjectOutputStream(OutputStream out, boolean append)
        throws IOException 
     {
        super(createStream(out, append));
     }
  
     /**
      * Helper to return a <tt>ObjectOutputStream</tt>.
      */
     private static ObjectOutputStream createStream(OutputStream out, 
                                                    boolean append)
        throws IOException
     {
        ObjectOutputStream stream;
  
        // if we are appending then return an append only stream
        if (append) {
           stream = new AppendObjectOutputStream(out);
        }
        // else if it already an oos then return it
        else if (out instanceof ObjectOutputStream) {
           stream = (ObjectOutputStream)out;
        }
        // else wrap the stream in an oos
        else {
           stream = new ObjectOutputStream(out);
        }
  
        return stream;
     }
  }
  
  
  
  1.1                  
jboss-common/src/main/org/jboss/util/stream/AutoResetObjectOutputStream.java
  
  Index: AutoResetObjectOutputStream.java
  ===================================================================
  /***************************************
   *                                     *
   *  JBoss: The OpenSource J2EE WebOS   *
   *                                     *
   *  Distributable under LGPL license.  *
   *  See terms of license at gnu.org.   *
   *                                     *
   ***************************************/
  
  package org.jboss.util.stream;
  
  import java.io.ObjectOutputStream;
  import java.io.IOException;
  
  /**
   * An <code>ObjectOutputStream</code> that will auto reset after <i>n</i>
   * objects have been written to the underlying stream.
   *
   * <h3>Concurrency</h3>
   * This class is <b>not</b> synchronized.
   *
   * @version <tt>$Revision: 1.1 $</tt>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
   */
  public class AutoResetObjectOutputStream
     extends ObjectOutputStreamAdapter
  {
     /** Number of objects to write before resetting. */
     protected int after; // = 0
  
     /** Number of objects written so far. */
     protected int count; // = 0
  
     /**
      * Construct a new AutoResetObjectOutputStream.
      *
      * @param out     An ObjectOutputStream stream.
      * @param after   Number of objects to write before resetting.
      *
      * @throws IllegalArgumentException    After <= 0
      * @throws IOException                 Any exception thrown by
      *                                     the underlying OutputStream.
      */
     public AutoResetObjectOutputStream(ObjectOutputStream out, int after)
        throws IOException 
     {
        super(out);
  
        setResetAfter(after);
     }
  
     /**
      * Set the number of objects that must be written before resetting
      * the stream.
      *
      * @param after   Number of objects to write before resetting.
      *
      * @throws IllegalArgumentException    After <= 0
      */
     public void setResetAfter(int after) {
        if (after <= 0)
           throw new IllegalArgumentException("after <= 0");
  
        this.after = after;
     }
  
     /**
      * Get the number of objects that must be written before resetting
      * the stream.
      *
      * @return  Number of objects to write before resetting.
      */
     public final int getResetAfter() {
        return after;
     }
  
     /**
      * Get the number of objects written to the stream so far.
      *
      * @return  The number of objects written to the stream so far.
      */
     public final int getCount() {
        return count;
     }
  
     /**
      * Write the given object and reset if the number of objects written
      * (including this one) exceeds the after count.
      *
      * @param obj     Object to write.
      *
      * @throws IOException  Any exception thrown by the underlying stream.
      */
     protected void writeObjectOverride(Object obj) throws IOException {
        super.writeObjectOverride(obj);
        count++;
  
        if (count >= after) {
           reset();
        }
     }
  
     /**
      * Resets the object counter as well as the nested stream.
      *
      * @throws IOException
      */
     public void reset() throws IOException {
        out.reset();
        count = 0;
     }
  }
  
  
  
  1.1                  jboss-common/src/main/org/jboss/util/stream/CRLFPrintWriter.java
  
  Index: CRLFPrintWriter.java
  ===================================================================
  /***************************************
   *                                     *
   *  JBoss: The OpenSource J2EE WebOS   *
   *                                     *
   *  Distributable under LGPL license.  *
   *  See terms of license at gnu.org.   *
   *                                     *
   ***************************************/
  
  package org.jboss.util.stream;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.io.OutputStream;
  import java.io.InterruptedIOException;
  import java.io.Writer;
  
  /**
   * A <tt>PrintWriter</tt> that ends lines with a carriage return-line feed 
   * (<tt>CRLF</tt>).
   *
   * <h3>Concurrency</h3>
   * This class is <b>as</b> synchronized as <tt>PrintWriter</tt>.
   *
   * @version <tt>$Revision: 1.1 $</tt>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
   */
  public class CRLFPrintWriter
     extends PrintWriter
  {
     protected boolean autoFlush = false;
  
     public CRLFPrintWriter(final Writer out) {
        super(out);
     }
  
     public CRLFPrintWriter(final Writer out, final boolean autoFlush) {
        super(out, autoFlush);
        this.autoFlush = autoFlush;
     }
  
     public CRLFPrintWriter(final OutputStream out) {
        super(out);
     }
  
     public CRLFPrintWriter(final OutputStream out, final boolean autoFlush) {
        super(out, autoFlush);
        this.autoFlush = autoFlush;
     }
  
     protected void ensureOpen() throws IOException {
        if (out == null)
           throw new IOException("Stream closed");
     }
  
     public void println() {
        try {
           synchronized (lock) {
              ensureOpen();
  
              out.write("\r\n");
  
              if (autoFlush) {
                 out.flush();
              }
           }
        }
        catch (InterruptedIOException e) {
           Thread.currentThread().interrupt();
        }
        catch (IOException e) {
           setError();
        }
     }      
  }
  
  
  
  1.1                  jboss-common/src/main/org/jboss/util/stream/NullInputStream.java
  
  Index: NullInputStream.java
  ===================================================================
  /***************************************
   *                                     *
   *  JBoss: The OpenSource J2EE WebOS   *
   *                                     *
   *  Distributable under LGPL license.  *
   *  See terms of license at gnu.org.   *
   *                                     *
   ***************************************/
  
  package org.jboss.util.stream;
  
  import java.io.InputStream;
  
  /**
   * A <tt>null</tt> <code>InputStream</code>.  Methods that return values, 
   * return values that indicate that there is no more data to be read, other 
   * methods are non-operations.
   *
   * @version <tt>$Revision: 1.1 $</tt>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
   */
  public final class NullInputStream
     extends InputStream
  {
     /** A default null input stream. */
     public static final NullInputStream INSTANCE = new NullInputStream();
  
     /**
      * Always returns zero.
      *
      * @return  Zero.
      */
     public int available() {
        return 0;
     }
  
     /**
      * Non-operation.
      */
     public void mark(final int readLimit) {
     }
  
     /**
      * Always returns false.
      *
      * @return  False.
      */
     public boolean markSupported() {
        return false;
     }
  
     /**
      * Non-operation.
      */
     public void reset() {
     }
  
     /**
      * Non-operation.
      */
     public void close() {
     }
  
     /**
      * Always returns -1.
      *
      * @return  -1.
      */
     public int read() {
        return -1;
     }
  
     /**
      * Always returns -1.
      *
      * @return  -1.
      */
     public int read(final byte bytes[], final int offset, final int length) {
        return -1;
     }
  
     /**
      * Always returns -1.
      *
      * @return  -1.
      */
     public int read(final byte bytes[]) {
        return -1;
     }
  
     /**
      * Always returns zero.
      *
      * @return  Zero.
      */
     public long skip(final long n) {
        return 0;
     }
  }
  
  
  
  1.1                  
jboss-common/src/main/org/jboss/util/stream/NullOutputStream.java
  
  Index: NullOutputStream.java
  ===================================================================
  /***************************************
   *                                     *
   *  JBoss: The OpenSource J2EE WebOS   *
   *                                     *
   *  Distributable under LGPL license.  *
   *  See terms of license at gnu.org.   *
   *                                     *
   ***************************************/
  
  package org.jboss.util.stream;
  
  import java.io.OutputStream;
  
  /**
   * A <tt>null</tt> <code>OutputStream</code>.  All values passed to 
   * {@link #write(int)} are discarded.  Calls to {@link #flush()} and 
   * {@link #close()} are ignored. 
   *
   * <p>All methods are declared <b>NOT</b> to throw <code>IOException</code>s.
   *
   * @version <tt>$Revision: 1.1 $</tt>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
   */
  public final class NullOutputStream
     extends OutputStream
  {
     /** A default null output stream. */
     public static final NullOutputStream STREAM = new NullOutputStream();
  
     /**
      * Non-operation.
      */
     public void write(final int b) {}
  
     /**
      * Non-operation.
      */
     public void flush() {}
  
     /**
      * Non-operation.
      */
     public void close() {}
  
     /**
      * Non-operation.
      */
     public void write(final byte[] bytes) {}
  
     /**
      * Non-operation.
      */
     public void write(final byte[] bytes, final int offset, final int length) {}
  }
  
  
  
  1.1                  
jboss-common/src/main/org/jboss/util/stream/ObjectOutputStreamAdapter.java
  
  Index: ObjectOutputStreamAdapter.java
  ===================================================================
  /***************************************
   *                                     *
   *  JBoss: The OpenSource J2EE WebOS   *
   *                                     *
   *  Distributable under LGPL license.  *
   *  See terms of license at gnu.org.   *
   *                                     *
   ***************************************/
  
  package org.jboss.util.stream;
  
  import java.io.IOException;
  import java.io.ObjectOutputStream;
  
  import org.jboss.util.NullArgumentException;
  
  /**
   * An <code>ObjectOutputStream</code> wrapping adapter.
   *
   * <h3>Concurrency</h3>
   * This class is <b>not</b> synchronized.
   *
   * @version <tt>$Revision: 1.1 $</tt>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
   */
  public abstract class ObjectOutputStreamAdapter
     extends ObjectOutputStream
  {
     /** Nested object output stream */
     protected ObjectOutputStream out;
     
     /**
      * Construct a new ObjectOutputStreamAdapter.
      *
      * @param out     An ObjectOutputStream stream.
      *
      * @throws IOException              Any exception thrown by the underlying
      *                                  OutputStream.
      * @throws IllegalArgumentException Out is null.
      */
     public ObjectOutputStreamAdapter(ObjectOutputStream out)
        throws IOException 
     {
        super(); // allow calls to writeObjectOverride()
  
        if (out == null)
           throw new NullArgumentException("out");
  
        this.out = out;
     }
  
     protected void writeObjectOverride(Object obj) throws IOException {
        out.writeObject(obj);
     }
  
     public void useProtocolVersion(int version) throws IOException {
        out.useProtocolVersion(version);
     }
  
     public void defaultWriteObject() throws IOException {
        out.defaultWriteObject();
     }
  
     public ObjectOutputStream.PutField putFields() throws IOException {
        return out.putFields();
     }
  
     public void writeFields() throws IOException {
        out.writeFields();
     }
  
     public void reset() throws IOException {
        out.reset();
     }
  
     public void write(int data) throws IOException {
        out.write(data);
     }
  
     public void write(byte b[]) throws IOException {
        out.write(b);
     }
  
     public void write(byte b[], int off, int len) throws IOException {
        out.write(b, off, len);
     }
  
     public void flush() throws IOException {
        out.flush();
     }
  
     public void close() throws IOException {
        out.close();
     }
  
     public void writeBoolean(boolean data) throws IOException {
        out.writeBoolean(data);
     }
  
     public void writeByte(int data) throws IOException {
        out.writeByte(data);
     }
  
     public void writeShort(int data) throws IOException {
        out.writeShort(data);
     }
  
     public void writeChar(int data) throws IOException {
        out.writeChar(data);
     }
  
     public void writeInt(int data) throws IOException {
        out.writeInt(data);
     }
  
     public void writeLong(long data) throws IOException {
        out.writeLong(data);
     }
  
     public void writeFloat(float data) throws IOException {
        out.writeFloat(data);
     }
  
     public void writeDouble(double data) throws IOException {
        out.writeDouble(data);
     }
  
     public void writeBytes(String data) throws IOException {
        out.writeBytes(data);
     }
  
     public void writeChars(String data) throws IOException {
        out.writeChars(data);
     }
  
     public void writeUTF(String s) throws IOException {
        out.writeUTF(s);
     }
  }
  
  
  
  1.1                  jboss-common/src/main/org/jboss/util/stream/Printable.java
  
  Index: Printable.java
  ===================================================================
  /***************************************
   *                                     *
   *  JBoss: The OpenSource J2EE WebOS   *
   *                                     *
   *  Distributable under LGPL license.  *
   *  See terms of license at gnu.org.   *
   *                                     *
   ***************************************/
  
  package org.jboss.util.stream;
  
  import java.io.PrintWriter;
  import java.io.PrintStream;
  
  /**
   * A simple interface to allow an object to print itself to a 
   * <code>PrintWriter</code> or <code>PrintStream</code>.
   *
   * @version <tt>$Revision: 1.1 $</tt>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
   */
  public interface Printable
  {
     /**
      * Print to a PrintWriter.
      *
      * @param writer  PrintWriter to print to.
      */
     void print(PrintWriter writer);
  
     /**
      * Print to a PrintWriter.
      *
      * @param writer  PrintWriter to print to.
      * @param prefix  Prefix to append to each line in the stream.
      */
     void print(PrintWriter writer, String prefix);
  
     /**
      * Print to a PrintStream.
      *
      * @param stream  PrintStream to print to.
      */
     void print(PrintStream stream);
  
     /**
      * Print to a PrintStream.
      *
      * @param stream  PrintStream to print to.
      * @param prefix  Prefix to append to each line in the stream.
      */
     void print(PrintStream stream, String prefix);
  }
  
  
  
  1.1                  jboss-common/src/main/org/jboss/util/stream/Streams.java
  
  Index: Streams.java
  ===================================================================
  /***************************************
   *                                     *
   *  JBoss: The OpenSource J2EE WebOS   *
   *                                     *
   *  Distributable under LGPL license.  *
   *  See terms of license at gnu.org.   *
   *                                     *
   ***************************************/
  
  package org.jboss.util.stream;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  import org.jboss.util.ThrowableHandler;
  
  /**
   * A collection of stream related utility methods.
   *
   * <p>Exceptions that are thrown and not explicitly declared are given to
   *    the {@link ThrowableHandler} for further processing.
   *
   * @version <tt>$Revision: 1.1 $</tt>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
   */
  public final class Streams
  {
     /////////////////////////////////////////////////////////////////////////
     //                               Closing                               //
     /////////////////////////////////////////////////////////////////////////
  
     /**
      * Attempt to close an <tt>InputStream</tt>.
      *
      * @param stream  <tt>InputStream</tt> to attempt to close.
      * @return        <tt>True</tt> if stream was closed, or <tt>false</tt> if
      *                an exception was thrown.
      */
     public static boolean close(final InputStream stream) {
        boolean success = true;
  
        try {
           stream.close();
        }
        catch (IOException e) {
           success = false;
           ThrowableHandler.add(e);
        }
  
        return success;
     }
  
     /**
      * Attempt to close an <tt>OutputStream</tt>.
      *
      * @param stream  <tt>OutputStream</tt> to attempt to close.
      * @return        <tt>True</tt> if stream was closed, or <tt>false</tt> if
      *                an exception was thrown.
      */
     public static boolean close(final OutputStream stream) {
        boolean success = true;
  
        try {
           stream.close();
        }
        catch (IOException e) {
           success = false;
           ThrowableHandler.add(e);
        }
  
        return success;
     }
  
     /**
      * Attempt to close an <tt>InputStream</tt> or <tt>OutputStream</tt>.
      *
      * @param stream  Stream to attempt to close.
      * @return        <tt>True</tt> if stream was closed, or <tt>false</tt> if
      *                an exception was thrown.
      *
      * @throws IllegalArgumentException    Stream is not an <tt>InputStream</tt>
      *                                     or <tt>OuputStream</tt>.
      */
     public static boolean close(final Object stream) {
        boolean success = false;
  
        if (stream instanceof InputStream) {
           success = close((InputStream)stream);
        }
        else if (stream instanceof OutputStream) {
           success = close((OutputStream)stream);
        }
        else {
           throw new IllegalArgumentException
              ("stream is not an InputStream or OutputStream");
        }
  
        return success;
     }
  
     /**
      * Attempt to close an array of <tt>InputStream</tt>s.
      *
      * @param streams Array of <tt>InputStream</tt>s to attempt to close.
      * @return        <tt>True</tt> if all streams were closed, or <tt>false</tt>
      *                if an exception was thrown.
      */
     public static boolean close(final InputStream[] streams) {
        boolean success = true;
  
        for (int i=0; i<streams.length; i++) {
           boolean rv = close(streams[i]);
           if (!rv) success = false;
        }
  
        return success;
     }
  
     /**
      * Attempt to close an array of <tt>OutputStream</tt>s.
      *
      * @param streams Array of <tt>OutputStream</tt>s to attempt to close.
      * @return        <tt>True</tt> if all streams were closed, or <tt>false</tt>
      *                if an exception was thrown.
      */
     public static boolean close(final OutputStream[] streams) {
        boolean success = true;
  
        for (int i=0; i<streams.length; i++) {
           boolean rv = close(streams[i]);
           if (!rv) success = false;
        }
  
        return success;
     }
  
     /**
      * Attempt to close an array of <tt>InputStream</tt>a and/or 
      * <tt>OutputStream</tt>s.
      *
      * @param streams Array of streams to attempt to close.
      * @return        <tt>True</tt> if all streams were closed, or <tt>false</tt>
      *                if an exception was thrown.
      *
      * @throws IllegalArgumentException    Stream is not an <tt>InputStream</tt>
      *                                     or <tt>OuputStream</tt>.  Closing 
      *                                     stops at the last valid stream
      *                                     object in this case.
      */
     public static boolean close(final Object[] streams) {
        boolean success = true;
  
        for (int i=0; i<streams.length; i++) {
           boolean rv = close(streams[i]);
           if (!rv) success = false;
        }
  
        return success;
     }
  
     /**
      * Attempt to flush and close an <tt>OutputStream</tt>.
      *
      * @param stream  <tt>OutputStream</tt> to attempt to flush and close.
      * @return        <tt>True</tt> if stream was flushed and closed, or
      *                <tt>false</tt> if an exception was thrown.
      */
     public static boolean fclose(final OutputStream stream) {
         return flush(stream) && close(stream);
     }
  
     /**
      * Attempt to flush and close an array of <tt>OutputStream</tt>s.
      *
      * @param streams  <tt>OutputStream</tt>s to attempt to flush and close.
      * @return         <tt>True</tt> if all streams were flushed and closed, 
      *                 or <tt>false</tt> if an exception was thrown.
      */
     public static boolean fclose(final OutputStream[] streams) {
        boolean success = true;
  
        for (int i=0; i<streams.length; i++) {
           boolean rv = fclose(streams[i]); 
           if (!rv) success = false;
        }
  
        return success;
     }
      
  
     /////////////////////////////////////////////////////////////////////////
     //                                Flushing                             //
     /////////////////////////////////////////////////////////////////////////
  
     /**
      * Attempt to flush an <tt>OutputStream</tt>.
      *
      * @param stream  <tt>OutputStream</tt> to attempt to flush.
      * @return        <tt>True</tt> if stream was flushed, or <tt>false</tt> if
      *                an exception was thrown.
      */
     public static boolean flush(final OutputStream stream) {
        boolean success = true;
  
        try {
           stream.flush();
        }
        catch (IOException e) {
           success = false;
           ThrowableHandler.add(e);
        }
  
        return success;
     }
  
     /**
      * Attempt to flush an array of <tt>OutputStream</tt>s.
      *
      * @param streams <tt>OutputStream</tt>s to attempt to flush.
      * @return        <tt>True</tt> if all streams were flushed, or <tt>false</tt>
      *                 if an exception was thrown.
      */
     public static boolean flush(final OutputStream[] streams) {
        boolean success = true;
  
        for (int i=0; i<streams.length; i++) {
           boolean rv = flush(streams[i]);
           if (!rv) success = false;
        }
  
        return success;
     }
  
  
     /////////////////////////////////////////////////////////////////////////
     //                                  Misc                               //
     /////////////////////////////////////////////////////////////////////////
  
     /** The default buffer size that will be used for buffered operations. */
     public static final int DEFAULT_BUFFER_SIZE = 2048;
  
     /**
      * Copy all of the bytes from the input stream to the output stream.
      *
      * @param input   Stream to read bytes from.
      * @param output  Stream to write bytes to.
      * @param buffer  The buffer to use while copying.
      * @return        The total number of bytes copied.
      *
      * @throws IOException  Failed to copy bytes.
      */
     public static long copy(final InputStream input, 
                             final OutputStream output, 
                             final byte buffer[])
        throws IOException
     {
        long total = 0;
        int read;
  
        while ((read = input.read(buffer)) != -1) {
           output.write(buffer, 0, read);
           total += read;
        }
  
        return total;
     }
  
     /**
      * Copy all of the bytes from the input stream to the output stream.
      *
      * @param input   Stream to read bytes from.
      * @param output  Stream to write bytes to.
      * @param size    The size of the buffer to use while copying.
      * @return        The total number of bytes copied.
      *
      * @throws IOException  Failed to copy bytes.
      */
     public static long copy(final InputStream input, 
                             final OutputStream output, 
                             final int size)
        throws IOException
     {
        return copy(input, output, new byte[size]);
     }
  
     /**
      * Copy all of the bytes from the input stream to the output stream.
      *
      * @param input   Stream to read bytes from.
      * @param output  Stream to write bytes to.
      * @return        The total number of bytes copied.
      *
      * @throws IOException  Failed to copy bytes.
      */
     public static long copy(final InputStream input, 
                             final OutputStream output)
        throws IOException
     {
        return copy(input, output, DEFAULT_BUFFER_SIZE);
     }
  
     /**
      * Copy a limited number of bytes from the input stream to the 
      * output stream.
      *
      * @param input   Stream to read bytes from.
      * @param output  Stream to write bytes to.
      * @param buffer  The buffer to use while copying.
      * @param length  The maximum number of bytes to copy.
      * @return        The total number of bytes copied.
      *
      * @throws IOException  Failed to copy bytes.
      */
     public static long copySome(final InputStream input, 
                                 final OutputStream output, 
                                 final byte buffer[],
                                 final long length)
        throws IOException
     {
        long total = 0;
        int read;
        int readLength;
  
        // setup the initial readLength, if length is less than the buffer
        // size, then we only want to read that much
        readLength = Math.min((int)length, buffer.length);
        // System.out.println("initial read length: " + readLength);
  
        while (readLength != 0 && (read = input.read(buffer, 0, readLength)) != -1) 
        {
           // System.out.println("read bytes: " + read);
           output.write(buffer, 0, read);
           total += read;
           // System.out.println("total bytes read: " + total);
  
           // update the readLength
           readLength = Math.min((int)(length - total), buffer.length);
           // System.out.println("next read length: " + readLength);
        }
  
        return total;
     }
  
     /**
      * Copy a limited number of bytes from the input stream to the 
      * output stream.
      *
      * @param input   Stream to read bytes from.
      * @param output  Stream to write bytes to.
      * @param size    The size of the buffer to use while copying.
      * @param length  The maximum number of bytes to copy.
      * @return        The total number of bytes copied.
      *
      * @throws IOException  Failed to copy bytes.
      */
     public static long copySome(final InputStream input, 
                                 final OutputStream output, 
                                 final int size,
                                 final long length)
        throws IOException
     {
        return copySome(input, output, new byte[size], length);
     }
  
     /**
      * Copy a limited number of bytes from the input stream to the 
      * output stream.
      *
      * @param input   Stream to read bytes from.
      * @param output  Stream to write bytes to.
      * @param length  The maximum number of bytes to copy.
      * @return        The total number of bytes copied.
      *
      * @throws IOException  Failed to copy bytes.
      */
     public static long copySome(final InputStream input, 
                                 final OutputStream output, 
                                 final long length)
        throws IOException
     {
        return copySome(input, output, DEFAULT_BUFFER_SIZE, length);
     }
  }
  
  
  
  1.1                  jboss-common/src/main/org/jboss/util/stream/package.html
  
  Index: package.html
  ===================================================================
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
    <head>
      <!-- $Id: package.html,v 1.1 2002/02/15 06:14:24 user57 Exp $ -->
      <!--
  
      JBoss: The OpenSource J2EE WebOS 
  
      Distributable under LGPL license.
      See terms of license at gnu.org.
  
      -->
    </head>
  
    <body bgcolor="white">
      <p>Stream related classes.</p>
  
      <h2>Package Specification</h2>
      <ul>
        <li><a href="javascript: alert('not available')">Not Available</a>
      </ul>
        
      <h2>Related Documentation</h2>
      <ul>
        <li><a href="javascript: alert('not available')">Not Available</a>
      </ul>
  
      <h2>Package Status</h2>
      <ul>
        <li><font color="green"><b>STABLE</b></font>
      </ul>
  
      <h2>Todo</h2>
      <ul>
        <li>???
      </ul>
  
      <!-- Put @see and @since tags down here. -->
  
    </body>
  </html>
  
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to