author | jboes |
Tue, 24 Sep 2019 09:43:43 +0100 | |
changeset 58288 | 48e480e56aad |
parent 58242 | 94bb65cb37d3 |
child 58679 | 9c3209ff7550 |
child 59201 | b24f4caa1411 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util.zip; |
|
27 |
||
28 |
import java.io.FilterInputStream; |
|
29 |
import java.io.InputStream; |
|
30 |
import java.io.IOException; |
|
31 |
||
32 |
/** |
|
33 |
* An input stream that also maintains a checksum of the data being read. |
|
34 |
* The checksum can then be used to verify the integrity of the input data. |
|
35 |
* |
|
36 |
* @see Checksum |
|
37 |
* @author David Connelly |
|
45434
4582657c7260
8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents:
25859
diff
changeset
|
38 |
* @since 1.1 |
2 | 39 |
*/ |
40 |
public |
|
41 |
class CheckedInputStream extends FilterInputStream { |
|
42 |
private Checksum cksum; |
|
43 |
||
44 |
/** |
|
45 |
* Creates an input stream using the specified Checksum. |
|
46 |
* @param in the input stream |
|
47 |
* @param cksum the Checksum |
|
48 |
*/ |
|
49 |
public CheckedInputStream(InputStream in, Checksum cksum) { |
|
50 |
super(in); |
|
51 |
this.cksum = cksum; |
|
52 |
} |
|
53 |
||
54 |
/** |
|
55 |
* Reads a byte. Will block if no input is available. |
|
56 |
* @return the byte read, or -1 if the end of the stream is reached. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
57 |
* @throws IOException if an I/O error has occurred |
2 | 58 |
*/ |
59 |
public int read() throws IOException { |
|
60 |
int b = in.read(); |
|
61 |
if (b != -1) { |
|
62 |
cksum.update(b); |
|
63 |
} |
|
64 |
return b; |
|
65 |
} |
|
66 |
||
67 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
68 |
* Reads into an array of bytes. If {@code len} is not zero, the method |
2 | 69 |
* blocks until some input is available; otherwise, no |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
70 |
* bytes are read and {@code 0} is returned. |
2 | 71 |
* @param buf the buffer into which the data is read |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
72 |
* @param off the start offset in the destination array {@code b} |
2 | 73 |
* @param len the maximum number of bytes read |
74 |
* @return the actual number of bytes read, or -1 if the end |
|
75 |
* of the stream is reached. |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
76 |
* @throws NullPointerException If {@code buf} is {@code null}. |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
77 |
* @throws IndexOutOfBoundsException If {@code off} is negative, |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
78 |
* {@code len} is negative, or {@code len} is greater than |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
79 |
* {@code buf.length - off} |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
80 |
* @throws IOException if an I/O error has occurred |
2 | 81 |
*/ |
82 |
public int read(byte[] buf, int off, int len) throws IOException { |
|
83 |
len = in.read(buf, off, len); |
|
84 |
if (len != -1) { |
|
85 |
cksum.update(buf, off, len); |
|
86 |
} |
|
87 |
return len; |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Skips specified number of bytes of input. |
|
92 |
* @param n the number of bytes to skip |
|
93 |
* @return the actual number of bytes skipped |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
94 |
* @throws IOException if an I/O error has occurred |
2 | 95 |
*/ |
96 |
public long skip(long n) throws IOException { |
|
97 |
byte[] buf = new byte[512]; |
|
98 |
long total = 0; |
|
99 |
while (total < n) { |
|
100 |
long len = n - total; |
|
101 |
len = read(buf, 0, len < buf.length ? (int)len : buf.length); |
|
102 |
if (len == -1) { |
|
103 |
return total; |
|
104 |
} |
|
105 |
total += len; |
|
106 |
} |
|
107 |
return total; |
|
108 |
} |
|
109 |
||
110 |
/** |
|
111 |
* Returns the Checksum for this input stream. |
|
112 |
* @return the Checksum value |
|
113 |
*/ |
|
114 |
public Checksum getChecksum() { |
|
115 |
return cksum; |
|
116 |
} |
|
117 |
} |