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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 org.eclipse.jgit.errors.InvalidObjectIdException;
  48. import org.eclipse.jgit.util.NB;
  49. import org.eclipse.jgit.util.RawParseUtils;
  50. /**
  51. * A mutable SHA-1 abstraction.
  52. */
  53. public class MutableObjectId extends AnyObjectId {
  54. /**
  55. * Empty constructor. Initialize object with default (zeros) value.
  56. */
  57. public MutableObjectId() {
  58. super();
  59. }
  60. /**
  61. * Copying constructor.
  62. *
  63. * @param src
  64. * original entry, to copy id from
  65. */
  66. MutableObjectId(MutableObjectId src) {
  67. this.w1 = src.w1;
  68. this.w2 = src.w2;
  69. this.w3 = src.w3;
  70. this.w4 = src.w4;
  71. this.w5 = src.w5;
  72. }
  73. /** Make this id match {@link ObjectId#zeroId()}. */
  74. public void clear() {
  75. w1 = 0;
  76. w2 = 0;
  77. w3 = 0;
  78. w4 = 0;
  79. w5 = 0;
  80. }
  81. /**
  82. * Convert an ObjectId from raw binary representation.
  83. *
  84. * @param bs
  85. * the raw byte buffer to read from. At least 20 bytes must be
  86. * available within this byte array.
  87. */
  88. public void fromRaw(final byte[] bs) {
  89. fromRaw(bs, 0);
  90. }
  91. /**
  92. * Convert an ObjectId from raw binary representation.
  93. *
  94. * @param bs
  95. * the raw byte buffer to read from. At least 20 bytes after p
  96. * must be available within this byte array.
  97. * @param p
  98. * position to read the first byte of data from.
  99. */
  100. public void fromRaw(final byte[] bs, final int p) {
  101. w1 = NB.decodeInt32(bs, p);
  102. w2 = NB.decodeInt32(bs, p + 4);
  103. w3 = NB.decodeInt32(bs, p + 8);
  104. w4 = NB.decodeInt32(bs, p + 12);
  105. w5 = NB.decodeInt32(bs, p + 16);
  106. }
  107. /**
  108. * Convert an ObjectId from binary representation expressed in integers.
  109. *
  110. * @param ints
  111. * the raw int buffer to read from. At least 5 integers must be
  112. * available within this integers array.
  113. */
  114. public void fromRaw(final int[] ints) {
  115. fromRaw(ints, 0);
  116. }
  117. /**
  118. * Convert an ObjectId from binary representation expressed in integers.
  119. *
  120. * @param ints
  121. * the raw int buffer to read from. At least 5 integers after p
  122. * must be available within this integers array.
  123. * @param p
  124. * position to read the first integer of data from.
  125. *
  126. */
  127. public void fromRaw(final int[] ints, final int p) {
  128. w1 = ints[p];
  129. w2 = ints[p + 1];
  130. w3 = ints[p + 2];
  131. w4 = ints[p + 3];
  132. w5 = ints[p + 4];
  133. }
  134. /**
  135. * Convert an ObjectId from hex characters (US-ASCII).
  136. *
  137. * @param buf
  138. * the US-ASCII buffer to read from. At least 40 bytes after
  139. * offset must be available within this byte array.
  140. * @param offset
  141. * position to read the first character from.
  142. */
  143. public void fromString(final byte[] buf, final int offset) {
  144. fromHexString(buf, offset);
  145. }
  146. /**
  147. * Convert an ObjectId from hex characters.
  148. *
  149. * @param str
  150. * the string to read from. Must be 40 characters long.
  151. */
  152. public void fromString(final String str) {
  153. if (str.length() != Constants.OBJECT_ID_STRING_LENGTH)
  154. throw new IllegalArgumentException("Invalid id: " + str);
  155. fromHexString(Constants.encodeASCII(str), 0);
  156. }
  157. private void fromHexString(final byte[] bs, int p) {
  158. try {
  159. w1 = RawParseUtils.parseHexInt32(bs, p);
  160. w2 = RawParseUtils.parseHexInt32(bs, p + 8);
  161. w3 = RawParseUtils.parseHexInt32(bs, p + 16);
  162. w4 = RawParseUtils.parseHexInt32(bs, p + 24);
  163. w5 = RawParseUtils.parseHexInt32(bs, p + 32);
  164. } catch (ArrayIndexOutOfBoundsException e1) {
  165. throw new InvalidObjectIdException(bs, p,
  166. Constants.OBJECT_ID_STRING_LENGTH);
  167. }
  168. }
  169. @Override
  170. public ObjectId toObjectId() {
  171. return new ObjectId(this);
  172. }
  173. }