Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

MutableLongObjectId.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (C) 2015, Matthias Sohn <matthias.sohn@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.lfs.lib;
  11. import java.text.MessageFormat;
  12. import org.eclipse.jgit.lfs.errors.InvalidLongObjectIdException;
  13. import org.eclipse.jgit.lfs.internal.LfsText;
  14. import org.eclipse.jgit.util.NB;
  15. import org.eclipse.jgit.util.RawParseUtils;
  16. /**
  17. * A mutable SHA-256 abstraction.
  18. *
  19. * Ported to SHA-256 from {@link org.eclipse.jgit.lib.MutableObjectId}
  20. *
  21. * @since 4.3
  22. */
  23. public class MutableLongObjectId extends AnyLongObjectId {
  24. /**
  25. * Empty constructor. Initialize object with default (zeros) value.
  26. */
  27. public MutableLongObjectId() {
  28. super();
  29. }
  30. /**
  31. * Copying constructor.
  32. *
  33. * @param src
  34. * original entry, to copy id from
  35. */
  36. MutableLongObjectId(MutableLongObjectId src) {
  37. fromObjectId(src);
  38. }
  39. /**
  40. * Set any byte in the id.
  41. *
  42. * @param index
  43. * index of the byte to set in the raw form of the ObjectId. Must
  44. * be in range [0,
  45. * {@link org.eclipse.jgit.lfs.lib.Constants#LONG_OBJECT_ID_LENGTH}).
  46. * @param value
  47. * the value of the specified byte at {@code index}. Values are
  48. * unsigned and thus are in the range [0,255] rather than the
  49. * signed byte range of [-128, 127].
  50. * @throws java.lang.ArrayIndexOutOfBoundsException
  51. * {@code index} is less than 0, equal to
  52. * {@link org.eclipse.jgit.lfs.lib.Constants#LONG_OBJECT_ID_LENGTH},
  53. * or greater than
  54. * {@link org.eclipse.jgit.lfs.lib.Constants#LONG_OBJECT_ID_LENGTH}.
  55. */
  56. public void setByte(int index, int value) {
  57. switch (index >> 3) {
  58. case 0:
  59. w1 = set(w1, index & 7, value);
  60. break;
  61. case 1:
  62. w2 = set(w2, index & 7, value);
  63. break;
  64. case 2:
  65. w3 = set(w3, index & 7, value);
  66. break;
  67. case 3:
  68. w4 = set(w4, index & 7, value);
  69. break;
  70. default:
  71. throw new ArrayIndexOutOfBoundsException(index);
  72. }
  73. }
  74. private static long set(long w, int index, long value) {
  75. value &= 0xff;
  76. switch (index) {
  77. case 0:
  78. return (w & 0x00ffffffffffffffL) | (value << 56);
  79. case 1:
  80. return (w & 0xff00ffffffffffffL) | (value << 48);
  81. case 2:
  82. return (w & 0xffff00ffffffffffL) | (value << 40);
  83. case 3:
  84. return (w & 0xffffff00ffffffffL) | (value << 32);
  85. case 4:
  86. return (w & 0xffffffff00ffffffL) | (value << 24);
  87. case 5:
  88. return (w & 0xffffffffff00ffffL) | (value << 16);
  89. case 6:
  90. return (w & 0xffffffffffff00ffL) | (value << 8);
  91. case 7:
  92. return (w & 0xffffffffffffff00L) | value;
  93. default:
  94. throw new ArrayIndexOutOfBoundsException();
  95. }
  96. }
  97. /**
  98. * Make this id match
  99. * {@link org.eclipse.jgit.lfs.lib.LongObjectId#zeroId()}.
  100. */
  101. public void clear() {
  102. w1 = 0;
  103. w2 = 0;
  104. w3 = 0;
  105. w4 = 0;
  106. }
  107. /**
  108. * Copy a LongObjectId into this mutable buffer.
  109. *
  110. * @param src
  111. * the source id to copy from.
  112. */
  113. public void fromObjectId(AnyLongObjectId src) {
  114. this.w1 = src.w1;
  115. this.w2 = src.w2;
  116. this.w3 = src.w3;
  117. this.w4 = src.w4;
  118. }
  119. /**
  120. * Convert a LongObjectId from raw binary representation.
  121. *
  122. * @param bs
  123. * the raw byte buffer to read from. At least 32 bytes must be
  124. * available within this byte array.
  125. */
  126. public void fromRaw(byte[] bs) {
  127. fromRaw(bs, 0);
  128. }
  129. /**
  130. * Convert a LongObjectId from raw binary representation.
  131. *
  132. * @param bs
  133. * the raw byte buffer to read from. At least 32 bytes after p
  134. * must be available within this byte array.
  135. * @param p
  136. * position to read the first byte of data from.
  137. */
  138. public void fromRaw(byte[] bs, int p) {
  139. w1 = NB.decodeInt64(bs, p);
  140. w2 = NB.decodeInt64(bs, p + 8);
  141. w3 = NB.decodeInt64(bs, p + 16);
  142. w4 = NB.decodeInt64(bs, p + 24);
  143. }
  144. /**
  145. * Convert a LongObjectId from binary representation expressed in integers.
  146. *
  147. * @param longs
  148. * the raw long buffer to read from. At least 4 longs must be
  149. * available within this longs array.
  150. */
  151. public void fromRaw(long[] longs) {
  152. fromRaw(longs, 0);
  153. }
  154. /**
  155. * Convert a LongObjectId from binary representation expressed in longs.
  156. *
  157. * @param longs
  158. * the raw int buffer to read from. At least 4 longs after p must
  159. * be available within this longs array.
  160. * @param p
  161. * position to read the first integer of data from.
  162. */
  163. public void fromRaw(long[] longs, int p) {
  164. w1 = longs[p];
  165. w2 = longs[p + 1];
  166. w3 = longs[p + 2];
  167. w4 = longs[p + 3];
  168. }
  169. /**
  170. * Convert a LongObjectId from hex characters (US-ASCII).
  171. *
  172. * @param buf
  173. * the US-ASCII buffer to read from. At least 32 bytes after
  174. * offset must be available within this byte array.
  175. * @param offset
  176. * position to read the first character from.
  177. */
  178. public void fromString(byte[] buf, int offset) {
  179. fromHexString(buf, offset);
  180. }
  181. /**
  182. * Convert a LongObjectId from hex characters.
  183. *
  184. * @param str
  185. * the string to read from. Must be 64 characters long.
  186. */
  187. public void fromString(String str) {
  188. if (str.length() != Constants.LONG_OBJECT_ID_STRING_LENGTH)
  189. throw new IllegalArgumentException(
  190. MessageFormat.format(LfsText.get().invalidLongId, str));
  191. fromHexString(org.eclipse.jgit.lib.Constants.encodeASCII(str), 0);
  192. }
  193. private void fromHexString(byte[] bs, int p) {
  194. try {
  195. w1 = RawParseUtils.parseHexInt64(bs, p);
  196. w2 = RawParseUtils.parseHexInt64(bs, p + 16);
  197. w3 = RawParseUtils.parseHexInt64(bs, p + 32);
  198. w4 = RawParseUtils.parseHexInt64(bs, p + 48);
  199. } catch (ArrayIndexOutOfBoundsException e) {
  200. InvalidLongObjectIdException e1 = new InvalidLongObjectIdException(
  201. bs, p, Constants.LONG_OBJECT_ID_STRING_LENGTH);
  202. e1.initCause(e);
  203. throw e1;
  204. }
  205. }
  206. /** {@inheritDoc} */
  207. @Override
  208. public LongObjectId toObjectId() {
  209. return new LongObjectId(this);
  210. }
  211. }