2 * Copyright (C) 2006 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 import org
.spearce
.jgit
.util
.NB
;
24 * A SHA-1 abstraction.
26 public class ObjectId
extends AnyObjectId
{
27 private static final ObjectId ZEROID
;
29 private static final String ZEROID_STR
;
32 ZEROID
= new ObjectId(0, 0, 0, 0, 0);
33 ZEROID_STR
= ZEROID
.toString();
37 * Get the special all-null ObjectId.
39 * @return the all-null ObjectId, often used to stand-in for no object.
41 public static final ObjectId
zeroId() {
46 * Test a string of characters to verify it is a hex format.
48 * If true the string can be parsed with {@link #fromString(String)}.
52 * @return true if the string can converted into an ObjectId.
54 public static final boolean isId(final String id
) {
55 if (id
.length() != 2 * Constants
.OBJECT_ID_LENGTH
)
58 for (int k
= id
.length() - 1; k
>= 0; k
--)
59 if (fromhex
[id
.charAt(k
)] < 0)
62 } catch (ArrayIndexOutOfBoundsException e
) {
68 * Convert an ObjectId into a hex string representation.
71 * the id to convert. May be null.
72 * @return the hex string conversion of this id's content.
74 public static final String
toString(final ObjectId i
) {
75 return i
!= null ? i
.toString() : ZEROID_STR
;
79 * Compare to object identifier byte sequences for equality.
82 * the first buffer to compare against. Must have at least 20
83 * bytes from position ai through the end of the buffer.
85 * first offset within firstBuffer to begin testing.
87 * the second buffer to compare against. Must have at least 2
88 * bytes from position bi through the end of the buffer.
90 * first offset within secondBuffer to begin testing.
91 * @return true if the two identifiers are the same.
93 public static boolean equals(final byte[] firstBuffer
, final int fi
,
94 final byte[] secondBuffer
, final int si
) {
95 return firstBuffer
[fi
] == secondBuffer
[si
]
96 && firstBuffer
[fi
+ 1] == secondBuffer
[si
+ 1]
97 && firstBuffer
[fi
+ 2] == secondBuffer
[si
+ 2]
98 && firstBuffer
[fi
+ 3] == secondBuffer
[si
+ 3]
99 && firstBuffer
[fi
+ 4] == secondBuffer
[si
+ 4]
100 && firstBuffer
[fi
+ 5] == secondBuffer
[si
+ 5]
101 && firstBuffer
[fi
+ 6] == secondBuffer
[si
+ 6]
102 && firstBuffer
[fi
+ 7] == secondBuffer
[si
+ 7]
103 && firstBuffer
[fi
+ 8] == secondBuffer
[si
+ 8]
104 && firstBuffer
[fi
+ 9] == secondBuffer
[si
+ 9]
105 && firstBuffer
[fi
+ 10] == secondBuffer
[si
+ 10]
106 && firstBuffer
[fi
+ 11] == secondBuffer
[si
+ 11]
107 && firstBuffer
[fi
+ 12] == secondBuffer
[si
+ 12]
108 && firstBuffer
[fi
+ 13] == secondBuffer
[si
+ 13]
109 && firstBuffer
[fi
+ 14] == secondBuffer
[si
+ 14]
110 && firstBuffer
[fi
+ 15] == secondBuffer
[si
+ 15]
111 && firstBuffer
[fi
+ 16] == secondBuffer
[si
+ 16]
112 && firstBuffer
[fi
+ 17] == secondBuffer
[si
+ 17]
113 && firstBuffer
[fi
+ 18] == secondBuffer
[si
+ 18]
114 && firstBuffer
[fi
+ 19] == secondBuffer
[si
+ 19];
118 * Convert an ObjectId from raw binary representation.
121 * the raw byte buffer to read from. At least 20 bytes must be
122 * available within this byte array.
123 * @return the converted object id.
125 public static final ObjectId
fromRaw(final byte[] bs
) {
126 return fromRaw(bs
, 0);
130 * Convert an ObjectId from raw binary representation.
133 * the raw byte buffer to read from. At least 20 bytes after p
134 * must be available within this byte array.
136 * position to read the first byte of data from.
137 * @return the converted object id.
139 public static final ObjectId
fromRaw(final byte[] bs
, final int p
) {
140 final int a
= NB
.decodeInt32(bs
, p
);
141 final int b
= NB
.decodeInt32(bs
, p
+ 4);
142 final int c
= NB
.decodeInt32(bs
, p
+ 8);
143 final int d
= NB
.decodeInt32(bs
, p
+ 12);
144 final int e
= NB
.decodeInt32(bs
, p
+ 16);
145 return new ObjectId(a
, b
, c
, d
, e
);
149 * Convert an ObjectId from hex characters (US-ASCII).
152 * the US-ASCII buffer to read from. At least 40 bytes after
153 * offset must be available within this byte array.
155 * position to read the first character from.
156 * @return the converted object id.
158 public static final ObjectId
fromString(final byte[] buf
, final int offset
) {
159 return fromHexString(buf
, offset
);
163 * Convert an ObjectId from hex characters.
166 * the string to read from. Must be 40 characters long.
167 * @return the converted object id.
169 public static final ObjectId
fromString(final String str
) {
170 if (str
.length() != STR_LEN
)
171 throw new IllegalArgumentException("Invalid id: " + str
);
172 return fromHexString(Constants
.encodeASCII(str
), 0);
175 private static final ObjectId
fromHexString(final byte[] bs
, int p
) {
177 final int a
= hexUInt32(bs
, p
);
178 final int b
= hexUInt32(bs
, p
+ 8);
179 final int c
= hexUInt32(bs
, p
+ 16);
180 final int d
= hexUInt32(bs
, p
+ 24);
181 final int e
= hexUInt32(bs
, p
+ 32);
182 return new ObjectId(a
, b
, c
, d
, e
);
183 } catch (ArrayIndexOutOfBoundsException e1
) {
185 final String str
= new String(bs
, p
, STR_LEN
, "US-ASCII");
186 throw new IllegalArgumentException("Invalid id: " + str
);
187 } catch (UnsupportedEncodingException e2
) {
188 throw new IllegalArgumentException("Invalid id");
193 protected ObjectId(final int new_1
, final int new_2
, final int new_3
,
194 final int new_4
, final int new_5
) {
203 * Initialize this instance by copying another existing ObjectId.
205 * This constructor is mostly useful for subclasses who want to extend an
206 * ObjectId with more properties, but initialize from an existing ObjectId
207 * instance acquired by other means.
210 * another already parsed ObjectId to copy the value out of.
212 protected ObjectId(final AnyObjectId src
) {
221 public ObjectId
toObjectId() {