Skip to content

Commit b39b77d

Browse files
[MOD] XQuery, Hashing Module: input is now streamed
1 parent 2813974 commit b39b77d

File tree

1 file changed

+19
-1
lines changed
  • basex-core/src/main/java/org/basex/query/func/hash

1 file changed

+19
-1
lines changed

basex-core/src/main/java/org/basex/query/func/hash/HashFn.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import static org.basex.query.QueryError.*;
44

5+
import java.io.*;
56
import java.security.*;
67

8+
import org.basex.io.*;
9+
import org.basex.io.in.*;
710
import org.basex.query.*;
811
import org.basex.query.func.*;
912
import org.basex.query.value.item.*;
@@ -23,8 +26,23 @@ abstract class HashFn extends StandardFunc {
2326
* @throws QueryException exception
2427
*/
2528
final B64 hash(final String algo, final QueryContext qc) throws QueryException {
29+
final Item item = exprs[0].atomItem(qc, info);
2630
try {
27-
return new B64(MessageDigest.getInstance(algo).digest(toBinary(exprs[0], qc)));
31+
final MessageDigest md = MessageDigest.getInstance(algo);
32+
if(item instanceof B64Stream) {
33+
try(final BufferInput bi = item.input(info)) {
34+
final byte[] tmp = new byte[IO.BLOCKSIZE];
35+
do {
36+
final int n = bi.read(tmp);
37+
if(n == -1) return new B64(md.digest());
38+
md.update(tmp, 0, n);
39+
} while(true);
40+
} catch(final IOException ex) {
41+
throw FILE_IO_ERROR_X.get(info, ex);
42+
}
43+
}
44+
// non-streaming item, string
45+
return new B64(md.digest(toBytes(item)));
2846
} catch(final NoSuchAlgorithmException ex) {
2947
throw HASH_ALG_X.get(info, algo);
3048
}

0 commit comments

Comments
 (0)