changeset 500:465316bf97e8

Tailored subclass of IAE for malformed Nodeids:HgBadNodeidFormatException
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 29 Oct 2012 18:16:21 +0100
parents 899a1b68ef03
children d2f6ab541330
files src/org/tmatesoft/hg/core/HgBadNodeidFormatException.java src/org/tmatesoft/hg/core/Nodeid.java
diffstat 2 files changed, 45 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/tmatesoft/hg/core/HgBadNodeidFormatException.java	Mon Oct 29 18:16:21 2012 +0100
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2012 TMate Software Ltd
+ *  
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * For information on how to redistribute this software under
+ * the terms of a license other than GNU General Public License
+ * contact TMate Software at support@hg4j.com
+ */
+package org.tmatesoft.hg.core;
+
+/**
+ * 
+ * @author Artem Tikhomirov
+ * @author TMate Software Ltd.
+ */
+@SuppressWarnings("serial")
+public class HgBadNodeidFormatException extends IllegalArgumentException {
+	public HgBadNodeidFormatException(String message) {
+		super(message);
+	}
+}
--- a/src/org/tmatesoft/hg/core/Nodeid.java	Fri Oct 26 18:21:50 2012 +0200
+++ b/src/org/tmatesoft/hg/core/Nodeid.java	Mon Oct 29 18:16:21 2012 +0100
@@ -44,13 +44,13 @@
 	/**
 	 * @param binaryRepresentation - array of exactly 20 bytes
 	 * @param shallClone - true if array is subject to future modification and shall be copied, not referenced
-	 * @throws IllegalArgumentException if supplied binary representation doesn't correspond to 20 bytes of sha1 digest 
+	 * @throws HgBadNodeidFormatException custom {@link IllegalArgumentException} subclass if supplied binary representation doesn't correspond to 20 bytes of sha1 digest 
 	 */
 	public Nodeid(byte[] binaryRepresentation, boolean shallClone) {
 		// 5 int fields => 32 bytes
 		// byte[20] => 48 bytes (16 bytes is Nodeid with one field, 32 bytes for byte[20] 
 		if (binaryRepresentation == null || binaryRepresentation.length != 20) {
-			throw new IllegalArgumentException();
+			throw new HgBadNodeidFormatException(String.format("Bad value: %s", String.valueOf(binaryRepresentation)));
 		}
 		/*
 		 * byte[].clone() is not reflected when ran with -agentlib:hprof=heap=sites
@@ -140,11 +140,11 @@
 	 * duplication - this method always makes a copy of an array passed
 	 * @param binaryRepresentation - byte array of a length at least offset + 20
 	 * @param offset - index in the array to start from
-	 * @throws IllegalArgumentException when arguments don't select 20 bytes
+	 * @throws HgBadNodeidFormatException custom {@link IllegalArgumentException} subclass when arguments don't select 20 bytes
 	 */
 	public static Nodeid fromBinary(byte[] binaryRepresentation, int offset) {
 		if (binaryRepresentation == null || binaryRepresentation.length - offset < 20) {
-			throw new IllegalArgumentException();
+			throw new HgBadNodeidFormatException(String.format("Bad value: %s", String.valueOf(binaryRepresentation)));
 		}
 		int i = 0;
 		while (i < 20 && binaryRepresentation[offset+i] == 0) i++;
@@ -164,11 +164,11 @@
 	 * 
 	 * @param asciiRepresentation - encoded form of the Nodeid.
 	 * @return object representation
-	 * @throws IllegalArgumentException when argument doesn't match encoded form of 20-bytes sha1 digest. 
+	 * @throws HgBadNodeidFormatException custom {@link IllegalArgumentException} subclass when argument doesn't match encoded form of 20-bytes sha1 digest. 
 	 */
 	public static Nodeid fromAscii(String asciiRepresentation) {
 		if (asciiRepresentation.length() != 40) {
-			throw new IllegalArgumentException();
+			throw new HgBadNodeidFormatException(String.format("Bad value: %s", asciiRepresentation));
 		}
 		// XXX is better impl for String possible?
 		return fromAscii(asciiRepresentation.toCharArray(), 0, 40);
@@ -176,17 +176,22 @@
 	
 	/**
 	 * Parse encoded representation. Similar to {@link #fromAscii(String)}.
+	 * @throws HgBadNodeidFormatException custom {@link IllegalArgumentException} subclass when bytes are not hex digits or number of bytes != 40 (160 bits) 
 	 */
 	public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) {
 		if (length != 40) {
 			throw new IllegalArgumentException();
 		}
-		byte[] data = new byte[20];
-		boolean zeroBytes = DigestHelper.ascii2bin(asciiRepresentation, offset, length, data);
-		if (zeroBytes) {
-			return NULL;
+		try {
+			byte[] data = new byte[20];
+			boolean zeroBytes = DigestHelper.ascii2bin(asciiRepresentation, offset, length, data);
+			if (zeroBytes) {
+				return NULL;
+			}
+			return new Nodeid(data, false);
+		} catch (IllegalArgumentException ex) {
+			throw new HgBadNodeidFormatException(ex.getMessage());
 		}
-		return new Nodeid(data, false);
 	}
 	
 	public static Nodeid fromAscii(char[] asciiRepresentation, int offset, int length) {