Coverage Report - org.jaudiotagger.audio.asf.io.FullRequestInputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
FullRequestInputStream
76%
16/21
58%
7/12
2.75
 
 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 repeatedly reads from the wrapped input stream until the
 9  
  * requested amount of bytes are read.<br>
 10  
  * 
 11  
  * @author Christian Laireiter
 12  
  */
 13  
 public class FullRequestInputStream extends FilterInputStream {
 14  
 
 15  
     /**
 16  
      * Creates an instance.
 17  
      * 
 18  
      * @param source
 19  
      *            stream to read from.
 20  
      */
 21  
     public FullRequestInputStream(final InputStream source) {
 22  343
         super(source);
 23  343
     }
 24  
 
 25  
     /**
 26  
      * {@inheritDoc}
 27  
      */
 28  
     @Override
 29  
     public int read(final byte[] buffer) throws IOException {
 30  0
         return read(buffer, 0, buffer.length);
 31  
     }
 32  
 
 33  
     /**
 34  
      * {@inheritDoc}
 35  
      */
 36  
     @Override
 37  
     public int read(final byte[] buffer, final int off, final int len)
 38  
             throws IOException {
 39  20926
         int totalRead = 0;
 40  
         int read;
 41  41897
         while (totalRead < len) {
 42  20971
             read = super.read(buffer, off + totalRead, len - totalRead);
 43  20971
             if (read >= 0) {
 44  20971
                 totalRead += read;
 45  
             }
 46  20971
             if (read == -1) {
 47  0
                 throw new IOException((len - totalRead)
 48  
                         + " more bytes expected.");
 49  
             }
 50  
         }
 51  20926
         return totalRead;
 52  
     }
 53  
 
 54  
     /**
 55  
      * {@inheritDoc}
 56  
      */
 57  
     @Override
 58  
     public long skip(final long amount) throws IOException {
 59  2954
         long skipped = 0;
 60  2954
         int zeroSkipCnt = 0;
 61  
         long currSkipped;
 62  5972
         while (skipped < amount) {
 63  3018
             currSkipped = super.skip(amount - skipped);
 64  3018
             if (currSkipped == 0) {
 65  0
                 zeroSkipCnt++;
 66  0
                 if (zeroSkipCnt == 2) {
 67  
                     // If the skip value exceeds streams size, this and the
 68  
                     // number is extremely large, this can lead to a very long
 69  
                     // running loop.
 70  0
                     break;
 71  
                 }
 72  
             }
 73  3018
             skipped += currSkipped;
 74  
         }
 75  2954
         return skipped;
 76  
     }
 77  
 
 78  
 }