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.

MutableLongObjectId.java 7.4KB

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