Move hex parsing functions to RawParseUtil, accept upper case
[egit.git] / org.spearce.jgit / src / org / spearce / jgit / lib / MutableObjectId.java
blob805f3281d47195f98a292aad995dbac71c634f36
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;
43 import org.spearce.jgit.util.RawParseUtils;
45 /**
46 * A mutable SHA-1 abstraction.
48 public class MutableObjectId extends AnyObjectId {
49 /**
50 * Empty constructor. Initialize object with default (zeros) value.
52 public MutableObjectId() {
53 super();
56 /**
57 * Copying constructor.
59 * @param src
60 * original entry, to copy id from
62 MutableObjectId(MutableObjectId src) {
63 this.w1 = src.w1;
64 this.w2 = src.w2;
65 this.w3 = src.w3;
66 this.w4 = src.w4;
67 this.w5 = src.w5;
70 /** Make this id match {@link ObjectId#zeroId()}. */
71 public void clear() {
72 w1 = 0;
73 w2 = 0;
74 w3 = 0;
75 w4 = 0;
76 w5 = 0;
79 /**
80 * Convert an ObjectId from raw binary representation.
82 * @param bs
83 * the raw byte buffer to read from. At least 20 bytes must be
84 * available within this byte array.
86 public void fromRaw(final byte[] bs) {
87 fromRaw(bs, 0);
90 /**
91 * Convert an ObjectId from raw binary representation.
93 * @param bs
94 * the raw byte buffer to read from. At least 20 bytes after p
95 * must be available within this byte array.
96 * @param p
97 * position to read the first byte of data from.
99 public void fromRaw(final byte[] bs, final int p) {
100 w1 = NB.decodeInt32(bs, p);
101 w2 = NB.decodeInt32(bs, p + 4);
102 w3 = NB.decodeInt32(bs, p + 8);
103 w4 = NB.decodeInt32(bs, p + 12);
104 w5 = NB.decodeInt32(bs, p + 16);
108 * Convert an ObjectId from binary representation expressed in integers.
110 * @param ints
111 * the raw int buffer to read from. At least 5 integers must be
112 * available within this integers array.
114 public void fromRaw(final int[] ints) {
115 fromRaw(ints, 0);
119 * Convert an ObjectId from binary representation expressed in integers.
121 * @param ints
122 * the raw int buffer to read from. At least 5 integers after p
123 * must be available within this integers array.
124 * @param p
125 * position to read the first integer of data from.
128 public void fromRaw(final int[] ints, final int p) {
129 w1 = ints[p];
130 w2 = ints[p + 1];
131 w3 = ints[p + 2];
132 w4 = ints[p + 3];
133 w5 = ints[p + 4];
137 * Convert an ObjectId from hex characters (US-ASCII).
139 * @param buf
140 * the US-ASCII buffer to read from. At least 40 bytes after
141 * offset must be available within this byte array.
142 * @param offset
143 * position to read the first character from.
145 public void fromString(final byte[] buf, final int offset) {
146 fromHexString(buf, offset);
150 * Convert an ObjectId from hex characters.
152 * @param str
153 * the string to read from. Must be 40 characters long.
155 public void fromString(final String str) {
156 if (str.length() != STR_LEN)
157 throw new IllegalArgumentException("Invalid id: " + str);
158 fromHexString(Constants.encodeASCII(str), 0);
161 private void fromHexString(final byte[] bs, int p) {
162 try {
163 w1 = RawParseUtils.parseHexInt32(bs, p);
164 w2 = RawParseUtils.parseHexInt32(bs, p + 8);
165 w3 = RawParseUtils.parseHexInt32(bs, p + 16);
166 w4 = RawParseUtils.parseHexInt32(bs, p + 24);
167 w5 = RawParseUtils.parseHexInt32(bs, p + 32);
168 } catch (ArrayIndexOutOfBoundsException e1) {
169 try {
170 final String str = new String(bs, p, STR_LEN, "US-ASCII");
171 throw new IllegalArgumentException("Invalid id: " + str);
172 } catch (UnsupportedEncodingException e2) {
173 throw new IllegalArgumentException("Invalid id");
174 } catch (StringIndexOutOfBoundsException e2) {
175 throw new IllegalArgumentException("Invalid id");
180 @Override
181 public ObjectId toObjectId() {
182 return new ObjectId(this);