ObjectId: Convert StringIndexOutOfBounds to IllegalArgumentException
[egit/charleso.git] / org.spearce.jgit / src / org / spearce / jgit / lib / MutableObjectId.java
blob5b16345caca651deadec98daceb79e6ed3dc4cd7
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.lib;
40 import java.io.UnsupportedEncodingException;
42 import org.spearce.jgit.util.NB;
44 /**
45 * A mutable SHA-1 abstraction.
47 public class MutableObjectId extends AnyObjectId {
48 /**
49 * Empty constructor. Initialize object with default (zeros) value.
51 public MutableObjectId() {
52 super();
55 /**
56 * Copying constructor.
58 * @param src
59 * original entry, to copy id from
61 MutableObjectId(MutableObjectId src) {
62 this.w1 = src.w1;
63 this.w2 = src.w2;
64 this.w3 = src.w3;
65 this.w4 = src.w4;
66 this.w5 = src.w5;
69 /**
70 * Convert an ObjectId from raw binary representation.
72 * @param bs
73 * the raw byte buffer to read from. At least 20 bytes must be
74 * available within this byte array.
76 public void fromRaw(final byte[] bs) {
77 fromRaw(bs, 0);
80 /**
81 * Convert an ObjectId from raw binary representation.
83 * @param bs
84 * the raw byte buffer to read from. At least 20 bytes after p
85 * must be available within this byte array.
86 * @param p
87 * position to read the first byte of data from.
89 public void fromRaw(final byte[] bs, final int p) {
90 w1 = NB.decodeInt32(bs, p);
91 w2 = NB.decodeInt32(bs, p + 4);
92 w3 = NB.decodeInt32(bs, p + 8);
93 w4 = NB.decodeInt32(bs, p + 12);
94 w5 = NB.decodeInt32(bs, p + 16);
97 /**
98 * Convert an ObjectId from binary representation expressed in integers.
100 * @param ints
101 * the raw int buffer to read from. At least 5 integers must be
102 * available within this integers array.
104 public void fromRaw(final int[] ints) {
105 fromRaw(ints, 0);
109 * Convert an ObjectId from binary representation expressed in integers.
111 * @param ints
112 * the raw int buffer to read from. At least 5 integers after p
113 * must be available within this integers array.
114 * @param p
115 * position to read the first integer of data from.
118 public void fromRaw(final int[] ints, final int p) {
119 w1 = ints[p];
120 w2 = ints[p + 1];
121 w3 = ints[p + 2];
122 w4 = ints[p + 3];
123 w5 = ints[p + 4];
127 * Convert an ObjectId from hex characters (US-ASCII).
129 * @param buf
130 * the US-ASCII buffer to read from. At least 40 bytes after
131 * offset must be available within this byte array.
132 * @param offset
133 * position to read the first character from.
135 public void fromString(final byte[] buf, final int offset) {
136 fromHexString(buf, offset);
140 * Convert an ObjectId from hex characters.
142 * @param str
143 * the string to read from. Must be 40 characters long.
145 public void fromString(final String str) {
146 if (str.length() != STR_LEN)
147 throw new IllegalArgumentException("Invalid id: " + str);
148 fromHexString(Constants.encodeASCII(str), 0);
151 private void fromHexString(final byte[] bs, int p) {
152 try {
153 w1 = hexUInt32(bs, p);
154 w2 = hexUInt32(bs, p + 8);
155 w3 = hexUInt32(bs, p + 16);
156 w4 = hexUInt32(bs, p + 24);
157 w5 = hexUInt32(bs, p + 32);
158 } catch (ArrayIndexOutOfBoundsException e1) {
159 try {
160 final String str = new String(bs, p, STR_LEN, "US-ASCII");
161 throw new IllegalArgumentException("Invalid id: " + str);
162 } catch (UnsupportedEncodingException e2) {
163 throw new IllegalArgumentException("Invalid id");
164 } catch (StringIndexOutOfBoundsException e2) {
165 throw new IllegalArgumentException("Invalid id");
170 @Override
171 public ObjectId toObjectId() {
172 return new ObjectId(this);