Mercurial > jhg
comparison src/org/tmatesoft/hg/core/Nodeid.java @ 148:1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Wed, 23 Feb 2011 22:36:28 +0100 |
parents | 26ad21b250e4 |
children | 71ddbf8603e8 |
comparison
equal
deleted
inserted
replaced
147:a05145db4d0c | 148:1a7a9a20e1f9 |
---|---|
39 | 39 |
40 private final byte[] binaryData; | 40 private final byte[] binaryData; |
41 | 41 |
42 /** | 42 /** |
43 * @param binaryRepresentation - array of exactly 20 bytes | 43 * @param binaryRepresentation - array of exactly 20 bytes |
44 * @param shallClone - true if array is subject to future modification and shall be copied, not referenced | 44 * @param shallClone - true if array is subject to future modification and shall be copied, not referenced |
45 * @throws IllegalArgumentException if supplied binary representation doesn't correspond to 20 bytes of sha1 digest | |
45 */ | 46 */ |
46 public Nodeid(byte[] binaryRepresentation, boolean shallClone) { | 47 public Nodeid(byte[] binaryRepresentation, boolean shallClone) { |
47 // 5 int fields => 32 bytes | 48 // 5 int fields => 32 bytes |
48 // byte[20] => 48 bytes | 49 // byte[20] => 48 bytes |
49 if (binaryRepresentation == null || binaryRepresentation.length != 20) { | 50 if (binaryRepresentation == null || binaryRepresentation.length != 20) { |
96 // copy | 97 // copy |
97 public byte[] toByteArray() { | 98 public byte[] toByteArray() { |
98 return binaryData.clone(); | 99 return binaryData.clone(); |
99 } | 100 } |
100 | 101 |
101 // primary difference with cons is handling of NULL id (this method returns constant) | 102 /** |
102 // always makes a copy of an array passed | 103 * Factory for {@link Nodeid Nodeids}. |
104 * Primary difference with cons is handling of NULL id (this method returns constant) and control over array | |
105 * duplication - this method always makes a copy of an array passed | |
106 * @param binaryRepresentation - byte array of a length at least offset + 20 | |
107 * @param offset - index in the array to start from | |
108 * @throws IllegalArgumentException when arguments don't select 20 bytes | |
109 */ | |
103 public static Nodeid fromBinary(byte[] binaryRepresentation, int offset) { | 110 public static Nodeid fromBinary(byte[] binaryRepresentation, int offset) { |
104 if (binaryRepresentation == null || binaryRepresentation.length - offset < 20) { | 111 if (binaryRepresentation == null || binaryRepresentation.length - offset < 20) { |
105 throw new IllegalArgumentException(); | 112 throw new IllegalArgumentException(); |
106 } | 113 } |
107 int i = 0; | 114 int i = 0; |
115 byte[] b = new byte[20]; // create new instance if no other reasonable guesses possible | 122 byte[] b = new byte[20]; // create new instance if no other reasonable guesses possible |
116 System.arraycopy(binaryRepresentation, offset, b, 0, 20); | 123 System.arraycopy(binaryRepresentation, offset, b, 0, 20); |
117 return new Nodeid(b, false); | 124 return new Nodeid(b, false); |
118 } | 125 } |
119 | 126 |
127 /** | |
128 * Parse encoded representation. | |
129 * | |
130 * @param asciiRepresentation - encoded form of the Nodeid. | |
131 * @return object representation | |
132 * @throws IllegalArgumentException when argument doesn't match encoded form of 20-bytes sha1 digest. | |
133 */ | |
120 public static Nodeid fromAscii(String asciiRepresentation) { | 134 public static Nodeid fromAscii(String asciiRepresentation) { |
121 if (asciiRepresentation.length() != 40) { | 135 if (asciiRepresentation.length() != 40) { |
122 throw new IllegalArgumentException(); | 136 throw new IllegalArgumentException(); |
123 } | 137 } |
124 // XXX is better impl for String possible? | 138 // XXX is better impl for String possible? |
125 return fromAscii(asciiRepresentation.getBytes(), 0, 40); | 139 return fromAscii(asciiRepresentation.getBytes(), 0, 40); |
126 } | 140 } |
141 | |
142 /** | |
143 * Parse encoded representation. Similar to {@link #fromAscii(String)}. | |
144 */ | |
127 public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) { | 145 public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) { |
128 if (length != 40) { | 146 if (length != 40) { |
129 throw new IllegalArgumentException(); | 147 throw new IllegalArgumentException(); |
130 } | 148 } |
131 byte[] data = new byte[20]; | 149 byte[] data = new byte[20]; |