comparison src/com/tmate/hgkit/ll/Nodeid.java @ 0:dbd663faec1f

Basic changelog parsing
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 17 Dec 2010 19:05:59 +0100
parents
children d6d2a630f4a6
comparison
equal deleted inserted replaced
-1:000000000000 0:dbd663faec1f
1 /**
2 * Copyright (c) 2010 Artem Tikhomirov
3 */
4 package com.tmate.hgkit.ll;
5
6 import java.math.BigInteger;
7 import java.util.Formatter;
8
9 /**
10 * @see mercurial/node.py
11 * @author artem
12 *
13 */
14 public class Nodeid {
15
16 public static int NULLREV = -1;
17 private final byte[] binaryData;
18
19 public Nodeid(byte[] binaryRepresentation) {
20 // 5 int fields => 32 bytes
21 // byte[20] => 48 bytes
22 this.binaryData = binaryRepresentation;
23 }
24
25 @Override
26 public String toString() {
27 // FIXME temp impl.
28 // BEWARE, if binaryData[0] > 0x80, BigInteger treats it as negative
29 return new BigInteger(binaryData).toString();
30 }
31
32 // binascii.unhexlify()
33 public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) {
34 assert length % 2 == 0; // Python's binascii.hexlify convert each byte into 2 digits
35 byte[] data = new byte[length / 2]; // XXX use known size instead? nodeid is always 20 bytes
36 for (int i = 0, j = offset; i < data.length; i++) {
37 int hiNibble = Character.digit(asciiRepresentation[j++], 16);
38 int lowNibble = Character.digit(asciiRepresentation[j++], 16);
39 data[i] = (byte) (((hiNibble << 4) | lowNibble) & 0xFF);
40 }
41 return new Nodeid(data);
42 }
43
44
45 }