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