annotate src/org/tmatesoft/hg/internal/DigestHelper.java @ 281:81e9a3c9bafe

Utilize IntMap when caching manifest revisions
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 02 Sep 2011 13:59:21 +0200
parents 0a2f445de774
children 981f9f50bb6c
rev   line source
31
346b66add79d Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 17
diff changeset
1 /*
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
2 * Copyright (c) 2010-2011 TMate Software Ltd
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
3 *
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
4 * This program is free software; you can redistribute it and/or modify
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
5 * it under the terms of the GNU General Public License as published by
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
6 * the Free Software Foundation; version 2 of the License.
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
7 *
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
8 * This program is distributed in the hope that it will be useful,
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
11 * GNU General Public License for more details.
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
12 *
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
13 * For information on how to redistribute this software under
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
14 * the terms of a license other than GNU General Public License
102
a3a2e5deb320 Updated contact address to support@hg4j.com
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 83
diff changeset
15 * contact TMate Software at support@hg4j.com
9
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
17 package org.tmatesoft.hg.internal;
9
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import java.io.IOException;
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20 import java.io.InputStream;
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.security.MessageDigest;
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.security.NoSuchAlgorithmException;
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
24 import org.tmatesoft.hg.core.Nodeid;
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
25
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
26
9
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 /**
42
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
28 * <pre>
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
29 * DigestHelper dh;
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
30 * dh.sha1(...).asHexString();
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
31 * or
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
32 * dh = dh.sha1(...);
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
33 * nodeid.equalsTo(dh.asBinary());
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
34 * </pre>
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
35 *
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
36 * @author Artem Tikhomirov
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
37 * @author TMate Software Ltd.
9
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 */
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 public class DigestHelper {
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 private MessageDigest sha1;
42
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
41 private byte[] digest;
9
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 public DigestHelper() {
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 }
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 private MessageDigest getSHA1() {
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 if (sha1 == null) {
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 try {
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 sha1 = MessageDigest.getInstance("SHA-1");
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 } catch (NoSuchAlgorithmException ex) {
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 // could hardly happen, JDK from Sun always has sha1.
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 ex.printStackTrace(); // FIXME log error
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 }
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 }
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 return sha1;
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 }
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57
41
858d1b2458cb Check integrity for bundle changelog. Sort nodeids when calculating hash
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 31
diff changeset
58
42
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
59 public DigestHelper sha1(Nodeid nodeid1, Nodeid nodeid2, byte[] data) {
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 42
diff changeset
60 return sha1(nodeid1.toByteArray(), nodeid2.toByteArray(), data);
41
858d1b2458cb Check integrity for bundle changelog. Sort nodeids when calculating hash
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 31
diff changeset
61 }
858d1b2458cb Check integrity for bundle changelog. Sort nodeids when calculating hash
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 31
diff changeset
62
858d1b2458cb Check integrity for bundle changelog. Sort nodeids when calculating hash
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 31
diff changeset
63 // sha1_digest(min(p1,p2) ++ max(p1,p2) ++ final_text)
42
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
64 public DigestHelper sha1(byte[] nodeidParent1, byte[] nodeidParent2, byte[] data) {
9
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 MessageDigest alg = getSHA1();
41
858d1b2458cb Check integrity for bundle changelog. Sort nodeids when calculating hash
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 31
diff changeset
66 if ((nodeidParent1[0] & 0x00FF) < (nodeidParent2[0] & 0x00FF)) {
858d1b2458cb Check integrity for bundle changelog. Sort nodeids when calculating hash
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 31
diff changeset
67 alg.update(nodeidParent1);
858d1b2458cb Check integrity for bundle changelog. Sort nodeids when calculating hash
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 31
diff changeset
68 alg.update(nodeidParent2);
858d1b2458cb Check integrity for bundle changelog. Sort nodeids when calculating hash
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 31
diff changeset
69 } else {
858d1b2458cb Check integrity for bundle changelog. Sort nodeids when calculating hash
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 31
diff changeset
70 alg.update(nodeidParent2);
858d1b2458cb Check integrity for bundle changelog. Sort nodeids when calculating hash
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 31
diff changeset
71 alg.update(nodeidParent1);
858d1b2458cb Check integrity for bundle changelog. Sort nodeids when calculating hash
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 31
diff changeset
72 }
42
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
73 digest = alg.digest(data);
9
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 assert digest.length == 20;
42
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
75 return this;
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
76 }
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
77
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
78 public String asHexString() {
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
79 if (digest == null) {
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
80 throw new IllegalStateException("Shall init with sha1() call first");
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
81 }
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
82 return toHexString(digest, 0, digest.length);
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
83 }
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
84
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
85 // by reference, be careful not to modify (or #clone() if needed)
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
86 public byte[] asBinary() {
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
87 if (digest == null) {
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
88 throw new IllegalStateException("Shall init with sha1() call first");
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
89 }
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
90 return digest;
9
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 }
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92
41
858d1b2458cb Check integrity for bundle changelog. Sort nodeids when calculating hash
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 31
diff changeset
93 // XXX perhaps, digest functions should throw an exception, as it's caller responsibility to deal with eof, etc
42
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
94 public DigestHelper sha1(InputStream is /*ByteBuffer*/) throws IOException {
9
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95 MessageDigest alg = getSHA1();
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 byte[] buf = new byte[1024];
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
97 int c;
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
98 while ((c = is.read(buf)) != -1) {
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99 alg.update(buf, 0, c);
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100 }
42
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
101 digest = alg.digest();
92c3d0920d58 Real integrity check, with exception. DigestHelper refactored to accomodate new needs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 41
diff changeset
102 return this;
9
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
104
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
105 public DigestHelper sha1(CharSequence... seq) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
106 MessageDigest alg = getSHA1();
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
107 for (CharSequence s : seq) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
108 byte[] b = s.toString().getBytes();
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
109 alg.update(b);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
110 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
111 digest = alg.digest();
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
112 return this;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
113 }
9
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
114
31
346b66add79d Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 17
diff changeset
115 public static String toHexString(byte[] data, final int offset, final int count) {
9
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
116 char[] result = new char[count << 1];
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
117 final String hexDigits = "0123456789abcdef";
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
118 final int end = offset+count;
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
119 for (int i = offset, j = 0; i < end; i++) {
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
120 result[j++] = hexDigits.charAt((data[i] >>> 4) & 0x0F);
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
121 result[j++] = hexDigits.charAt(data[i] & 0x0F);
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
122 }
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
123 return new String(result);
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
124 }
266
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
125
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
126 public static boolean ascii2bin(byte[] ascii, int offset, int len, byte[] binary) {
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
127 assert len % 2 == 0;
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
128 assert binary.length >= len >>> 1;
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
129
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
130 boolean zeroBytes = true;
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
131 for (int i = 0, j = offset; i < len >>> 1; i++) {
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
132 int b = ascii[j++] & 0xCF; // -0x30 to get decimal digit out from their char, and to uppercase if a letter
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
133 int hiNibble = b > 64 ? b - 55 : b;
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
134 b = ascii[j++] & 0xCF;
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
135 int lowNibble = b > 64 ? b - 55 : b;
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
136 if (hiNibble >= 16 || lowNibble >= 16) {
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
137 throw new IllegalArgumentException(String.format("Characters '%c%c' (%1$d and %2$d) at index %d are not valid hex digits", ascii[j-2], ascii[j-1], j-2));
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
138 }
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
139 b = (((hiNibble << 4) | lowNibble) & 0xFF);
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
140 binary[i] = (byte) b;
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
141 zeroBytes = zeroBytes && b == 0;
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
142 }
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
143 return zeroBytes;
0a2f445de774 Improve manifest parsing: reduce number of arrays instantiated for Nodeid
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
144 }
9
d6d2a630f4a6 Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
145 }