%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
ca.spaz.util.CountableInputStream |
|
|
1 | /* |
|
2 | ******************************************************************************* |
|
3 | * Copyright (c) 2005 Chris Rose and AIMedia |
|
4 | * All rights reserved. CountableBufferedInputStream and the accompanying materials |
|
5 | * are made available under the terms of the Common Public License v1.0 |
|
6 | * which accompanies this distribution, and is available at |
|
7 | * http://www.eclipse.org/legal/cpl-v10.html |
|
8 | * |
|
9 | * Contributors: |
|
10 | * Chris Rose |
|
11 | *******************************************************************************/ |
|
12 | package ca.spaz.util; |
|
13 | ||
14 | import java.io.*; |
|
15 | ||
16 | /** |
|
17 | * This class implements an InputStream that counts the bytes that it has |
|
18 | * read. |
|
19 | * |
|
20 | * @author Chris Rose |
|
21 | */ |
|
22 | public class CountableInputStream extends FilterInputStream { |
|
23 | ||
24 | private static final int DEAFULT_BUFFER_SIZE = 8192; |
|
25 | ||
26 | 0 | private long bytesRetrieved = 0; |
27 | ||
28 | 0 | private long markpos = -1; |
29 | ||
30 | /** |
|
31 | * Create a new <code>CountableInputStream</code> and wrap it around the |
|
32 | * supplied <code>InputStream</code>. |
|
33 | * |
|
34 | * @param in an <code>InputStream</code> object to count. |
|
35 | */ |
|
36 | public CountableInputStream(InputStream in) { |
|
37 | 0 | super(in); |
38 | 0 | } |
39 | ||
40 | /* (non-Javadoc) |
|
41 | * @see java.io.InputStream#read() |
|
42 | */ |
|
43 | public synchronized int read() throws IOException { |
|
44 | 0 | int next = super.read(); |
45 | 0 | if (next != -1) { |
46 | 0 | bytesRetrieved++; |
47 | } |
|
48 | 0 | return next; |
49 | } |
|
50 | ||
51 | /* (non-Javadoc) |
|
52 | * @see java.io.InputStream#read(byte[], int, int) |
|
53 | */ |
|
54 | public synchronized int read(byte[] b, class="keyword">int off, class="keyword">int len) throws IOException { |
|
55 | 0 | int next = super.read(b, off, len); |
56 | 0 | if (next != -1) { |
57 | 0 | bytesRetrieved += next; |
58 | } |
|
59 | 0 | return next; |
60 | } |
|
61 | ||
62 | /* (non-Javadoc) |
|
63 | * @see java.io.InputStream#mark(int) |
|
64 | */ |
|
65 | public synchronized void mark(int readlimit) { |
|
66 | 0 | super.mark(readlimit); |
67 | 0 | markpos = bytesRetrieved; |
68 | 0 | } |
69 | ||
70 | /* (non-Javadoc) |
|
71 | * @see java.io.InputStream#skip(long) |
|
72 | */ |
|
73 | public long skip(class="keyword">long n) throws IOException { |
|
74 | 0 | long next = super.skip(n); |
75 | 0 | bytesRetrieved += next; |
76 | 0 | return next; |
77 | } |
|
78 | ||
79 | /* (non-Javadoc) |
|
80 | * @see java.io.InputStream#reset() |
|
81 | */ |
|
82 | public synchronized void reset() throws IOException { |
|
83 | 0 | bytesRetrieved = markpos; |
84 | 0 | super.reset(); |
85 | 0 | } |
86 | ||
87 | /* (non-Javadoc) |
|
88 | * @see java.io.InputStream#read(byte[]) |
|
89 | */ |
|
90 | public int read(byte[] b) throws IOException { |
|
91 | 0 | int next = super.read(b); |
92 | 0 | if (next != -1) { |
93 | 0 | bytesRetrieved += next; |
94 | } |
|
95 | 0 | return next; |
96 | } |
|
97 | ||
98 | /** |
|
99 | * Get a count of the bytes read by this stream. |
|
100 | * @return the number of bytes read by this stream. |
|
101 | */ |
|
102 | public long getBytesRead() { |
|
103 | 0 | return bytesRetrieved; |
104 | } |
|
105 | ||
106 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |