comparison src/org/tmatesoft/hg/core/Nodeid.java @ 535:d9c07e1432c4

Issue 42: tolerate lines in .hgtags that do not conform to its format specification
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 05 Feb 2013 15:54:37 +0100
parents 465316bf97e8
children d29d9dc6c128
comparison
equal deleted inserted replaced
534:243202f1bda5 535:d9c07e1432c4
1 /* 1 /*
2 * Copyright (c) 2010-2011 TMate Software Ltd 2 * Copyright (c) 2010-2013 TMate Software Ltd
3 * 3 *
4 * This program is free software; you can redistribute it and/or modify 4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by 5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License. 6 * the Free Software Foundation; version 2 of the License.
7 * 7 *
164 * 164 *
165 * @param asciiRepresentation - encoded form of the Nodeid. 165 * @param asciiRepresentation - encoded form of the Nodeid.
166 * @return object representation 166 * @return object representation
167 * @throws HgBadNodeidFormatException custom {@link IllegalArgumentException} subclass when argument doesn't match encoded form of 20-bytes sha1 digest. 167 * @throws HgBadNodeidFormatException custom {@link IllegalArgumentException} subclass when argument doesn't match encoded form of 20-bytes sha1 digest.
168 */ 168 */
169 public static Nodeid fromAscii(String asciiRepresentation) { 169 public static Nodeid fromAscii(String asciiRepresentation) throws HgBadNodeidFormatException {
170 if (asciiRepresentation.length() != 40) { 170 if (asciiRepresentation.length() != 40) {
171 throw new HgBadNodeidFormatException(String.format("Bad value: %s", asciiRepresentation)); 171 throw new HgBadNodeidFormatException(String.format("Bad value: %s", asciiRepresentation));
172 } 172 }
173 // XXX is better impl for String possible? 173 // XXX is better impl for String possible?
174 return fromAscii(asciiRepresentation.toCharArray(), 0, 40); 174 return fromAscii(asciiRepresentation.toCharArray(), 0, 40);
176 176
177 /** 177 /**
178 * Parse encoded representation. Similar to {@link #fromAscii(String)}. 178 * Parse encoded representation. Similar to {@link #fromAscii(String)}.
179 * @throws HgBadNodeidFormatException custom {@link IllegalArgumentException} subclass when bytes are not hex digits or number of bytes != 40 (160 bits) 179 * @throws HgBadNodeidFormatException custom {@link IllegalArgumentException} subclass when bytes are not hex digits or number of bytes != 40 (160 bits)
180 */ 180 */
181 public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) { 181 public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) throws HgBadNodeidFormatException {
182 if (length != 40) { 182 if (length != 40) {
183 throw new IllegalArgumentException(); 183 throw new HgBadNodeidFormatException(String.format("Expected 40 hex characters for nodeid, not %d", length));
184 } 184 }
185 try { 185 try {
186 byte[] data = new byte[20]; 186 byte[] data = new byte[20];
187 boolean zeroBytes = DigestHelper.ascii2bin(asciiRepresentation, offset, length, data); 187 boolean zeroBytes = DigestHelper.ascii2bin(asciiRepresentation, offset, length, data);
188 if (zeroBytes) { 188 if (zeroBytes) {
189 return NULL; 189 return NULL;
190 } 190 }
191 return new Nodeid(data, false); 191 return new Nodeid(data, false);
192 } catch (HgBadNodeidFormatException ex) {
193 throw ex;
192 } catch (IllegalArgumentException ex) { 194 } catch (IllegalArgumentException ex) {
193 throw new HgBadNodeidFormatException(ex.getMessage()); 195 throw new HgBadNodeidFormatException(ex.getMessage());
194 } 196 }
195 } 197 }
196 198