Allow mutable object ids, improving commit parsing performance
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / lib / MutableObjectId.java
blobfa1956a53d9fa7a54f6e3b17eb8aacfb283b58c5
1 /*
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;
21 /**
22 * A mutable SHA-1 abstraction.
24 public class MutableObjectId extends AnyObjectId {
25 /**
26 * Convert an ObjectId from raw binary representation.
28 * @param bs
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) {
33 fromRaw(bs, 0);
36 /**
37 * Convert an ObjectId from raw binary representation.
39 * @param bs
40 * the raw byte buffer to read from. At least 20 bytes after p
41 * must be available within this byte array.
42 * @param p
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);
53 /**
54 * Convert an ObjectId from hex characters (US-ASCII).
56 * @param buf
57 * the US-ASCII buffer to read from. At least 40 bytes after
58 * offset must be available within this byte array.
59 * @param offset
60 * position to read the first character from.
62 public void fromString(final byte[] buf, final int offset) {
63 fromHexString(buf, offset);
66 /**
67 * Convert an ObjectId from hex characters.
69 * @param str
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) {
79 try {
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) {
86 try {
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");
95 @Override
96 public ObjectId toObjectId() {
97 return new ObjectId(this);