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.

MutableObjectId.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.lib;
  47. import java.text.MessageFormat;
  48. import org.eclipse.jgit.errors.InvalidObjectIdException;
  49. import org.eclipse.jgit.internal.JGitText;
  50. import org.eclipse.jgit.util.NB;
  51. import org.eclipse.jgit.util.RawParseUtils;
  52. /**
  53. * A mutable SHA-1 abstraction.
  54. */
  55. public class MutableObjectId extends AnyObjectId {
  56. /**
  57. * Empty constructor. Initialize object with default (zeros) value.
  58. */
  59. public MutableObjectId() {
  60. super();
  61. }
  62. /**
  63. * Copying constructor.
  64. *
  65. * @param src
  66. * original entry, to copy id from
  67. */
  68. MutableObjectId(MutableObjectId src) {
  69. fromObjectId(src);
  70. }
  71. /**
  72. * Set any byte in the id.
  73. *
  74. * @param index
  75. * index of the byte to set in the raw form of the ObjectId. Must
  76. * be in range [0,
  77. * {@link org.eclipse.jgit.lib.Constants#OBJECT_ID_LENGTH}).
  78. * @param value
  79. * the value of the specified byte at {@code index}. Values are
  80. * unsigned and thus are in the range [0,255] rather than the
  81. * signed byte range of [-128, 127].
  82. * @throws java.lang.ArrayIndexOutOfBoundsException
  83. * {@code index} is less than 0, equal to
  84. * {@link org.eclipse.jgit.lib.Constants#OBJECT_ID_LENGTH}, or
  85. * greater than
  86. * {@link org.eclipse.jgit.lib.Constants#OBJECT_ID_LENGTH}.
  87. */
  88. public void setByte(int index, int value) {
  89. switch (index >> 2) {
  90. case 0:
  91. w1 = set(w1, index & 3, value);
  92. break;
  93. case 1:
  94. w2 = set(w2, index & 3, value);
  95. break;
  96. case 2:
  97. w3 = set(w3, index & 3, value);
  98. break;
  99. case 3:
  100. w4 = set(w4, index & 3, value);
  101. break;
  102. case 4:
  103. w5 = set(w5, index & 3, value);
  104. break;
  105. default:
  106. throw new ArrayIndexOutOfBoundsException(index);
  107. }
  108. }
  109. private static int set(int w, int index, int value) {
  110. value &= 0xff;
  111. switch (index) {
  112. case 0:
  113. return (w & 0x00ffffff) | (value << 24);
  114. case 1:
  115. return (w & 0xff00ffff) | (value << 16);
  116. case 2:
  117. return (w & 0xffff00ff) | (value << 8);
  118. case 3:
  119. return (w & 0xffffff00) | value;
  120. default:
  121. throw new ArrayIndexOutOfBoundsException();
  122. }
  123. }
  124. /**
  125. * Make this id match {@link org.eclipse.jgit.lib.ObjectId#zeroId()}.
  126. */
  127. public void clear() {
  128. w1 = 0;
  129. w2 = 0;
  130. w3 = 0;
  131. w4 = 0;
  132. w5 = 0;
  133. }
  134. /**
  135. * Copy an ObjectId into this mutable buffer.
  136. *
  137. * @param src
  138. * the source id to copy from.
  139. */
  140. public void fromObjectId(AnyObjectId src) {
  141. this.w1 = src.w1;
  142. this.w2 = src.w2;
  143. this.w3 = src.w3;
  144. this.w4 = src.w4;
  145. this.w5 = src.w5;
  146. }
  147. /**
  148. * Convert an ObjectId from raw binary representation.
  149. *
  150. * @param bs
  151. * the raw byte buffer to read from. At least 20 bytes must be
  152. * available within this byte array.
  153. */
  154. public void fromRaw(byte[] bs) {
  155. fromRaw(bs, 0);
  156. }
  157. /**
  158. * Convert an ObjectId from raw binary representation.
  159. *
  160. * @param bs
  161. * the raw byte buffer to read from. At least 20 bytes after p
  162. * must be available within this byte array.
  163. * @param p
  164. * position to read the first byte of data from.
  165. */
  166. public void fromRaw(byte[] bs, int p) {
  167. w1 = NB.decodeInt32(bs, p);
  168. w2 = NB.decodeInt32(bs, p + 4);
  169. w3 = NB.decodeInt32(bs, p + 8);
  170. w4 = NB.decodeInt32(bs, p + 12);
  171. w5 = NB.decodeInt32(bs, p + 16);
  172. }
  173. /**
  174. * Convert an ObjectId from binary representation expressed in integers.
  175. *
  176. * @param ints
  177. * the raw int buffer to read from. At least 5 integers must be
  178. * available within this integers array.
  179. */
  180. public void fromRaw(int[] ints) {
  181. fromRaw(ints, 0);
  182. }
  183. /**
  184. * Convert an ObjectId from binary representation expressed in integers.
  185. *
  186. * @param ints
  187. * the raw int buffer to read from. At least 5 integers after p
  188. * must be available within this integers array.
  189. * @param p
  190. * position to read the first integer of data from.
  191. */
  192. public void fromRaw(int[] ints, int p) {
  193. w1 = ints[p];
  194. w2 = ints[p + 1];
  195. w3 = ints[p + 2];
  196. w4 = ints[p + 3];
  197. w5 = ints[p + 4];
  198. }
  199. /**
  200. * Convert an ObjectId from binary representation expressed in integers.
  201. *
  202. * @param a
  203. * an int.
  204. * @param b
  205. * an int.
  206. * @param c
  207. * an int.
  208. * @param d
  209. * an int.
  210. * @param e
  211. * an int.
  212. * @since 4.7
  213. */
  214. public void set(int a, int b, int c, int d, int e) {
  215. w1 = a;
  216. w2 = b;
  217. w3 = c;
  218. w4 = d;
  219. w5 = e;
  220. }
  221. /**
  222. * Convert an ObjectId from hex characters (US-ASCII).
  223. *
  224. * @param buf
  225. * the US-ASCII buffer to read from. At least 40 bytes after
  226. * offset must be available within this byte array.
  227. * @param offset
  228. * position to read the first character from.
  229. */
  230. public void fromString(byte[] buf, int offset) {
  231. fromHexString(buf, offset);
  232. }
  233. /**
  234. * Convert an ObjectId from hex characters.
  235. *
  236. * @param str
  237. * the string to read from. Must be 40 characters long.
  238. */
  239. public void fromString(String str) {
  240. if (str.length() != Constants.OBJECT_ID_STRING_LENGTH)
  241. throw new IllegalArgumentException(MessageFormat.format(
  242. JGitText.get().invalidId, str));
  243. fromHexString(Constants.encodeASCII(str), 0);
  244. }
  245. private void fromHexString(byte[] bs, int p) {
  246. try {
  247. w1 = RawParseUtils.parseHexInt32(bs, p);
  248. w2 = RawParseUtils.parseHexInt32(bs, p + 8);
  249. w3 = RawParseUtils.parseHexInt32(bs, p + 16);
  250. w4 = RawParseUtils.parseHexInt32(bs, p + 24);
  251. w5 = RawParseUtils.parseHexInt32(bs, p + 32);
  252. } catch (ArrayIndexOutOfBoundsException e1) {
  253. throw new InvalidObjectIdException(bs, p,
  254. Constants.OBJECT_ID_STRING_LENGTH);
  255. }
  256. }
  257. /** {@inheritDoc} */
  258. @Override
  259. public ObjectId toObjectId() {
  260. return new ObjectId(this);
  261. }
  262. }