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
;
45 * A mutable SHA-1 abstraction.
47 public class MutableObjectId
extends AnyObjectId
{
49 * Empty constructor. Initialize object with default (zeros) value.
51 public MutableObjectId() {
56 * Copying constructor.
59 * original entry, to copy id from
61 MutableObjectId(MutableObjectId src
) {
70 * Convert an ObjectId from raw binary representation.
73 * the raw byte buffer to read from. At least 20 bytes must be
74 * available within this byte array.
76 public void fromRaw(final byte[] bs
) {
81 * Convert an ObjectId from raw binary representation.
84 * the raw byte buffer to read from. At least 20 bytes after p
85 * must be available within this byte array.
87 * position to read the first byte of data from.
89 public void fromRaw(final byte[] bs
, final int p
) {
90 w1
= NB
.decodeInt32(bs
, p
);
91 w2
= NB
.decodeInt32(bs
, p
+ 4);
92 w3
= NB
.decodeInt32(bs
, p
+ 8);
93 w4
= NB
.decodeInt32(bs
, p
+ 12);
94 w5
= NB
.decodeInt32(bs
, p
+ 16);
98 * Convert an ObjectId from binary representation expressed in integers.
101 * the raw int buffer to read from. At least 5 integers must be
102 * available within this integers array.
104 public void fromRaw(final int[] ints
) {
109 * Convert an ObjectId from binary representation expressed in integers.
112 * the raw int buffer to read from. At least 5 integers after p
113 * must be available within this integers array.
115 * position to read the first integer of data from.
118 public void fromRaw(final int[] ints
, final int p
) {
127 * Convert an ObjectId from hex characters (US-ASCII).
130 * the US-ASCII buffer to read from. At least 40 bytes after
131 * offset must be available within this byte array.
133 * position to read the first character from.
135 public void fromString(final byte[] buf
, final int offset
) {
136 fromHexString(buf
, offset
);
140 * Convert an ObjectId from hex characters.
143 * the string to read from. Must be 40 characters long.
145 public void fromString(final String str
) {
146 if (str
.length() != STR_LEN
)
147 throw new IllegalArgumentException("Invalid id: " + str
);
148 fromHexString(Constants
.encodeASCII(str
), 0);
151 private void fromHexString(final byte[] bs
, int p
) {
153 w1
= hexUInt32(bs
, p
);
154 w2
= hexUInt32(bs
, p
+ 8);
155 w3
= hexUInt32(bs
, p
+ 16);
156 w4
= hexUInt32(bs
, p
+ 24);
157 w5
= hexUInt32(bs
, p
+ 32);
158 } catch (ArrayIndexOutOfBoundsException e1
) {
160 final String str
= new String(bs
, p
, STR_LEN
, "US-ASCII");
161 throw new IllegalArgumentException("Invalid id: " + str
);
162 } catch (UnsupportedEncodingException e2
) {
163 throw new IllegalArgumentException("Invalid id");
164 } catch (StringIndexOutOfBoundsException e2
) {
165 throw new IllegalArgumentException("Invalid id");
171 public ObjectId
toObjectId() {
172 return new ObjectId(this);