2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.spearce
.jgit
.lib
;
40 import java
.io
.UnsupportedEncodingException
;
42 import org
.spearce
.jgit
.util
.NB
;
43 import org
.spearce
.jgit
.util
.RawParseUtils
;
46 * A mutable SHA-1 abstraction.
48 public class MutableObjectId
extends AnyObjectId
{
50 * Empty constructor. Initialize object with default (zeros) value.
52 public MutableObjectId() {
57 * Copying constructor.
60 * original entry, to copy id from
62 MutableObjectId(MutableObjectId src
) {
70 /** Make this id match {@link ObjectId#zeroId()}. */
80 * Convert an ObjectId from raw binary representation.
83 * the raw byte buffer to read from. At least 20 bytes must be
84 * available within this byte array.
86 public void fromRaw(final byte[] bs
) {
91 * Convert an ObjectId from raw binary representation.
94 * the raw byte buffer to read from. At least 20 bytes after p
95 * must be available within this byte array.
97 * position to read the first byte of data from.
99 public void fromRaw(final byte[] bs
, final int p
) {
100 w1
= NB
.decodeInt32(bs
, p
);
101 w2
= NB
.decodeInt32(bs
, p
+ 4);
102 w3
= NB
.decodeInt32(bs
, p
+ 8);
103 w4
= NB
.decodeInt32(bs
, p
+ 12);
104 w5
= NB
.decodeInt32(bs
, p
+ 16);
108 * Convert an ObjectId from binary representation expressed in integers.
111 * the raw int buffer to read from. At least 5 integers must be
112 * available within this integers array.
114 public void fromRaw(final int[] ints
) {
119 * Convert an ObjectId from binary representation expressed in integers.
122 * the raw int buffer to read from. At least 5 integers after p
123 * must be available within this integers array.
125 * position to read the first integer of data from.
128 public void fromRaw(final int[] ints
, final int p
) {
137 * Convert an ObjectId from hex characters (US-ASCII).
140 * the US-ASCII buffer to read from. At least 40 bytes after
141 * offset must be available within this byte array.
143 * position to read the first character from.
145 public void fromString(final byte[] buf
, final int offset
) {
146 fromHexString(buf
, offset
);
150 * Convert an ObjectId from hex characters.
153 * the string to read from. Must be 40 characters long.
155 public void fromString(final String str
) {
156 if (str
.length() != STR_LEN
)
157 throw new IllegalArgumentException("Invalid id: " + str
);
158 fromHexString(Constants
.encodeASCII(str
), 0);
161 private void fromHexString(final byte[] bs
, int p
) {
163 w1
= RawParseUtils
.parseHexInt32(bs
, p
);
164 w2
= RawParseUtils
.parseHexInt32(bs
, p
+ 8);
165 w3
= RawParseUtils
.parseHexInt32(bs
, p
+ 16);
166 w4
= RawParseUtils
.parseHexInt32(bs
, p
+ 24);
167 w5
= RawParseUtils
.parseHexInt32(bs
, p
+ 32);
168 } catch (ArrayIndexOutOfBoundsException e1
) {
170 final String str
= new String(bs
, p
, STR_LEN
, "US-ASCII");
171 throw new IllegalArgumentException("Invalid id: " + str
);
172 } catch (UnsupportedEncodingException e2
) {
173 throw new IllegalArgumentException("Invalid id");
174 } catch (StringIndexOutOfBoundsException e2
) {
175 throw new IllegalArgumentException("Invalid id");
181 public ObjectId
toObjectId() {
182 return new ObjectId(this);