選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

MutableObjectId.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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, {@link Constants#OBJECT_ID_LENGTH}).
  77. * @param value
  78. * the value of the specified byte at {@code index}. Values are
  79. * unsigned and thus are in the range [0,255] rather than the
  80. * signed byte range of [-128, 127].
  81. * @throws ArrayIndexOutOfBoundsException
  82. * {@code index} is less than 0, equal to
  83. * {@link Constants#OBJECT_ID_LENGTH}, or greater than
  84. * {@link Constants#OBJECT_ID_LENGTH}.
  85. */
  86. public void setByte(int index, int value) {
  87. switch (index >> 2) {
  88. case 0:
  89. w1 = set(w1, index & 3, value);
  90. break;
  91. case 1:
  92. w2 = set(w2, index & 3, value);
  93. break;
  94. case 2:
  95. w3 = set(w3, index & 3, value);
  96. break;
  97. case 3:
  98. w4 = set(w4, index & 3, value);
  99. break;
  100. case 4:
  101. w5 = set(w5, index & 3, value);
  102. break;
  103. default:
  104. throw new ArrayIndexOutOfBoundsException(index);
  105. }
  106. }
  107. private static int set(int w, int index, int value) {
  108. value &= 0xff;
  109. switch (index) {
  110. case 0:
  111. return (w & 0x00ffffff) | (value << 24);
  112. case 1:
  113. return (w & 0xff00ffff) | (value << 16);
  114. case 2:
  115. return (w & 0xffff00ff) | (value << 8);
  116. case 3:
  117. return (w & 0xffffff00) | value;
  118. default:
  119. throw new ArrayIndexOutOfBoundsException();
  120. }
  121. }
  122. /** Make this id match {@link ObjectId#zeroId()}. */
  123. public void clear() {
  124. w1 = 0;
  125. w2 = 0;
  126. w3 = 0;
  127. w4 = 0;
  128. w5 = 0;
  129. }
  130. /**
  131. * Copy an ObjectId into this mutable buffer.
  132. *
  133. * @param src
  134. * the source id to copy from.
  135. */
  136. public void fromObjectId(AnyObjectId src) {
  137. this.w1 = src.w1;
  138. this.w2 = src.w2;
  139. this.w3 = src.w3;
  140. this.w4 = src.w4;
  141. this.w5 = src.w5;
  142. }
  143. /**
  144. * Convert an ObjectId from raw binary representation.
  145. *
  146. * @param bs
  147. * the raw byte buffer to read from. At least 20 bytes must be
  148. * available within this byte array.
  149. */
  150. public void fromRaw(final byte[] bs) {
  151. fromRaw(bs, 0);
  152. }
  153. /**
  154. * Convert an ObjectId from raw binary representation.
  155. *
  156. * @param bs
  157. * the raw byte buffer to read from. At least 20 bytes after p
  158. * must be available within this byte array.
  159. * @param p
  160. * position to read the first byte of data from.
  161. */
  162. public void fromRaw(final byte[] bs, final int p) {
  163. w1 = NB.decodeInt32(bs, p);
  164. w2 = NB.decodeInt32(bs, p + 4);
  165. w3 = NB.decodeInt32(bs, p + 8);
  166. w4 = NB.decodeInt32(bs, p + 12);
  167. w5 = NB.decodeInt32(bs, p + 16);
  168. }
  169. /**
  170. * Convert an ObjectId from binary representation expressed in integers.
  171. *
  172. * @param ints
  173. * the raw int buffer to read from. At least 5 integers must be
  174. * available within this integers array.
  175. */
  176. public void fromRaw(final int[] ints) {
  177. fromRaw(ints, 0);
  178. }
  179. /**
  180. * Convert an ObjectId from binary representation expressed in integers.
  181. *
  182. * @param ints
  183. * the raw int buffer to read from. At least 5 integers after p
  184. * must be available within this integers array.
  185. * @param p
  186. * position to read the first integer of data from.
  187. *
  188. */
  189. public void fromRaw(final int[] ints, final int p) {
  190. w1 = ints[p];
  191. w2 = ints[p + 1];
  192. w3 = ints[p + 2];
  193. w4 = ints[p + 3];
  194. w5 = ints[p + 4];
  195. }
  196. /**
  197. * Convert an ObjectId from hex characters (US-ASCII).
  198. *
  199. * @param buf
  200. * the US-ASCII buffer to read from. At least 40 bytes after
  201. * offset must be available within this byte array.
  202. * @param offset
  203. * position to read the first character from.
  204. */
  205. public void fromString(final byte[] buf, final int offset) {
  206. fromHexString(buf, offset);
  207. }
  208. /**
  209. * Convert an ObjectId from hex characters.
  210. *
  211. * @param str
  212. * the string to read from. Must be 40 characters long.
  213. */
  214. public void fromString(final String str) {
  215. if (str.length() != Constants.OBJECT_ID_STRING_LENGTH)
  216. throw new IllegalArgumentException(MessageFormat.format(
  217. JGitText.get().invalidId, str));
  218. fromHexString(Constants.encodeASCII(str), 0);
  219. }
  220. private void fromHexString(final byte[] bs, int p) {
  221. try {
  222. w1 = RawParseUtils.parseHexInt32(bs, p);
  223. w2 = RawParseUtils.parseHexInt32(bs, p + 8);
  224. w3 = RawParseUtils.parseHexInt32(bs, p + 16);
  225. w4 = RawParseUtils.parseHexInt32(bs, p + 24);
  226. w5 = RawParseUtils.parseHexInt32(bs, p + 32);
  227. } catch (ArrayIndexOutOfBoundsException e1) {
  228. throw new InvalidObjectIdException(bs, p,
  229. Constants.OBJECT_ID_STRING_LENGTH);
  230. }
  231. }
  232. @Override
  233. public ObjectId toObjectId() {
  234. return new ObjectId(this);
  235. }
  236. }