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.

LongObjectId.java 10KB

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