| 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 | |
class CountingInputStream extends FilterInputStream |
| 14 | |
{ |
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | 164 | private long markPos = 0; |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | 164 | private long readCount = 0; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
public CountingInputStream(InputStream stream) |
| 32 | |
{ |
| 33 | 164 | super(stream); |
| 34 | 164 | } |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 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 | |
|
| 53 | |
|
| 54 | |
public long getReadCount() |
| 55 | |
{ |
| 56 | 184 | return this.readCount; |
| 57 | |
} |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
} |