You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ObjectId.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. package org.eclipse.jgit.lib;
  45. import java.io.IOException;
  46. import java.io.ObjectInputStream;
  47. import java.io.ObjectOutputStream;
  48. import java.io.Serializable;
  49. import org.eclipse.jgit.errors.InvalidObjectIdException;
  50. import org.eclipse.jgit.util.NB;
  51. import org.eclipse.jgit.util.RawParseUtils;
  52. /**
  53. * A SHA-1 abstraction.
  54. */
  55. public class ObjectId extends AnyObjectId implements Serializable {
  56. private static final long serialVersionUID = 1L;
  57. private static final ObjectId ZEROID;
  58. private static final String ZEROID_STR;
  59. static {
  60. ZEROID = new ObjectId(0, 0, 0, 0, 0);
  61. ZEROID_STR = ZEROID.name();
  62. }
  63. /**
  64. * Get the special all-null ObjectId.
  65. *
  66. * @return the all-null ObjectId, often used to stand-in for no object.
  67. */
  68. public static final ObjectId zeroId() {
  69. return ZEROID;
  70. }
  71. /**
  72. * Test a string of characters to verify it is a hex format.
  73. * <p>
  74. * If true the string can be parsed with {@link #fromString(String)}.
  75. *
  76. * @param id
  77. * the string to test.
  78. * @return true if the string can converted into an ObjectId.
  79. */
  80. public static final boolean isId(final String id) {
  81. if (id.length() != Constants.OBJECT_ID_STRING_LENGTH)
  82. return false;
  83. try {
  84. for (int i = 0; i < Constants.OBJECT_ID_STRING_LENGTH; i++) {
  85. RawParseUtils.parseHexInt4((byte) id.charAt(i));
  86. }
  87. return true;
  88. } catch (ArrayIndexOutOfBoundsException e) {
  89. return false;
  90. }
  91. }
  92. /**
  93. * Convert an ObjectId into a hex string representation.
  94. *
  95. * @param i
  96. * the id to convert. May be null.
  97. * @return the hex string conversion of this id's content.
  98. */
  99. public static final String toString(final ObjectId i) {
  100. return i != null ? i.name() : ZEROID_STR;
  101. }
  102. /**
  103. * Compare two object identifier byte sequences for equality.
  104. *
  105. * @param firstBuffer
  106. * the first buffer to compare against. Must have at least 20
  107. * bytes from position fi through the end of the buffer.
  108. * @param fi
  109. * first offset within firstBuffer to begin testing.
  110. * @param secondBuffer
  111. * the second buffer to compare against. Must have at least 20
  112. * bytes from position si through the end of the buffer.
  113. * @param si
  114. * first offset within secondBuffer to begin testing.
  115. * @return true if the two identifiers are the same.
  116. */
  117. public static boolean equals(final byte[] firstBuffer, final int fi,
  118. final byte[] secondBuffer, final int si) {
  119. return firstBuffer[fi] == secondBuffer[si]
  120. && firstBuffer[fi + 1] == secondBuffer[si + 1]
  121. && firstBuffer[fi + 2] == secondBuffer[si + 2]
  122. && firstBuffer[fi + 3] == secondBuffer[si + 3]
  123. && firstBuffer[fi + 4] == secondBuffer[si + 4]
  124. && firstBuffer[fi + 5] == secondBuffer[si + 5]
  125. && firstBuffer[fi + 6] == secondBuffer[si + 6]
  126. && firstBuffer[fi + 7] == secondBuffer[si + 7]
  127. && firstBuffer[fi + 8] == secondBuffer[si + 8]
  128. && firstBuffer[fi + 9] == secondBuffer[si + 9]
  129. && firstBuffer[fi + 10] == secondBuffer[si + 10]
  130. && firstBuffer[fi + 11] == secondBuffer[si + 11]
  131. && firstBuffer[fi + 12] == secondBuffer[si + 12]
  132. && firstBuffer[fi + 13] == secondBuffer[si + 13]
  133. && firstBuffer[fi + 14] == secondBuffer[si + 14]
  134. && firstBuffer[fi + 15] == secondBuffer[si + 15]
  135. && firstBuffer[fi + 16] == secondBuffer[si + 16]
  136. && firstBuffer[fi + 17] == secondBuffer[si + 17]
  137. && firstBuffer[fi + 18] == secondBuffer[si + 18]
  138. && firstBuffer[fi + 19] == secondBuffer[si + 19];
  139. }
  140. /**
  141. * Convert an ObjectId from raw binary representation.
  142. *
  143. * @param bs
  144. * the raw byte buffer to read from. At least 20 bytes must be
  145. * available within this byte array.
  146. * @return the converted object id.
  147. */
  148. public static final ObjectId fromRaw(final byte[] bs) {
  149. return fromRaw(bs, 0);
  150. }
  151. /**
  152. * Convert an ObjectId from raw binary representation.
  153. *
  154. * @param bs
  155. * the raw byte buffer to read from. At least 20 bytes after p
  156. * must be available within this byte array.
  157. * @param p
  158. * position to read the first byte of data from.
  159. * @return the converted object id.
  160. */
  161. public static final ObjectId fromRaw(final byte[] bs, final int p) {
  162. final int a = NB.decodeInt32(bs, p);
  163. final int b = NB.decodeInt32(bs, p + 4);
  164. final int c = NB.decodeInt32(bs, p + 8);
  165. final int d = NB.decodeInt32(bs, p + 12);
  166. final int e = NB.decodeInt32(bs, p + 16);
  167. return new ObjectId(a, b, c, d, e);
  168. }
  169. /**
  170. * Convert an ObjectId from raw binary representation.
  171. *
  172. * @param is
  173. * the raw integers buffer to read from. At least 5 integers must
  174. * be available within this int array.
  175. * @return the converted object id.
  176. */
  177. public static final ObjectId fromRaw(final int[] is) {
  178. return fromRaw(is, 0);
  179. }
  180. /**
  181. * Convert an ObjectId from raw binary representation.
  182. *
  183. * @param is
  184. * the raw integers buffer to read from. At least 5 integers
  185. * after p must be available within this int array.
  186. * @param p
  187. * position to read the first integer of data from.
  188. * @return the converted object id.
  189. */
  190. public static final ObjectId fromRaw(final int[] is, final int p) {
  191. return new ObjectId(is[p], is[p + 1], is[p + 2], is[p + 3], is[p + 4]);
  192. }
  193. /**
  194. * Convert an ObjectId from hex characters (US-ASCII).
  195. *
  196. * @param buf
  197. * the US-ASCII buffer to read from. At least 40 bytes after
  198. * offset must be available within this byte array.
  199. * @param offset
  200. * position to read the first character from.
  201. * @return the converted object id.
  202. */
  203. public static final ObjectId fromString(final byte[] buf, final int offset) {
  204. return fromHexString(buf, offset);
  205. }
  206. /**
  207. * Convert an ObjectId from hex characters.
  208. *
  209. * @param str
  210. * the string to read from. Must be 40 characters long.
  211. * @return the converted object id.
  212. */
  213. public static ObjectId fromString(final String str) {
  214. if (str.length() != Constants.OBJECT_ID_STRING_LENGTH) {
  215. throw new InvalidObjectIdException(str);
  216. }
  217. return fromHexString(Constants.encodeASCII(str), 0);
  218. }
  219. private static final ObjectId fromHexString(final byte[] bs, int p) {
  220. try {
  221. final int a = RawParseUtils.parseHexInt32(bs, p);
  222. final int b = RawParseUtils.parseHexInt32(bs, p + 8);
  223. final int c = RawParseUtils.parseHexInt32(bs, p + 16);
  224. final int d = RawParseUtils.parseHexInt32(bs, p + 24);
  225. final int e = RawParseUtils.parseHexInt32(bs, p + 32);
  226. return new ObjectId(a, b, c, d, e);
  227. } catch (ArrayIndexOutOfBoundsException e1) {
  228. throw new InvalidObjectIdException(bs, p,
  229. Constants.OBJECT_ID_STRING_LENGTH);
  230. }
  231. }
  232. ObjectId(final int new_1, final int new_2, final int new_3,
  233. final int new_4, final int new_5) {
  234. w1 = new_1;
  235. w2 = new_2;
  236. w3 = new_3;
  237. w4 = new_4;
  238. w5 = new_5;
  239. }
  240. /**
  241. * Initialize this instance by copying another existing ObjectId.
  242. * <p>
  243. * This constructor is mostly useful for subclasses who want to extend an
  244. * ObjectId with more properties, but initialize from an existing ObjectId
  245. * instance acquired by other means.
  246. *
  247. * @param src
  248. * another already parsed ObjectId to copy the value out of.
  249. */
  250. protected ObjectId(final AnyObjectId src) {
  251. w1 = src.w1;
  252. w2 = src.w2;
  253. w3 = src.w3;
  254. w4 = src.w4;
  255. w5 = src.w5;
  256. }
  257. @Override
  258. public ObjectId toObjectId() {
  259. return this;
  260. }
  261. private void writeObject(ObjectOutputStream os) throws IOException {
  262. os.writeInt(w1);
  263. os.writeInt(w2);
  264. os.writeInt(w3);
  265. os.writeInt(w4);
  266. os.writeInt(w5);
  267. }
  268. private void readObject(ObjectInputStream ois) throws IOException {
  269. w1 = ois.readInt();
  270. w2 = ois.readInt();
  271. w3 = ois.readInt();
  272. w4 = ois.readInt();
  273. w5 = ois.readInt();
  274. }
  275. }