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

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