Byte Counting OutputStream : OutputStream « File « Java Tutorial






import java.io.IOException;
import java.io.OutputStream;

/**
 * Output stream that counts bytes written to it (but discards them).
 * 
 * @author Jonathan Locke
 */
public final class ByteCountingOutputStream extends OutputStream
{
  private long size;

  /**
   * @see java.io.OutputStream#write(int)
   */
  public void write(int b) throws IOException
  {
    size++;
  }

  /**
   * @see java.io.OutputStream#write(byte[], int, int)
   */
  public void write(byte b[], int off, int len) throws IOException
  {
    size += len;
  }

  /**
   * @return Number of bytes written to this stream
   */
  public long size()
  {
    return size;
  }
}








11.13.OutputStream
11.13.1.Byte Counting OutputStream
11.13.2.Memory Byte Array OutputStream
11.13.3.A null output stream. All data written to this stream is ignored.
11.13.4.Provides true Closable semantics ordinarily missing in a {@link java.io.ByteArrayOutputStream}.
11.13.5.Write the entire contents of the supplied string to the given stream. This method always flushes and closes the stream when finished.
11.13.6.Transfers all bytes that can be read from one stream to another stream.