2 * Copyright (C) 2008 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License, version 2, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org
.spearce
.jgit
.lib
;
19 import java
.io
.UnsupportedEncodingException
;
22 * A mutable SHA-1 abstraction.
24 public class MutableObjectId
extends AnyObjectId
{
26 * Convert an ObjectId from raw binary representation.
29 * the raw byte buffer to read from. At least 20 bytes must be
30 * available within this byte array.
32 public void fromRaw(final byte[] bs
) {
37 * Convert an ObjectId from raw binary representation.
40 * the raw byte buffer to read from. At least 20 bytes after p
41 * must be available within this byte array.
43 * position to read the first byte of data from.
45 public void fromRaw(final byte[] bs
, final int p
) {
46 w1
= rawUInt32(bs
, p
);
47 w2
= rawUInt32(bs
, p
+ 4);
48 w3
= rawUInt32(bs
, p
+ 8);
49 w4
= rawUInt32(bs
, p
+ 12);
50 w5
= rawUInt32(bs
, p
+ 16);
54 * Convert an ObjectId from hex characters (US-ASCII).
57 * the US-ASCII buffer to read from. At least 40 bytes after
58 * offset must be available within this byte array.
60 * position to read the first character from.
62 public void fromString(final byte[] buf
, final int offset
) {
63 fromHexString(buf
, offset
);
67 * Convert an ObjectId from hex characters.
70 * the string to read from. Must be 40 characters long.
72 public void fromString(final String str
) {
73 if (str
.length() != STR_LEN
)
74 throw new IllegalArgumentException("Invalid id: " + str
);
75 fromHexString(Constants
.encodeASCII(str
), 0);
78 private void fromHexString(final byte[] bs
, int p
) {
80 w1
= hexUInt32(bs
, p
);
81 w2
= hexUInt32(bs
, p
+ 8);
82 w3
= hexUInt32(bs
, p
+ 16);
83 w4
= hexUInt32(bs
, p
+ 24);
84 w5
= hexUInt32(bs
, p
+ 32);
85 } catch (ArrayIndexOutOfBoundsException e1
) {
87 final String str
= new String(bs
, p
, STR_LEN
, "US-ASCII");
88 throw new IllegalArgumentException("Invalid id: " + str
);
89 } catch (UnsupportedEncodingException e2
) {
90 throw new IllegalArgumentException("Invalid id");
96 public ObjectId
toObjectId() {
97 return new ObjectId(this);