Byte Counting OutputStream : OutputStream « File Input Output « Java






Byte Counting OutputStream

  




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;
  }
}

   
    
  








Related examples in the same category

1.String Buffer OutputStream
2.A null output stream. All data written to this stream is ignored.
3.Memory Byte Array OutputStream
4.Read from InputStream and write to OutputStream
5.Compare the contents of two Streams to determine if they are equal or not.
6.Counting OutputStream
7.A utility class that allows for easy simple obfuscation of streamed data