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.0KB

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