Skip to content

Commit 7d32bee

Browse files
committed
OutputStream that computes hash while writing.
1 parent 5e26d9b commit 7d32bee

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* $Id$
3+
*/
4+
5+
/*
6+
7+
Copyright (c) 2016 Board of Trustees of Leland Stanford Jr. University,
8+
all rights reserved.
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a copy
11+
of this software and associated documentation files (the "Software"), to deal
12+
in the Software without restriction, including without limitation the rights
13+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
copies of the Software, and to permit persons to whom the Software is
15+
furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included in
18+
all copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23+
STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
25+
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26+
27+
Except as contained in this notice, the name of Stanford University shall not
28+
be used in advertising or otherwise to promote the sale, use or other dealings
29+
in this Software without prior written authorization from Stanford University.
30+
31+
*/
32+
33+
package org.lockss.util;
34+
import java.io.*;
35+
import java.security.MessageDigest;
36+
37+
/**
38+
* An OutputStream wrapper that feeds all the bytes written to a MessageDigest.
39+
*/
40+
public class HashedOutputStream extends FilterOutputStream {
41+
private MessageDigest md;
42+
43+
/**
44+
* @param os the underlying OutputStream.
45+
* @param md the MessageDigest to which to feed the bytes written.
46+
*/
47+
public HashedOutputStream(OutputStream os, MessageDigest md) {
48+
super(os);
49+
if (os == null) {
50+
throw new IllegalArgumentException("null OutputStream");
51+
}
52+
if (md == null) {
53+
throw new IllegalArgumentException("null MessageDigest");
54+
}
55+
this.md = md;
56+
}
57+
58+
public void write(int b) throws IOException {
59+
byte[] buf = new byte[1];
60+
buf[0] = (byte)b;
61+
write(buf);
62+
}
63+
64+
public void write(byte[] buf) throws IOException {
65+
write(buf, 0, buf.length);
66+
}
67+
68+
public void write(byte[] buf, int off, int len) throws IOException {
69+
out.write(buf, off, len);
70+
md.update(buf, off, len);
71+
}
72+
73+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* $Id$
3+
*/
4+
5+
/*
6+
7+
Copyright (c) 2000-2016 Board of Trustees of Leland Stanford Jr. University,
8+
all rights reserved.
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a copy
11+
of this software and associated documentation files (the "Software"), to deal
12+
in the Software without restriction, including without limitation the rights
13+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
copies of the Software, and to permit persons to whom the Software is
15+
furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included in
18+
all copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23+
STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
25+
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26+
27+
Except as contained in this notice, the name of Stanford University shall not
28+
be used in advertising or otherwise to promote the sale, use or other dealings
29+
in this Software without prior written authorization from Stanford University.
30+
31+
*/
32+
33+
package org.lockss.util;
34+
import java.io.*;
35+
import java.security.*;
36+
import org.lockss.test.*;
37+
38+
public class TestHashedOutputStream extends LockssTestCase {
39+
40+
private MockMessageDigest makeMessageDigest() {
41+
return new MockMessageDigest();
42+
}
43+
44+
public void testNullArgumentsToConstructor() {
45+
MessageDigest md = makeMessageDigest();
46+
OutputStream out = new ByteArrayOutputStream();
47+
try {
48+
new HashedOutputStream(null, md);
49+
fail("Calling the constructor with null OutputStream should throw");
50+
} catch (IllegalArgumentException e) {
51+
}
52+
try {
53+
new HashedOutputStream(out, null);
54+
fail("Calling the constructor with null MessageDigest should throw");
55+
} catch (IllegalArgumentException e) {
56+
}
57+
}
58+
59+
byte[] getBytes(String s) throws UnsupportedEncodingException {
60+
return s.getBytes(Constants.DEFAULT_ENCODING);
61+
}
62+
63+
public void testWrite() throws IOException {
64+
ByteArrayOutputStream out = new ByteArrayOutputStream();
65+
MockMessageDigest md = makeMessageDigest();
66+
HashedOutputStream hos = new HashedOutputStream(out, md);
67+
hos.write('a');
68+
assertEquals(getBytes("a"), md.getUpdatedBytes());
69+
hos.write(getBytes("bcdefghijk"));
70+
hos.write(getBytes("1234567890"), 3, 4);
71+
assertEquals(getBytes("bcdefghijk4567"), md.getUpdatedBytes());
72+
assertEquals("abcdefghijk4567", out.toString());
73+
}
74+
75+
}

0 commit comments

Comments
 (0)