Refactor common network byte order decode functions to utility class
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / lib / MutableObjectId.java
blob202f4e4973d7d412b868b9dd907e4a521a546965
1 /*
2 * Copyright (C) 2008 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License, version 2, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org.spearce.jgit.lib;
19 import java.io.UnsupportedEncodingException;
21 import org.spearce.jgit.util.NB;
23 /**
24 * A mutable SHA-1 abstraction.
26 public class MutableObjectId extends AnyObjectId {
27 /**
28 * Convert an ObjectId from raw binary representation.
30 * @param bs
31 * the raw byte buffer to read from. At least 20 bytes must be
32 * available within this byte array.
34 public void fromRaw(final byte[] bs) {
35 fromRaw(bs, 0);
38 /**
39 * Convert an ObjectId from raw binary representation.
41 * @param bs
42 * the raw byte buffer to read from. At least 20 bytes after p
43 * must be available within this byte array.
44 * @param p
45 * position to read the first byte of data from.
47 public void fromRaw(final byte[] bs, final int p) {
48 w1 = NB.decodeInt32(bs, p);
49 w2 = NB.decodeInt32(bs, p + 4);
50 w3 = NB.decodeInt32(bs, p + 8);
51 w4 = NB.decodeInt32(bs, p + 12);
52 w5 = NB.decodeInt32(bs, p + 16);
55 /**
56 * Convert an ObjectId from hex characters (US-ASCII).
58 * @param buf
59 * the US-ASCII buffer to read from. At least 40 bytes after
60 * offset must be available within this byte array.
61 * @param offset
62 * position to read the first character from.
64 public void fromString(final byte[] buf, final int offset) {
65 fromHexString(buf, offset);
68 /**
69 * Convert an ObjectId from hex characters.
71 * @param str
72 * the string to read from. Must be 40 characters long.
74 public void fromString(final String str) {
75 if (str.length() != STR_LEN)
76 throw new IllegalArgumentException("Invalid id: " + str);
77 fromHexString(Constants.encodeASCII(str), 0);
80 private void fromHexString(final byte[] bs, int p) {
81 try {
82 w1 = hexUInt32(bs, p);
83 w2 = hexUInt32(bs, p + 8);
84 w3 = hexUInt32(bs, p + 16);
85 w4 = hexUInt32(bs, p + 24);
86 w5 = hexUInt32(bs, p + 32);
87 } catch (ArrayIndexOutOfBoundsException e1) {
88 try {
89 final String str = new String(bs, p, STR_LEN, "US-ASCII");
90 throw new IllegalArgumentException("Invalid id: " + str);
91 } catch (UnsupportedEncodingException e2) {
92 throw new IllegalArgumentException("Invalid id");
97 @Override
98 public ObjectId toObjectId() {
99 return new ObjectId(this);