1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 package org.eclipse.jgit.lib;
46
47 import java.io.IOException;
48 import java.io.ObjectInputStream;
49 import java.io.ObjectOutputStream;
50 import java.io.Serializable;
51
52 import org.eclipse.jgit.errors.InvalidObjectIdException;
53 import org.eclipse.jgit.util.NB;
54 import org.eclipse.jgit.util.RawParseUtils;
55
56
57
58
59 public class ObjectId extends AnyObjectId implements Serializable {
60 private static final long serialVersionUID = 1L;
61
62 private static final ObjectId ZEROID;
63
64 private static final String ZEROID_STR;
65
66 static {
67 ZEROID = new ObjectId(0, 0, 0, 0, 0);
68 ZEROID_STR = ZEROID.name();
69 }
70
71
72
73
74
75
76 public static final ObjectId zeroId() {
77 return ZEROID;
78 }
79
80
81
82
83
84
85
86
87
88
89 public static final boolean isId(final String id) {
90 if (id.length() != Constants.OBJECT_ID_STRING_LENGTH)
91 return false;
92 try {
93 for (int i = 0; i < Constants.OBJECT_ID_STRING_LENGTH; i++) {
94 RawParseUtils.parseHexInt4((byte) id.charAt(i));
95 }
96 return true;
97 } catch (ArrayIndexOutOfBoundsException e) {
98 return false;
99 }
100 }
101
102
103
104
105
106
107
108
109 public static final String toString(final ObjectId i) {
110 return i != null ? i.name() : ZEROID_STR;
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 public static boolean equals(final byte[] firstBuffer, final int fi,
129 final byte[] secondBuffer, final int si) {
130 return firstBuffer[fi] == secondBuffer[si]
131 && firstBuffer[fi + 1] == secondBuffer[si + 1]
132 && firstBuffer[fi + 2] == secondBuffer[si + 2]
133 && firstBuffer[fi + 3] == secondBuffer[si + 3]
134 && firstBuffer[fi + 4] == secondBuffer[si + 4]
135 && firstBuffer[fi + 5] == secondBuffer[si + 5]
136 && firstBuffer[fi + 6] == secondBuffer[si + 6]
137 && firstBuffer[fi + 7] == secondBuffer[si + 7]
138 && firstBuffer[fi + 8] == secondBuffer[si + 8]
139 && firstBuffer[fi + 9] == secondBuffer[si + 9]
140 && firstBuffer[fi + 10] == secondBuffer[si + 10]
141 && firstBuffer[fi + 11] == secondBuffer[si + 11]
142 && firstBuffer[fi + 12] == secondBuffer[si + 12]
143 && firstBuffer[fi + 13] == secondBuffer[si + 13]
144 && firstBuffer[fi + 14] == secondBuffer[si + 14]
145 && firstBuffer[fi + 15] == secondBuffer[si + 15]
146 && firstBuffer[fi + 16] == secondBuffer[si + 16]
147 && firstBuffer[fi + 17] == secondBuffer[si + 17]
148 && firstBuffer[fi + 18] == secondBuffer[si + 18]
149 && firstBuffer[fi + 19] == secondBuffer[si + 19];
150 }
151
152
153
154
155
156
157
158
159
160 public static final ObjectId fromRaw(final byte[] bs) {
161 return fromRaw(bs, 0);
162 }
163
164
165
166
167
168
169
170
171
172
173
174 public static final ObjectId fromRaw(final byte[] bs, final int p) {
175 final int a = NB.decodeInt32(bs, p);
176 final int b = NB.decodeInt32(bs, p + 4);
177 final int c = NB.decodeInt32(bs, p + 8);
178 final int d = NB.decodeInt32(bs, p + 12);
179 final int e = NB.decodeInt32(bs, p + 16);
180 return new ObjectId(a, b, c, d, e);
181 }
182
183
184
185
186
187
188
189
190
191 public static final ObjectId fromRaw(final int[] is) {
192 return fromRaw(is, 0);
193 }
194
195
196
197
198
199
200
201
202
203
204
205 public static final ObjectId fromRaw(final int[] is, final int p) {
206 return new ObjectId(is[p], is[p + 1], is[p + 2], is[p + 3], is[p + 4]);
207 }
208
209
210
211
212
213
214
215
216
217
218
219 public static final ObjectId fromString(final byte[] buf, final int offset) {
220 return fromHexString(buf, offset);
221 }
222
223
224
225
226
227
228
229
230 public static ObjectId fromString(final String str) {
231 if (str.length() != Constants.OBJECT_ID_STRING_LENGTH) {
232 throw new InvalidObjectIdException(str);
233 }
234 return fromHexString(Constants.encodeASCII(str), 0);
235 }
236
237 private static final ObjectId fromHexString(final byte[] bs, int p) {
238 try {
239 final int a = RawParseUtils.parseHexInt32(bs, p);
240 final int b = RawParseUtils.parseHexInt32(bs, p + 8);
241 final int c = RawParseUtils.parseHexInt32(bs, p + 16);
242 final int d = RawParseUtils.parseHexInt32(bs, p + 24);
243 final int e = RawParseUtils.parseHexInt32(bs, p + 32);
244 return new ObjectId(a, b, c, d, e);
245 } catch (ArrayIndexOutOfBoundsException e1) {
246 throw new InvalidObjectIdException(bs, p,
247 Constants.OBJECT_ID_STRING_LENGTH);
248 }
249 }
250
251
252
253
254
255
256
257
258
259
260
261 public ObjectId(int new_1, int new_2, int new_3, int new_4, int new_5) {
262 w1 = new_1;
263 w2 = new_2;
264 w3 = new_3;
265 w4 = new_4;
266 w5 = new_5;
267 }
268
269
270
271
272
273
274
275
276
277
278
279 protected ObjectId(final AnyObjectId src) {
280 w1 = src.w1;
281 w2 = src.w2;
282 w3 = src.w3;
283 w4 = src.w4;
284 w5 = src.w5;
285 }
286
287 @Override
288 public ObjectId toObjectId() {
289 return this;
290 }
291
292 private void writeObject(ObjectOutputStream os) throws IOException {
293 os.writeInt(w1);
294 os.writeInt(w2);
295 os.writeInt(w3);
296 os.writeInt(w4);
297 os.writeInt(w5);
298 }
299
300 private void readObject(ObjectInputStream ois) throws IOException {
301 w1 = ois.readInt();
302 w2 = ois.readInt();
303 w3 = ois.readInt();
304 w4 = ois.readInt();
305 w5 = ois.readInt();
306 }
307 }