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.

MutableObjectId.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  4. * Copyright (C) 2007-2009, Robin Rosenberg <robin.rosenberg@dewire.com>
  5. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org> and others
  6. *
  7. * This program and the accompanying materials are made available under the
  8. * terms of the Eclipse Distribution License v. 1.0 which is available at
  9. * https://www.eclipse.org/org/documents/edl-v10.php.
  10. *
  11. * SPDX-License-Identifier: BSD-3-Clause
  12. */
  13. package org.eclipse.jgit.lib;
  14. import java.text.MessageFormat;
  15. import org.eclipse.jgit.errors.InvalidObjectIdException;
  16. import org.eclipse.jgit.internal.JGitText;
  17. import org.eclipse.jgit.util.NB;
  18. import org.eclipse.jgit.util.RawParseUtils;
  19. /**
  20. * A mutable SHA-1 abstraction.
  21. */
  22. public class MutableObjectId extends AnyObjectId {
  23. /**
  24. * Empty constructor. Initialize object with default (zeros) value.
  25. */
  26. public MutableObjectId() {
  27. super();
  28. }
  29. /**
  30. * Copying constructor.
  31. *
  32. * @param src
  33. * original entry, to copy id from
  34. */
  35. MutableObjectId(MutableObjectId src) {
  36. fromObjectId(src);
  37. }
  38. /**
  39. * Set any byte in the id.
  40. *
  41. * @param index
  42. * index of the byte to set in the raw form of the ObjectId. Must
  43. * be in range [0,
  44. * {@link org.eclipse.jgit.lib.Constants#OBJECT_ID_LENGTH}).
  45. * @param value
  46. * the value of the specified byte at {@code index}. Values are
  47. * unsigned and thus are in the range [0,255] rather than the
  48. * signed byte range of [-128, 127].
  49. * @throws java.lang.ArrayIndexOutOfBoundsException
  50. * {@code index} is less than 0, equal to
  51. * {@link org.eclipse.jgit.lib.Constants#OBJECT_ID_LENGTH}, or
  52. * greater than
  53. * {@link org.eclipse.jgit.lib.Constants#OBJECT_ID_LENGTH}.
  54. */
  55. public void setByte(int index, int value) {
  56. switch (index >> 2) {
  57. case 0:
  58. w1 = set(w1, index & 3, value);
  59. break;
  60. case 1:
  61. w2 = set(w2, index & 3, value);
  62. break;
  63. case 2:
  64. w3 = set(w3, index & 3, value);
  65. break;
  66. case 3:
  67. w4 = set(w4, index & 3, value);
  68. break;
  69. case 4:
  70. w5 = set(w5, index & 3, value);
  71. break;
  72. default:
  73. throw new ArrayIndexOutOfBoundsException(index);
  74. }
  75. }
  76. private static int set(int w, int index, int value) {
  77. value &= 0xff;
  78. switch (index) {
  79. case 0:
  80. return (w & 0x00ffffff) | (value << 24);
  81. case 1:
  82. return (w & 0xff00ffff) | (value << 16);
  83. case 2:
  84. return (w & 0xffff00ff) | (value << 8);
  85. case 3:
  86. return (w & 0xffffff00) | value;
  87. default:
  88. throw new ArrayIndexOutOfBoundsException();
  89. }
  90. }
  91. /**
  92. * Make this id match {@link org.eclipse.jgit.lib.ObjectId#zeroId()}.
  93. */
  94. public void clear() {
  95. w1 = 0;
  96. w2 = 0;
  97. w3 = 0;
  98. w4 = 0;
  99. w5 = 0;
  100. }
  101. /**
  102. * Copy an ObjectId into this mutable buffer.
  103. *
  104. * @param src
  105. * the source id to copy from.
  106. */
  107. public void fromObjectId(AnyObjectId src) {
  108. this.w1 = src.w1;
  109. this.w2 = src.w2;
  110. this.w3 = src.w3;
  111. this.w4 = src.w4;
  112. this.w5 = src.w5;
  113. }
  114. /**
  115. * Convert an ObjectId from raw binary representation.
  116. *
  117. * @param bs
  118. * the raw byte buffer to read from. At least 20 bytes must be
  119. * available within this byte array.
  120. */
  121. public void fromRaw(byte[] bs) {
  122. fromRaw(bs, 0);
  123. }
  124. /**
  125. * Convert an ObjectId from raw binary representation.
  126. *
  127. * @param bs
  128. * the raw byte buffer to read from. At least 20 bytes after p
  129. * must be available within this byte array.
  130. * @param p
  131. * position to read the first byte of data from.
  132. */
  133. public void fromRaw(byte[] bs, int p) {
  134. w1 = NB.decodeInt32(bs, p);
  135. w2 = NB.decodeInt32(bs, p + 4);
  136. w3 = NB.decodeInt32(bs, p + 8);
  137. w4 = NB.decodeInt32(bs, p + 12);
  138. w5 = NB.decodeInt32(bs, p + 16);
  139. }
  140. /**
  141. * Convert an ObjectId from binary representation expressed in integers.
  142. *
  143. * @param ints
  144. * the raw int buffer to read from. At least 5 integers must be
  145. * available within this integers array.
  146. */
  147. public void fromRaw(int[] ints) {
  148. fromRaw(ints, 0);
  149. }
  150. /**
  151. * Convert an ObjectId from binary representation expressed in integers.
  152. *
  153. * @param ints
  154. * the raw int buffer to read from. At least 5 integers after p
  155. * must be available within this integers array.
  156. * @param p
  157. * position to read the first integer of data from.
  158. */
  159. public void fromRaw(int[] ints, int p) {
  160. w1 = ints[p];
  161. w2 = ints[p + 1];
  162. w3 = ints[p + 2];
  163. w4 = ints[p + 3];
  164. w5 = ints[p + 4];
  165. }
  166. /**
  167. * Convert an ObjectId from binary representation expressed in integers.
  168. *
  169. * @param a
  170. * an int.
  171. * @param b
  172. * an int.
  173. * @param c
  174. * an int.
  175. * @param d
  176. * an int.
  177. * @param e
  178. * an int.
  179. * @since 4.7
  180. */
  181. public void set(int a, int b, int c, int d, int e) {
  182. w1 = a;
  183. w2 = b;
  184. w3 = c;
  185. w4 = d;
  186. w5 = e;
  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. */
  197. public void fromString(byte[] buf, int offset) {
  198. fromHexString(buf, offset);
  199. }
  200. /**
  201. * Convert an ObjectId from hex characters.
  202. *
  203. * @param str
  204. * the string to read from. Must be 40 characters long.
  205. */
  206. public void fromString(String str) {
  207. if (str.length() != Constants.OBJECT_ID_STRING_LENGTH)
  208. throw new IllegalArgumentException(MessageFormat.format(
  209. JGitText.get().invalidId, str));
  210. fromHexString(Constants.encodeASCII(str), 0);
  211. }
  212. private void fromHexString(byte[] bs, int p) {
  213. try {
  214. w1 = RawParseUtils.parseHexInt32(bs, p);
  215. w2 = RawParseUtils.parseHexInt32(bs, p + 8);
  216. w3 = RawParseUtils.parseHexInt32(bs, p + 16);
  217. w4 = RawParseUtils.parseHexInt32(bs, p + 24);
  218. w5 = RawParseUtils.parseHexInt32(bs, p + 32);
  219. } catch (ArrayIndexOutOfBoundsException e) {
  220. InvalidObjectIdException e1 = new InvalidObjectIdException(bs, p,
  221. Constants.OBJECT_ID_STRING_LENGTH);
  222. e1.initCause(e);
  223. throw e1;
  224. }
  225. }
  226. /** {@inheritDoc} */
  227. @Override
  228. public ObjectId toObjectId() {
  229. return new ObjectId(this);
  230. }
  231. }