Coverage Report - org.jaudiotagger.audio.asf.io.CountingInputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
CountingInputStream
81%
22/27
50%
1/2
0
 
 1  
 package org.jaudiotagger.audio.asf.io;
 2  
 
 3  
 import java.io.FilterInputStream;
 4  
 import java.io.IOException;
 5  
 import java.io.InputStream;
 6  
 
 7  
 /**
 8  
  * This implementation of {@link FilterInputStream} counts each read byte.<br>
 9  
  * So at each time, with {@link #getReadCount()} one can determine how many bytes have been read, by
 10  
  * this classes read and skip methods (mark and reset are also taken into account).<br>  
 11  
  * @author Christian Laireiter
 12  
  */
 13  
 class CountingInputStream extends FilterInputStream
 14  
 {
 15  
 
 16  
     /**
 17  
      * If {@link #mark(int)} has been called, the current value of {@link #readCount} is stored, in
 18  
      * order to reset it upon {@link #reset()}.
 19  
      */
 20  164
     private long markPos = 0;
 21  
 
 22  
     /**
 23  
      * The amount of read or skipped bytes.
 24  
      */
 25  164
     private long readCount = 0;
 26  
 
 27  
     /**
 28  
      * Creates an instance, which delegates the commands to the given stream.
 29  
      * @param stream stream to actually work with.
 30  
      */
 31  
     public CountingInputStream(InputStream stream)
 32  
     {
 33  164
         super(stream);
 34  164
     }
 35  
 
 36  
     /**
 37  
      * Counts the given amount of bytes.
 38  
      * @param i number of bytes to increase.
 39  
      */
 40  
     private void bytesRead(final long i)
 41  
     {
 42  46616
         synchronized (this)
 43  
         {
 44  46616
             if (i >= 0)
 45  
             {
 46  46616
                 readCount += i;
 47  
             }
 48  46616
         }
 49  46616
     }
 50  
 
 51  
     /**
 52  
      * @return the readCount
 53  
      */
 54  
     public long getReadCount()
 55  
     {
 56  184
         return this.readCount;
 57  
     }
 58  
 
 59  
     /**
 60  
      * {@inheritDoc}
 61  
      */
 62  
     @Override
 63  
     public synchronized void mark(int readlimit)
 64  
     {
 65  55
         super.mark(readlimit);
 66  55
         this.markPos = readCount;
 67  55
     }
 68  
 
 69  
     /**
 70  
      * {@inheritDoc}
 71  
      */
 72  
     @Override
 73  
     public int read() throws IOException
 74  
     {
 75  40766
         final int result = super.read();
 76  40766
         bytesRead(1);
 77  40766
         return result;
 78  
     }
 79  
 
 80  
     /**
 81  
      * {@inheritDoc}
 82  
      */
 83  
     @Override
 84  
     public int read(byte[] b, int off, int len) throws IOException
 85  
     {
 86  4638
         final int result = super.read(b, off, len);
 87  4638
         bytesRead(result);
 88  4638
         return result;
 89  
     }
 90  
 
 91  
     /**
 92  
      * {@inheritDoc}
 93  
      */
 94  
     @Override
 95  
     public synchronized void reset() throws IOException
 96  
     {
 97  0
         super.reset();
 98  0
         synchronized (this)
 99  
         {
 100  0
             this.readCount = markPos;
 101  0
         }
 102  0
     }
 103  
 
 104  
     /**
 105  
      * {@inheritDoc}
 106  
      */
 107  
     @Override
 108  
     public long skip(long n) throws IOException
 109  
     {
 110  1212
         long skipped = super.skip(n);
 111  1212
         bytesRead(skipped);
 112  1212
         return skipped;
 113  
     }
 114  
 
 115  
 }