View Javadoc
1   /*
2    * Copyright (C) 2015, Matthias Sohn <matthias.sohn@sap.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.lfs.lib;
45  
46  import java.io.IOException;
47  import java.io.ObjectInputStream;
48  import java.io.ObjectOutputStream;
49  import java.io.Serializable;
50  
51  import org.eclipse.jgit.lfs.errors.InvalidLongObjectIdException;
52  import org.eclipse.jgit.lib.ObjectId;
53  import org.eclipse.jgit.util.NB;
54  import org.eclipse.jgit.util.RawParseUtils;
55  
56  /**
57   * A SHA-256 abstraction.
58   *
59   * Ported to SHA-256 from {@link ObjectId}
60   *
61   * @since 4.3
62   */
63  public class LongObjectId extends AnyLongObjectId implements Serializable {
64  	private static final long serialVersionUID = 1L;
65  
66  	private static final LongObjectId ZEROID;
67  
68  	private static final String ZEROID_STR;
69  
70  	static {
71  		ZEROID = new LongObjectId(0L, 0L, 0L, 0L);
72  		ZEROID_STR = ZEROID.name();
73  	}
74  
75  	/**
76  	 * Get the special all-zero LongObjectId.
77  	 *
78  	 * @return the all-zero LongObjectId, often used to stand-in for no object.
79  	 */
80  	public static final LongObjectId zeroId() {
81  		return ZEROID;
82  	}
83  
84  	/**
85  	 * Test a string of characters to verify that it can be interpreted as
86  	 * LongObjectId.
87  	 * <p>
88  	 * If true the string can be parsed with {@link #fromString(String)}.
89  	 *
90  	 * @param id
91  	 *            the string to test.
92  	 * @return true if the string can converted into an LongObjectId.
93  	 */
94  	public static final boolean isId(final String id) {
95  		if (id.length() != Constants.LONG_OBJECT_ID_STRING_LENGTH)
96  			return false;
97  		try {
98  			for (int i = 0; i < Constants.LONG_OBJECT_ID_STRING_LENGTH; i++) {
99  				RawParseUtils.parseHexInt4((byte) id.charAt(i));
100 			}
101 			return true;
102 		} catch (ArrayIndexOutOfBoundsException e) {
103 			return false;
104 		}
105 	}
106 
107 	/**
108 	 * Convert a LongObjectId into a hex string representation.
109 	 *
110 	 * @param i
111 	 *            the id to convert. May be null.
112 	 * @return the hex string conversion of this id's content.
113 	 */
114 	public static final String toString(final LongObjectId i) {
115 		return i != null ? i.name() : ZEROID_STR;
116 	}
117 
118 	/**
119 	 * Compare two object identifier byte sequences for equality.
120 	 *
121 	 * @param firstBuffer
122 	 *            the first buffer to compare against. Must have at least 32
123 	 *            bytes from position fi through the end of the buffer.
124 	 * @param fi
125 	 *            first offset within firstBuffer to begin testing.
126 	 * @param secondBuffer
127 	 *            the second buffer to compare against. Must have at least 32
128 	 *            bytes from position si through the end of the buffer.
129 	 * @param si
130 	 *            first offset within secondBuffer to begin testing.
131 	 * @return true if the two identifiers are the same.
132 	 */
133 	public static boolean equals(final byte[] firstBuffer, final int fi,
134 			final byte[] secondBuffer, final int si) {
135 		return firstBuffer[fi] == secondBuffer[si]
136 				&& firstBuffer[fi + 1] == secondBuffer[si + 1]
137 				&& firstBuffer[fi + 2] == secondBuffer[si + 2]
138 				&& firstBuffer[fi + 3] == secondBuffer[si + 3]
139 				&& firstBuffer[fi + 4] == secondBuffer[si + 4]
140 				&& firstBuffer[fi + 5] == secondBuffer[si + 5]
141 				&& firstBuffer[fi + 6] == secondBuffer[si + 6]
142 				&& firstBuffer[fi + 7] == secondBuffer[si + 7]
143 				&& firstBuffer[fi + 8] == secondBuffer[si + 8]
144 				&& firstBuffer[fi + 9] == secondBuffer[si + 9]
145 				&& firstBuffer[fi + 10] == secondBuffer[si + 10]
146 				&& firstBuffer[fi + 11] == secondBuffer[si + 11]
147 				&& firstBuffer[fi + 12] == secondBuffer[si + 12]
148 				&& firstBuffer[fi + 13] == secondBuffer[si + 13]
149 				&& firstBuffer[fi + 14] == secondBuffer[si + 14]
150 				&& firstBuffer[fi + 15] == secondBuffer[si + 15]
151 				&& firstBuffer[fi + 16] == secondBuffer[si + 16]
152 				&& firstBuffer[fi + 17] == secondBuffer[si + 17]
153 				&& firstBuffer[fi + 18] == secondBuffer[si + 18]
154 				&& firstBuffer[fi + 19] == secondBuffer[si + 19]
155 				&& firstBuffer[fi + 20] == secondBuffer[si + 20]
156 				&& firstBuffer[fi + 21] == secondBuffer[si + 21]
157 				&& firstBuffer[fi + 22] == secondBuffer[si + 22]
158 				&& firstBuffer[fi + 23] == secondBuffer[si + 23]
159 				&& firstBuffer[fi + 24] == secondBuffer[si + 24]
160 				&& firstBuffer[fi + 25] == secondBuffer[si + 25]
161 				&& firstBuffer[fi + 26] == secondBuffer[si + 26]
162 				&& firstBuffer[fi + 27] == secondBuffer[si + 27]
163 				&& firstBuffer[fi + 28] == secondBuffer[si + 28]
164 				&& firstBuffer[fi + 29] == secondBuffer[si + 29]
165 				&& firstBuffer[fi + 30] == secondBuffer[si + 30]
166 				&& firstBuffer[fi + 31] == secondBuffer[si + 31];
167 	}
168 
169 	/**
170 	 * Convert a LongObjectId from raw binary representation.
171 	 *
172 	 * @param bs
173 	 *            the raw byte buffer to read from. At least 32 bytes must be
174 	 *            available within this byte array.
175 	 * @return the converted object id.
176 	 */
177 	public static final LongObjectId fromRaw(final byte[] bs) {
178 		return fromRaw(bs, 0);
179 	}
180 
181 	/**
182 	 * Convert a LongObjectId from raw binary representation.
183 	 *
184 	 * @param bs
185 	 *            the raw byte buffer to read from. At least 32 bytes after p
186 	 *            must be available within this byte array.
187 	 * @param p
188 	 *            position to read the first byte of data from.
189 	 * @return the converted object id.
190 	 */
191 	public static final LongObjectId fromRaw(final byte[] bs, final int p) {
192 		final long a = NB.decodeInt64(bs, p);
193 		final long b = NB.decodeInt64(bs, p + 8);
194 		final long c = NB.decodeInt64(bs, p + 16);
195 		final long d = NB.decodeInt64(bs, p + 24);
196 		return new LongObjectId(a, b, c, d);
197 	}
198 
199 	/**
200 	 * Convert a LongObjectId from raw binary representation.
201 	 *
202 	 * @param is
203 	 *            the raw long buffer to read from. At least 4 longs must be
204 	 *            available within this long array.
205 	 * @return the converted object id.
206 	 */
207 	public static final LongObjectId fromRaw(final long[] is) {
208 		return fromRaw(is, 0);
209 	}
210 
211 	/**
212 	 * Convert a LongObjectId from raw binary representation.
213 	 *
214 	 * @param is
215 	 *            the raw long buffer to read from. At least 4 longs after p
216 	 *            must be available within this long array.
217 	 * @param p
218 	 *            position to read the first long of data from.
219 	 * @return the converted object id.
220 	 */
221 	public static final LongObjectId fromRaw(final long[] is, final int p) {
222 		return new LongObjectId(is[p], is[p + 1], is[p + 2], is[p + 3]);
223 	}
224 
225 	/**
226 	 * Convert a LongObjectId from hex characters (US-ASCII).
227 	 *
228 	 * @param buf
229 	 *            the US-ASCII buffer to read from. At least 64 bytes after
230 	 *            offset must be available within this byte array.
231 	 * @param offset
232 	 *            position to read the first character from.
233 	 * @return the converted object id.
234 	 */
235 	public static final LongObjectId fromString(final byte[] buf, final int offset) {
236 		return fromHexString(buf, offset);
237 	}
238 
239 	/**
240 	 * Convert a LongObjectId from hex characters.
241 	 *
242 	 * @param str
243 	 *            the string to read from. Must be 64 characters long.
244 	 * @return the converted object id.
245 	 */
246 	public static LongObjectId fromString(final String str) {
247 		if (str.length() != Constants.LONG_OBJECT_ID_STRING_LENGTH)
248 			throw new InvalidLongObjectIdException(str);
249 		return fromHexString(org.eclipse.jgit.lib.Constants.encodeASCII(str),
250 				0);
251 	}
252 
253 	private static final LongObjectId fromHexString(final byte[] bs, int p) {
254 		try {
255 			final long a = RawParseUtils.parseHexInt64(bs, p);
256 			final long b = RawParseUtils.parseHexInt64(bs, p + 16);
257 			final long c = RawParseUtils.parseHexInt64(bs, p + 32);
258 			final long d = RawParseUtils.parseHexInt64(bs, p + 48);
259 			return new LongObjectId(a, b, c, d);
260 		} catch (ArrayIndexOutOfBoundsException e1) {
261 			throw new InvalidLongObjectIdException(bs, p,
262 					Constants.LONG_OBJECT_ID_STRING_LENGTH);
263 		}
264 	}
265 
266 	LongObjectId(final long new_1, final long new_2, final long new_3,
267 			final long new_4) {
268 		w1 = new_1;
269 		w2 = new_2;
270 		w3 = new_3;
271 		w4 = new_4;
272 	}
273 
274 	/**
275 	 * Initialize this instance by copying another existing LongObjectId.
276 	 * <p>
277 	 * This constructor is mostly useful for subclasses which want to extend a
278 	 * LongObjectId with more properties, but initialize from an existing
279 	 * LongObjectId instance acquired by other means.
280 	 *
281 	 * @param src
282 	 *            another already parsed LongObjectId to copy the value out of.
283 	 */
284 	protected LongObjectId(final AnyLongObjectId src) {
285 		w1 = src.w1;
286 		w2 = src.w2;
287 		w3 = src.w3;
288 		w4 = src.w4;
289 	}
290 
291 	@Override
292 	public LongObjectId toObjectId() {
293 		return this;
294 	}
295 
296 	private void writeObject(ObjectOutputStream os) throws IOException {
297 		os.writeLong(w1);
298 		os.writeLong(w2);
299 		os.writeLong(w3);
300 		os.writeLong(w4);
301 	}
302 
303 	private void readObject(ObjectInputStream ois) throws IOException {
304 		w1 = ois.readLong();
305 		w2 = ois.readLong();
306 		w3 = ois.readLong();
307 		w4 = ois.readLong();
308 	}
309 }