Switch jgit library to the EDL (3-clause BSD)
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / lib / MutableObjectId.java
blobb23d36c09cf95814e7c624464c8cfb36c2e2e393
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.lib;
40 import java.io.UnsupportedEncodingException;
42 import org.spearce.jgit.util.NB;
44 /**
45 * A mutable SHA-1 abstraction.
47 public class MutableObjectId extends AnyObjectId {
48 /**
49 * Convert an ObjectId from raw binary representation.
51 * @param bs
52 * the raw byte buffer to read from. At least 20 bytes must be
53 * available within this byte array.
55 public void fromRaw(final byte[] bs) {
56 fromRaw(bs, 0);
59 /**
60 * Convert an ObjectId from raw binary representation.
62 * @param bs
63 * the raw byte buffer to read from. At least 20 bytes after p
64 * must be available within this byte array.
65 * @param p
66 * position to read the first byte of data from.
68 public void fromRaw(final byte[] bs, final int p) {
69 w1 = NB.decodeInt32(bs, p);
70 w2 = NB.decodeInt32(bs, p + 4);
71 w3 = NB.decodeInt32(bs, p + 8);
72 w4 = NB.decodeInt32(bs, p + 12);
73 w5 = NB.decodeInt32(bs, p + 16);
76 /**
77 * Convert an ObjectId from hex characters (US-ASCII).
79 * @param buf
80 * the US-ASCII buffer to read from. At least 40 bytes after
81 * offset must be available within this byte array.
82 * @param offset
83 * position to read the first character from.
85 public void fromString(final byte[] buf, final int offset) {
86 fromHexString(buf, offset);
89 /**
90 * Convert an ObjectId from hex characters.
92 * @param str
93 * the string to read from. Must be 40 characters long.
95 public void fromString(final String str) {
96 if (str.length() != STR_LEN)
97 throw new IllegalArgumentException("Invalid id: " + str);
98 fromHexString(Constants.encodeASCII(str), 0);
101 private void fromHexString(final byte[] bs, int p) {
102 try {
103 w1 = hexUInt32(bs, p);
104 w2 = hexUInt32(bs, p + 8);
105 w3 = hexUInt32(bs, p + 16);
106 w4 = hexUInt32(bs, p + 24);
107 w5 = hexUInt32(bs, p + 32);
108 } catch (ArrayIndexOutOfBoundsException e1) {
109 try {
110 final String str = new String(bs, p, STR_LEN, "US-ASCII");
111 throw new IllegalArgumentException("Invalid id: " + str);
112 } catch (UnsupportedEncodingException e2) {
113 throw new IllegalArgumentException("Invalid id");
118 @Override
119 public ObjectId toObjectId() {
120 return new ObjectId(this);