View Javadoc
1   /*
2    * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3    * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.lib;
46  
47  import java.io.IOException;
48  import java.io.ObjectInputStream;
49  import java.io.ObjectOutputStream;
50  import java.io.Serializable;
51  
52  import org.eclipse.jgit.errors.InvalidObjectIdException;
53  import org.eclipse.jgit.util.NB;
54  import org.eclipse.jgit.util.RawParseUtils;
55  
56  /**
57   * A SHA-1 abstraction.
58   */
59  public class ObjectId extends AnyObjectId implements Serializable {
60  	private static final long serialVersionUID = 1L;
61  
62  	private static final ObjectId ZEROID;
63  
64  	private static final String ZEROID_STR;
65  
66  	static {
67  		ZEROID = new ObjectId(0, 0, 0, 0, 0);
68  		ZEROID_STR = ZEROID.name();
69  	}
70  
71  	/**
72  	 * Get the special all-null ObjectId.
73  	 *
74  	 * @return the all-null ObjectId, often used to stand-in for no object.
75  	 */
76  	public static final ObjectId zeroId() {
77  		return ZEROID;
78  	}
79  
80  	/**
81  	 * Test a string of characters to verify it is a hex format.
82  	 * <p>
83  	 * If true the string can be parsed with {@link #fromString(String)}.
84  	 *
85  	 * @param id
86  	 *            the string to test.
87  	 * @return true if the string can converted into an ObjectId.
88  	 */
89  	public static final boolean isId(final String id) {
90  		if (id.length() != Constants.OBJECT_ID_STRING_LENGTH)
91  			return false;
92  		try {
93  			for (int i = 0; i < Constants.OBJECT_ID_STRING_LENGTH; i++) {
94  				RawParseUtils.parseHexInt4((byte) id.charAt(i));
95  			}
96  			return true;
97  		} catch (ArrayIndexOutOfBoundsException e) {
98  			return false;
99  		}
100 	}
101 
102 	/**
103 	 * Convert an ObjectId into a hex string representation.
104 	 *
105 	 * @param i
106 	 *            the id to convert. May be null.
107 	 * @return the hex string conversion of this id's content.
108 	 */
109 	public static final String toString(final ObjectId i) {
110 		return i != null ? i.name() : ZEROID_STR;
111 	}
112 
113 	/**
114 	 * Compare two object identifier byte sequences for equality.
115 	 *
116 	 * @param firstBuffer
117 	 *            the first buffer to compare against. Must have at least 20
118 	 *            bytes from position fi through the end of the buffer.
119 	 * @param fi
120 	 *            first offset within firstBuffer to begin testing.
121 	 * @param secondBuffer
122 	 *            the second buffer to compare against. Must have at least 20
123 	 *            bytes from position si through the end of the buffer.
124 	 * @param si
125 	 *            first offset within secondBuffer to begin testing.
126 	 * @return true if the two identifiers are the same.
127 	 */
128 	public static boolean equals(final byte[] firstBuffer, final int fi,
129 			final byte[] secondBuffer, final int si) {
130 		return firstBuffer[fi] == secondBuffer[si]
131 				&& firstBuffer[fi + 1] == secondBuffer[si + 1]
132 				&& firstBuffer[fi + 2] == secondBuffer[si + 2]
133 				&& firstBuffer[fi + 3] == secondBuffer[si + 3]
134 				&& firstBuffer[fi + 4] == secondBuffer[si + 4]
135 				&& firstBuffer[fi + 5] == secondBuffer[si + 5]
136 				&& firstBuffer[fi + 6] == secondBuffer[si + 6]
137 				&& firstBuffer[fi + 7] == secondBuffer[si + 7]
138 				&& firstBuffer[fi + 8] == secondBuffer[si + 8]
139 				&& firstBuffer[fi + 9] == secondBuffer[si + 9]
140 				&& firstBuffer[fi + 10] == secondBuffer[si + 10]
141 				&& firstBuffer[fi + 11] == secondBuffer[si + 11]
142 				&& firstBuffer[fi + 12] == secondBuffer[si + 12]
143 				&& firstBuffer[fi + 13] == secondBuffer[si + 13]
144 				&& firstBuffer[fi + 14] == secondBuffer[si + 14]
145 				&& firstBuffer[fi + 15] == secondBuffer[si + 15]
146 				&& firstBuffer[fi + 16] == secondBuffer[si + 16]
147 				&& firstBuffer[fi + 17] == secondBuffer[si + 17]
148 				&& firstBuffer[fi + 18] == secondBuffer[si + 18]
149 				&& firstBuffer[fi + 19] == secondBuffer[si + 19];
150 	}
151 
152 	/**
153 	 * Convert an ObjectId from raw binary representation.
154 	 *
155 	 * @param bs
156 	 *            the raw byte buffer to read from. At least 20 bytes must be
157 	 *            available within this byte array.
158 	 * @return the converted object id.
159 	 */
160 	public static final ObjectId fromRaw(final byte[] bs) {
161 		return fromRaw(bs, 0);
162 	}
163 
164 	/**
165 	 * Convert an ObjectId from raw binary representation.
166 	 *
167 	 * @param bs
168 	 *            the raw byte buffer to read from. At least 20 bytes after p
169 	 *            must be available within this byte array.
170 	 * @param p
171 	 *            position to read the first byte of data from.
172 	 * @return the converted object id.
173 	 */
174 	public static final ObjectId fromRaw(final byte[] bs, final int p) {
175 		final int a = NB.decodeInt32(bs, p);
176 		final int b = NB.decodeInt32(bs, p + 4);
177 		final int c = NB.decodeInt32(bs, p + 8);
178 		final int d = NB.decodeInt32(bs, p + 12);
179 		final int e = NB.decodeInt32(bs, p + 16);
180 		return new ObjectId(a, b, c, d, e);
181 	}
182 
183 	/**
184 	 * Convert an ObjectId from raw binary representation.
185 	 *
186 	 * @param is
187 	 *            the raw integers buffer to read from. At least 5 integers must
188 	 *            be available within this int array.
189 	 * @return the converted object id.
190 	 */
191 	public static final ObjectId fromRaw(final int[] is) {
192 		return fromRaw(is, 0);
193 	}
194 
195 	/**
196 	 * Convert an ObjectId from raw binary representation.
197 	 *
198 	 * @param is
199 	 *            the raw integers buffer to read from. At least 5 integers
200 	 *            after p must be available within this int array.
201 	 * @param p
202 	 *            position to read the first integer of data from.
203 	 * @return the converted object id.
204 	 */
205 	public static final ObjectId fromRaw(final int[] is, final int p) {
206 		return new ObjectId(is[p], is[p + 1], is[p + 2], is[p + 3], is[p + 4]);
207 	}
208 
209 	/**
210 	 * Convert an ObjectId from hex characters (US-ASCII).
211 	 *
212 	 * @param buf
213 	 *            the US-ASCII buffer to read from. At least 40 bytes after
214 	 *            offset must be available within this byte array.
215 	 * @param offset
216 	 *            position to read the first character from.
217 	 * @return the converted object id.
218 	 */
219 	public static final ObjectId fromString(final byte[] buf, final int offset) {
220 		return fromHexString(buf, offset);
221 	}
222 
223 	/**
224 	 * Convert an ObjectId from hex characters.
225 	 *
226 	 * @param str
227 	 *            the string to read from. Must be 40 characters long.
228 	 * @return the converted object id.
229 	 */
230 	public static ObjectId fromString(final String str) {
231 		if (str.length() != Constants.OBJECT_ID_STRING_LENGTH) {
232 			throw new InvalidObjectIdException(str);
233 		}
234 		return fromHexString(Constants.encodeASCII(str), 0);
235 	}
236 
237 	private static final ObjectId fromHexString(final byte[] bs, int p) {
238 		try {
239 			final int a = RawParseUtils.parseHexInt32(bs, p);
240 			final int b = RawParseUtils.parseHexInt32(bs, p + 8);
241 			final int c = RawParseUtils.parseHexInt32(bs, p + 16);
242 			final int d = RawParseUtils.parseHexInt32(bs, p + 24);
243 			final int e = RawParseUtils.parseHexInt32(bs, p + 32);
244 			return new ObjectId(a, b, c, d, e);
245 		} catch (ArrayIndexOutOfBoundsException e1) {
246 			throw new InvalidObjectIdException(bs, p,
247 					Constants.OBJECT_ID_STRING_LENGTH);
248 		}
249 	}
250 
251 	/**
252 	 * Construct an ObjectId from 160 bits provided in 5 words.
253 	 *
254 	 * @param new_1
255 	 * @param new_2
256 	 * @param new_3
257 	 * @param new_4
258 	 * @param new_5
259 	 * @since 4.7
260 	 */
261 	public ObjectId(int new_1, int new_2, int new_3, int new_4, int new_5) {
262 		w1 = new_1;
263 		w2 = new_2;
264 		w3 = new_3;
265 		w4 = new_4;
266 		w5 = new_5;
267 	}
268 
269 	/**
270 	 * Initialize this instance by copying another existing ObjectId.
271 	 * <p>
272 	 * This constructor is mostly useful for subclasses who want to extend an
273 	 * ObjectId with more properties, but initialize from an existing ObjectId
274 	 * instance acquired by other means.
275 	 *
276 	 * @param src
277 	 *            another already parsed ObjectId to copy the value out of.
278 	 */
279 	protected ObjectId(final AnyObjectId src) {
280 		w1 = src.w1;
281 		w2 = src.w2;
282 		w3 = src.w3;
283 		w4 = src.w4;
284 		w5 = src.w5;
285 	}
286 
287 	@Override
288 	public ObjectId toObjectId() {
289 		return this;
290 	}
291 
292 	private void writeObject(ObjectOutputStream os) throws IOException {
293 		os.writeInt(w1);
294 		os.writeInt(w2);
295 		os.writeInt(w3);
296 		os.writeInt(w4);
297 		os.writeInt(w5);
298 	}
299 
300 	private void readObject(ObjectInputStream ois) throws IOException {
301 		w1 = ois.readInt();
302 		w2 = ois.readInt();
303 		w3 = ois.readInt();
304 		w4 = ois.readInt();
305 		w5 = ois.readInt();
306 	}
307 }