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