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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.JGitText;
  49. import org.eclipse.jgit.errors.InvalidObjectIdException;
  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. /** Make this id match {@link ObjectId#zeroId()}. */
  72. public void clear() {
  73. w1 = 0;
  74. w2 = 0;
  75. w3 = 0;
  76. w4 = 0;
  77. w5 = 0;
  78. }
  79. /**
  80. * Copy an ObjectId into this mutable buffer.
  81. *
  82. * @param src
  83. * the source id to copy from.
  84. */
  85. public void fromObjectId(AnyObjectId src) {
  86. this.w1 = src.w1;
  87. this.w2 = src.w2;
  88. this.w3 = src.w3;
  89. this.w4 = src.w4;
  90. this.w5 = src.w5;
  91. }
  92. /**
  93. * Convert an ObjectId from raw binary representation.
  94. *
  95. * @param bs
  96. * the raw byte buffer to read from. At least 20 bytes must be
  97. * available within this byte array.
  98. */
  99. public void fromRaw(final byte[] bs) {
  100. fromRaw(bs, 0);
  101. }
  102. /**
  103. * Convert an ObjectId from raw binary representation.
  104. *
  105. * @param bs
  106. * the raw byte buffer to read from. At least 20 bytes after p
  107. * must be available within this byte array.
  108. * @param p
  109. * position to read the first byte of data from.
  110. */
  111. public void fromRaw(final byte[] bs, final int p) {
  112. w1 = NB.decodeInt32(bs, p);
  113. w2 = NB.decodeInt32(bs, p + 4);
  114. w3 = NB.decodeInt32(bs, p + 8);
  115. w4 = NB.decodeInt32(bs, p + 12);
  116. w5 = NB.decodeInt32(bs, p + 16);
  117. }
  118. /**
  119. * Convert an ObjectId from binary representation expressed in integers.
  120. *
  121. * @param ints
  122. * the raw int buffer to read from. At least 5 integers must be
  123. * available within this integers array.
  124. */
  125. public void fromRaw(final int[] ints) {
  126. fromRaw(ints, 0);
  127. }
  128. /**
  129. * Convert an ObjectId from binary representation expressed in integers.
  130. *
  131. * @param ints
  132. * the raw int buffer to read from. At least 5 integers after p
  133. * must be available within this integers array.
  134. * @param p
  135. * position to read the first integer of data from.
  136. *
  137. */
  138. public void fromRaw(final int[] ints, final int p) {
  139. w1 = ints[p];
  140. w2 = ints[p + 1];
  141. w3 = ints[p + 2];
  142. w4 = ints[p + 3];
  143. w5 = ints[p + 4];
  144. }
  145. /**
  146. * Convert an ObjectId from hex characters (US-ASCII).
  147. *
  148. * @param buf
  149. * the US-ASCII buffer to read from. At least 40 bytes after
  150. * offset must be available within this byte array.
  151. * @param offset
  152. * position to read the first character from.
  153. */
  154. public void fromString(final byte[] buf, final int offset) {
  155. fromHexString(buf, offset);
  156. }
  157. /**
  158. * Convert an ObjectId from hex characters.
  159. *
  160. * @param str
  161. * the string to read from. Must be 40 characters long.
  162. */
  163. public void fromString(final String str) {
  164. if (str.length() != Constants.OBJECT_ID_STRING_LENGTH)
  165. throw new IllegalArgumentException(MessageFormat.format(
  166. JGitText.get().invalidId, str));
  167. fromHexString(Constants.encodeASCII(str), 0);
  168. }
  169. private void fromHexString(final byte[] bs, int p) {
  170. try {
  171. w1 = RawParseUtils.parseHexInt32(bs, p);
  172. w2 = RawParseUtils.parseHexInt32(bs, p + 8);
  173. w3 = RawParseUtils.parseHexInt32(bs, p + 16);
  174. w4 = RawParseUtils.parseHexInt32(bs, p + 24);
  175. w5 = RawParseUtils.parseHexInt32(bs, p + 32);
  176. } catch (ArrayIndexOutOfBoundsException e1) {
  177. throw new InvalidObjectIdException(bs, p,
  178. Constants.OBJECT_ID_STRING_LENGTH);
  179. }
  180. }
  181. @Override
  182. public ObjectId toObjectId() {
  183. return new ObjectId(this);
  184. }
  185. }