Class CounterOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable, Limit

    public class CounterOutputStream
    extends java.io.OutputStream
    implements Limit
    An OutputStream that simply provides methods to count the number of bytes written to an underlying stream.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int count  
      private int limit  
      protected java.io.OutputStream out  
    • Constructor Summary

      Constructors 
      Constructor Description
      CounterOutputStream()
      Create a CounterOutputStream that will discard any bytes written but still coutn them and call its reset method so that the count is intially zero.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int clearLimit()
      Clear any limit set by setLimit.
      int getCount()
      Get count of bytes written to the stream since the last reset() call.
      void setLimit​(int limit)
      Set a limit at which an exception will be thrown.
      void setOutputStream​(java.io.OutputStream out)  
      void write​(byte[] b, int off, int len)
      Add len to the count, discard the data.
      void write​(int b)
      Add 1 to the count.
      • Methods inherited from class java.io.OutputStream

        close, flush, nullOutputStream, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • out

        protected java.io.OutputStream out
      • count

        private int count
      • limit

        private int limit
    • Constructor Detail

      • CounterOutputStream

        public CounterOutputStream()
        Create a CounterOutputStream that will discard any bytes written but still coutn them and call its reset method so that the count is intially zero.
    • Method Detail

      • setOutputStream

        public void setOutputStream​(java.io.OutputStream out)
      • getCount

        public int getCount()
        Get count of bytes written to the stream since the last reset() call.
      • setLimit

        public void setLimit​(int limit)
        Set a limit at which an exception will be thrown. This allows callers to count the number of bytes up to some point, without having to complete the count. E.g. a caller may only want to see if some object will write out over 4096 bytes, without waiting for all 200,000 bytes of the object to be written.
        If the passed in limit is 0 or negative then the stream will count bytes without throwing an exception.
        Specified by:
        setLimit in interface Limit
        See Also:
        EOFException
      • clearLimit

        public int clearLimit()
        Description copied from interface: Limit
        Clear any limit set by setLimit. After this call no limit checking will be made on any read until a setLimit()) call is made.
        Specified by:
        clearLimit in interface Limit
        Returns:
        the number of bytes within the limit that have not been read or written.
      • write

        public void write​(int b)
                   throws java.io.IOException
        Add 1 to the count.
        Specified by:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
        See Also:
        OutputStream.write(int)
      • write

        public void write​(byte[] b,
                          int off,
                          int len)
                   throws java.io.IOException
        Add len to the count, discard the data.
        Overrides:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
        See Also:
        OutputStream.write(int)