comparison src/com/tmate/hgkit/ll/Nodeid.java @ 9:d6d2a630f4a6

Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Sat, 25 Dec 2010 04:45:59 +0100
parents dbd663faec1f
children df8c67f3006a
comparison
equal deleted inserted replaced
8:a78c980749e3 9:d6d2a630f4a6
1 /** 1 /**
2 * Copyright (c) 2010 Artem Tikhomirov 2 * Copyright (c) 2010 Artem Tikhomirov
3 */ 3 */
4 package com.tmate.hgkit.ll; 4 package com.tmate.hgkit.ll;
5 5
6 import java.math.BigInteger;
7 import java.util.Formatter;
8 6
9 /** 7 /**
10 * @see mercurial/node.py 8 * @see mercurial/node.py
11 * @author artem 9 * @author artem
12 * 10 *
22 this.binaryData = binaryRepresentation; 20 this.binaryData = binaryRepresentation;
23 } 21 }
24 22
25 @Override 23 @Override
26 public String toString() { 24 public String toString() {
27 // FIXME temp impl. 25 return new DigestHelper().toHexString(binaryData, 0, 20);
28 // BEWARE, if binaryData[0] > 0x80, BigInteger treats it as negative
29 return new BigInteger(binaryData).toString();
30 } 26 }
31 27
32 // binascii.unhexlify() 28 // binascii.unhexlify()
33 public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) { 29 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 30 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 31 byte[] data = new byte[length >>> 1]; // XXX use known size instead? nodeid is always 20 bytes
36 for (int i = 0, j = offset; i < data.length; i++) { 32 for (int i = 0, j = offset; i < data.length; i++) {
37 int hiNibble = Character.digit(asciiRepresentation[j++], 16); 33 int hiNibble = Character.digit(asciiRepresentation[j++], 16);
38 int lowNibble = Character.digit(asciiRepresentation[j++], 16); 34 int lowNibble = Character.digit(asciiRepresentation[j++], 16);
39 data[i] = (byte) (((hiNibble << 4) | lowNibble) & 0xFF); 35 data[i] = (byte) (((hiNibble << 4) | lowNibble) & 0xFF);
40 } 36 }