Merge branch 'master' into thread-safety

This commit is contained in:
Gianluigi 2014-08-27 12:50:22 +02:00
commit 3c106c7dbf
3 changed files with 80 additions and 6 deletions

View File

@ -385,10 +385,10 @@ public class Repository {
String fileName = "";
if (txHash != null)
fileName = String.format("%d_%d_%s.dmp", block.getNumber(), txNumber,
fileName = String.format("%05d_%d_%s.dmp", block.getNumber(), txNumber,
Hex.toHexString(txHash).substring(0, 8));
else
fileName = String.format("%d_c.dmp", block.getNumber());
fileName = String.format("%05d_c.dmp", block.getNumber());
File dumpFile = new File(System.getProperty("user.dir") + "/" + dir + fileName);
FileWriter fw = null;

View File

@ -9,6 +9,8 @@ import static org.spongycastle.util.encoders.Hex.encode;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
/**
* Compact encoding of hex sequence with optional terminator
@ -47,7 +49,26 @@ import java.nio.ByteBuffer;
public class CompactEncoder {
private final static byte TERMINATOR = 16;
private final static String hexBase = "0123456789abcdef";
private final static Map<Character, Byte> hexMap = new HashMap();
static {
hexMap.put('0', (byte)0);
hexMap.put('1', (byte)1);
hexMap.put('2', (byte)2);
hexMap.put('3', (byte)3);
hexMap.put('4', (byte)4);
hexMap.put('5', (byte)5);
hexMap.put('6', (byte)6);
hexMap.put('7', (byte)7);
hexMap.put('8', (byte)8);
hexMap.put('9', (byte)9);
hexMap.put('a', (byte)10);
hexMap.put('b', (byte)11);
hexMap.put('c', (byte)12);
hexMap.put('d', (byte)13);
hexMap.put('e', (byte)14);
hexMap.put('f', (byte)15);
}
/**
* Pack nibbles to binary
@ -107,9 +128,30 @@ public class CompactEncoder {
byte[] hexEncoded = encode(str);
ByteBuffer slice = ByteBuffer.allocate(hexEncoded.length + 1);
for (byte b : hexEncoded) {
slice.put((byte)hexBase.indexOf(b));
slice.put(hexMap.get((char)b));
}
slice.put(TERMINATOR);
return slice.array();
}
/**
* turn nibbles to a nice good looking output string
*
* @param nibbles - getting byte of data [ 04 ] and turning
* it to a '\x04' representation
* @return
*/
public static String nibblesToPrettyString(byte[] nibbles){
StringBuffer buffer = new StringBuffer();
for (byte nibble : nibbles){
String nibleString = Utils.oneByteToHexString(nibble);
buffer.append("\\x" + nibleString);
}
return buffer.toString();
}
}

View File

@ -66,9 +66,41 @@ public class CompactEncoderTest {
}
@Test
public void testCompactHexDecode() {
public void testCompactHexEncode_1() {
byte[] test = "stallion".getBytes();
byte[] result = new byte[] { 7, 3, 7, 4, 6, 1, 6, 12, 6, 12, 6, 9, 6, 15, 6, 14, T };
assertArrayEquals(result, CompactEncoder.binToNibbles(test));
}
@Test
public void testCompactHexEncode_2() {
byte[] test = "verb".getBytes();
byte[] result = new byte[] { 7, 6, 6, 5, 7, 2, 6, 2, T };
assertArrayEquals(result, CompactEncoder.binToNibbles(test));
}
@Test
public void testCompactHexEncode_3() {
byte[] test = "puppy".getBytes();
byte[] result = new byte[] { 7, 0, 7, 5, 7, 0, 7, 0, 7, 9, T };
assertArrayEquals(result, CompactEncoder.binToNibbles(test));
}
@Test
public void testNiceNiblesOutput_1(){
byte[] test = {7, 0, 7, 5, 7, 0, 7, 0, 7, 9};
String result = "\\x07\\x00\\x07\\x05\\x07\\x00\\x07\\x00\\x07\\x09";
assertEquals(result, CompactEncoder.nibblesToPrettyString(test));
}
@Test
public void testNiceNiblesOutput_2(){
byte[] test = {7, 0, 7, 0xf, 7, 0, 0xa, 0, 7, 9};
String result = "\\x07\\x00\\x07\\x0f\\x07\\x00\\x0a\\x00\\x07\\x09";
assertEquals(result, CompactEncoder.nibblesToPrettyString(test));
}
}