diff options
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java | 129 |
1 files changed, 67 insertions, 62 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java index 2f04751a1d..1b455b974d 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java @@ -1,57 +1,26 @@ /* * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com> - * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org> - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org> and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.lib; -import org.eclipse.jgit.errors.InvalidObjectIdException; -import org.eclipse.jgit.util.NB; -import org.eclipse.jgit.util.RawParseUtils; - import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; +import java.nio.ByteBuffer; + +import org.eclipse.jgit.annotations.Nullable; +import org.eclipse.jgit.errors.InvalidObjectIdException; +import org.eclipse.jgit.util.NB; +import org.eclipse.jgit.util.RawParseUtils; /** * A SHA-1 abstraction. @@ -86,7 +55,10 @@ public class ObjectId extends AnyObjectId implements Serializable { * the string to test. * @return true if the string can converted into an ObjectId. */ - public static final boolean isId(final String id) { + public static final boolean isId(@Nullable String id) { + if (id == null) { + return false; + } if (id.length() != Constants.OBJECT_ID_STRING_LENGTH) return false; try { @@ -106,21 +78,21 @@ public class ObjectId extends AnyObjectId implements Serializable { * the id to convert. May be null. * @return the hex string conversion of this id's content. */ - public static final String toString(final ObjectId i) { + public static final String toString(ObjectId i) { return i != null ? i.name() : ZEROID_STR; } /** - * Compare to object identifier byte sequences for equality. + * Compare two object identifier byte sequences for equality. * * @param firstBuffer * the first buffer to compare against. Must have at least 20 - * bytes from position ai through the end of the buffer. + * bytes from position fi through the end of the buffer. * @param fi * first offset within firstBuffer to begin testing. * @param secondBuffer - * the second buffer to compare against. Must have at least 2 - * bytes from position bi through the end of the buffer. + * the second buffer to compare against. Must have at least 20 + * bytes from position si through the end of the buffer. * @param si * first offset within secondBuffer to begin testing. * @return true if the two identifiers are the same. @@ -157,7 +129,7 @@ public class ObjectId extends AnyObjectId implements Serializable { * available within this byte array. * @return the converted object id. */ - public static final ObjectId fromRaw(final byte[] bs) { + public static final ObjectId fromRaw(byte[] bs) { return fromRaw(bs, 0); } @@ -171,7 +143,7 @@ public class ObjectId extends AnyObjectId implements Serializable { * position to read the first byte of data from. * @return the converted object id. */ - public static final ObjectId fromRaw(final byte[] bs, final int p) { + public static final ObjectId fromRaw(byte[] bs, int p) { final int a = NB.decodeInt32(bs, p); final int b = NB.decodeInt32(bs, p + 4); final int c = NB.decodeInt32(bs, p + 8); @@ -181,6 +153,22 @@ public class ObjectId extends AnyObjectId implements Serializable { } /** + * Convert an ObjectId from raw binary representation + * + * @param bb + * a bytebuffer with the objectid encoded as 5 consecutive ints. + * This is the reverse of {@link ObjectId#copyRawTo(ByteBuffer)} + * + * @return the converted object id. + * + * @since 7.0 + */ + public static final ObjectId fromRaw(ByteBuffer bb) { + return new ObjectId(bb.getInt(), bb.getInt(), bb.getInt(), bb.getInt(), + bb.getInt()); + } + + /** * Convert an ObjectId from raw binary representation. * * @param is @@ -188,7 +176,7 @@ public class ObjectId extends AnyObjectId implements Serializable { * be available within this int array. * @return the converted object id. */ - public static final ObjectId fromRaw(final int[] is) { + public static final ObjectId fromRaw(int[] is) { return fromRaw(is, 0); } @@ -202,7 +190,7 @@ public class ObjectId extends AnyObjectId implements Serializable { * position to read the first integer of data from. * @return the converted object id. */ - public static final ObjectId fromRaw(final int[] is, final int p) { + public static final ObjectId fromRaw(int[] is, int p) { return new ObjectId(is[p], is[p + 1], is[p + 2], is[p + 3], is[p + 4]); } @@ -216,7 +204,7 @@ public class ObjectId extends AnyObjectId implements Serializable { * position to read the first character from. * @return the converted object id. */ - public static final ObjectId fromString(final byte[] buf, final int offset) { + public static final ObjectId fromString(byte[] buf, int offset) { return fromHexString(buf, offset); } @@ -227,13 +215,14 @@ public class ObjectId extends AnyObjectId implements Serializable { * the string to read from. Must be 40 characters long. * @return the converted object id. */ - public static ObjectId fromString(final String str) { - if (str.length() != Constants.OBJECT_ID_STRING_LENGTH) - throw new IllegalArgumentException("Invalid id: " + str); + public static ObjectId fromString(String str) { + if (str.length() != Constants.OBJECT_ID_STRING_LENGTH) { + throw new InvalidObjectIdException(str); + } return fromHexString(Constants.encodeASCII(str), 0); } - private static final ObjectId fromHexString(final byte[] bs, int p) { + private static final ObjectId fromHexString(byte[] bs, int p) { try { final int a = RawParseUtils.parseHexInt32(bs, p); final int b = RawParseUtils.parseHexInt32(bs, p + 8); @@ -241,14 +230,30 @@ public class ObjectId extends AnyObjectId implements Serializable { final int d = RawParseUtils.parseHexInt32(bs, p + 24); final int e = RawParseUtils.parseHexInt32(bs, p + 32); return new ObjectId(a, b, c, d, e); - } catch (ArrayIndexOutOfBoundsException e1) { - throw new InvalidObjectIdException(bs, p, + } catch (ArrayIndexOutOfBoundsException e) { + InvalidObjectIdException e1 = new InvalidObjectIdException(bs, p, Constants.OBJECT_ID_STRING_LENGTH); + e1.initCause(e); + throw e1; } } - ObjectId(final int new_1, final int new_2, final int new_3, - final int new_4, final int new_5) { + /** + * Construct an ObjectId from 160 bits provided in 5 words. + * + * @param new_1 + * an int + * @param new_2 + * an int + * @param new_3 + * an int + * @param new_4 + * an int + * @param new_5 + * an int + * @since 4.7 + */ + public ObjectId(int new_1, int new_2, int new_3, int new_4, int new_5) { w1 = new_1; w2 = new_2; w3 = new_3; @@ -266,7 +271,7 @@ public class ObjectId extends AnyObjectId implements Serializable { * @param src * another already parsed ObjectId to copy the value out of. */ - protected ObjectId(final AnyObjectId src) { + protected ObjectId(AnyObjectId src) { w1 = src.w1; w2 = src.w2; w3 = src.w3; |