| 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 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
class CountingInputStream extends FilterInputStream { |
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
private long markPos; |
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
private long readCount; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 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 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 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 | |
|
| 55 | |
|
| 56 | |
public synchronized long getReadCount() { |
| 57 | 6112 | return this.readCount; |
| 58 | |
} |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
} |