1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jgit.lib;
15
16 import java.text.MessageFormat;
17
18 import org.eclipse.jgit.errors.InvalidObjectIdException;
19 import org.eclipse.jgit.internal.JGitText;
20 import org.eclipse.jgit.util.NB;
21 import org.eclipse.jgit.util.RawParseUtils;
22
23
24
25
26 public class MutableObjectId extends AnyObjectId {
27
28
29
30 public MutableObjectId() {
31 super();
32 }
33
34
35
36
37
38
39
40 MutableObjectId(MutableObjectId src) {
41 fromObjectId(src);
42 }
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public void setByte(int index, int value) {
62 switch (index >> 2) {
63 case 0:
64 w1 = set(w1, index & 3, value);
65 break;
66 case 1:
67 w2 = set(w2, index & 3, value);
68 break;
69 case 2:
70 w3 = set(w3, index & 3, value);
71 break;
72 case 3:
73 w4 = set(w4, index & 3, value);
74 break;
75 case 4:
76 w5 = set(w5, index & 3, value);
77 break;
78 default:
79 throw new ArrayIndexOutOfBoundsException(index);
80 }
81 }
82
83 private static int set(int w, int index, int value) {
84 value &= 0xff;
85
86 switch (index) {
87 case 0:
88 return (w & 0x00ffffff) | (value << 24);
89 case 1:
90 return (w & 0xff00ffff) | (value << 16);
91 case 2:
92 return (w & 0xffff00ff) | (value << 8);
93 case 3:
94 return (w & 0xffffff00) | value;
95 default:
96 throw new ArrayIndexOutOfBoundsException();
97 }
98 }
99
100
101
102
103 public void clear() {
104 w1 = 0;
105 w2 = 0;
106 w3 = 0;
107 w4 = 0;
108 w5 = 0;
109 }
110
111
112
113
114
115
116
117 public void fromObjectId(AnyObjectId src) {
118 this.w1 = src.w1;
119 this.w2 = src.w2;
120 this.w3 = src.w3;
121 this.w4 = src.w4;
122 this.w5 = src.w5;
123 }
124
125
126
127
128
129
130
131
132 public void fromRaw(byte[] bs) {
133 fromRaw(bs, 0);
134 }
135
136
137
138
139
140
141
142
143
144
145 public void fromRaw(byte[] bs, int p) {
146 w1 = NB.decodeInt32(bs, p);
147 w2 = NB.decodeInt32(bs, p + 4);
148 w3 = NB.decodeInt32(bs, p + 8);
149 w4 = NB.decodeInt32(bs, p + 12);
150 w5 = NB.decodeInt32(bs, p + 16);
151 }
152
153
154
155
156
157
158
159
160 public void fromRaw(int[] ints) {
161 fromRaw(ints, 0);
162 }
163
164
165
166
167
168
169
170
171
172
173 public void fromRaw(int[] ints, int p) {
174 w1 = ints[p];
175 w2 = ints[p + 1];
176 w3 = ints[p + 2];
177 w4 = ints[p + 3];
178 w5 = ints[p + 4];
179 }
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 public void set(int a, int b, int c, int d, int e) {
197 w1 = a;
198 w2 = b;
199 w3 = c;
200 w4 = d;
201 w5 = e;
202 }
203
204
205
206
207
208
209
210
211
212
213 public void fromString(byte[] buf, int offset) {
214 fromHexString(buf, offset);
215 }
216
217
218
219
220
221
222
223 public void fromString(String str) {
224 if (str.length() != Constants.OBJECT_ID_STRING_LENGTH)
225 throw new IllegalArgumentException(MessageFormat.format(
226 JGitText.get().invalidId, str));
227 fromHexString(Constants.encodeASCII(str), 0);
228 }
229
230 private void fromHexString(byte[] bs, int p) {
231 try {
232 w1 = RawParseUtils.parseHexInt32(bs, p);
233 w2 = RawParseUtils.parseHexInt32(bs, p + 8);
234 w3 = RawParseUtils.parseHexInt32(bs, p + 16);
235 w4 = RawParseUtils.parseHexInt32(bs, p + 24);
236 w5 = RawParseUtils.parseHexInt32(bs, p + 32);
237 } catch (ArrayIndexOutOfBoundsException e) {
238 InvalidObjectIdException e1 = new InvalidObjectIdException(bs, p,
239 Constants.OBJECT_ID_STRING_LENGTH);
240 e1.initCause(e);
241 throw e1;
242 }
243 }
244
245
246 @Override
247 public ObjectId toObjectId() {
248 return new ObjectId(this);
249 }
250 }