1.0 preparations
This commit is contained in:
parent
55717c6e71
commit
8fa82633d4
34 changed files with 742 additions and 462 deletions
|
|
@ -19,7 +19,7 @@ package btools.util;
|
|||
// 001 -> 3 + following 2-bit word ( 3..6 )
|
||||
// 0001 -> 7 + following 3-bit word ( 7..14 ) etc.
|
||||
|
||||
public void encodeDistance( int value )
|
||||
public void encodeVarBits( int value )
|
||||
{
|
||||
int range = 0;
|
||||
while ( value > range )
|
||||
|
|
@ -33,7 +33,7 @@ package btools.util;
|
|||
}
|
||||
|
||||
// twin to encodeDistance
|
||||
public int decodeDistance()
|
||||
public int decodeVarBits()
|
||||
{
|
||||
int range = 0;
|
||||
int value = 0;
|
||||
|
|
|
|||
|
|
@ -9,7 +9,11 @@ public final class ByteArrayUnifier
|
|||
public ByteArrayUnifier( int size, boolean validateImmutability )
|
||||
{
|
||||
this.size = size;
|
||||
byteArrayCache = new byte[size][];
|
||||
|
||||
if ( !Boolean.getBoolean( "disableByteArrayUnifification" ) )
|
||||
{
|
||||
byteArrayCache = new byte[size][];
|
||||
}
|
||||
if ( validateImmutability ) crcCrosscheck = new int[size];
|
||||
}
|
||||
|
||||
|
|
@ -22,6 +26,8 @@ public final class ByteArrayUnifier
|
|||
*/
|
||||
public byte[] unify( byte[] ab )
|
||||
{
|
||||
if ( byteArrayCache == null ) return ab;
|
||||
|
||||
int n = ab.length;
|
||||
int crc = Crc32.crc( ab, 0, n );
|
||||
int idx = (crc & 0xfffffff) % size;
|
||||
|
|
|
|||
95
brouter-util/src/main/java/btools/util/ByteDataReader.java
Normal file
95
brouter-util/src/main/java/btools/util/ByteDataReader.java
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
/**
|
||||
* fast data-reading from a byte-array
|
||||
*
|
||||
* @author ab
|
||||
*/
|
||||
package btools.util;
|
||||
|
||||
|
||||
public class ByteDataReader
|
||||
{
|
||||
protected byte[] ab;
|
||||
protected int aboffset;
|
||||
|
||||
public ByteDataReader( byte[] byteArray )
|
||||
{
|
||||
ab = byteArray;
|
||||
}
|
||||
|
||||
public final int readInt()
|
||||
{
|
||||
int i3 = ab[aboffset++]& 0xff;
|
||||
int i2 = ab[aboffset++]& 0xff;
|
||||
int i1 = ab[aboffset++]& 0xff;
|
||||
int i0 = ab[aboffset++]& 0xff;
|
||||
return (i3 << 24) + (i2 << 16) + (i1 << 8) + i0;
|
||||
}
|
||||
|
||||
public final long readLong()
|
||||
{
|
||||
long i7 = ab[aboffset++]& 0xff;
|
||||
long i6 = ab[aboffset++]& 0xff;
|
||||
long i5 = ab[aboffset++]& 0xff;
|
||||
long i4 = ab[aboffset++]& 0xff;
|
||||
long i3 = ab[aboffset++]& 0xff;
|
||||
long i2 = ab[aboffset++]& 0xff;
|
||||
long i1 = ab[aboffset++]& 0xff;
|
||||
long i0 = ab[aboffset++]& 0xff;
|
||||
return (i7 << 56) + (i6 << 48) + (i5 << 40) + (i4 << 32) + (i3 << 24) + (i2 << 16) + (i1 << 8) + i0;
|
||||
}
|
||||
|
||||
public final boolean readBoolean()
|
||||
{
|
||||
int i0 = ab[aboffset++]& 0xff;
|
||||
return i0 != 0;
|
||||
}
|
||||
|
||||
public final byte readByte()
|
||||
{
|
||||
int i0 = ab[aboffset++] & 0xff;
|
||||
return (byte)(i0);
|
||||
}
|
||||
|
||||
public final short readShort()
|
||||
{
|
||||
int i1 = ab[aboffset++] & 0xff;
|
||||
int i0 = ab[aboffset++] & 0xff;
|
||||
return (short)( (i1 << 8) | i0);
|
||||
}
|
||||
|
||||
public final int readVarLengthSigned()
|
||||
{
|
||||
int v = readVarLengthUnsigned();
|
||||
return ( v & 1 ) == 0 ? v >> 1 : -(v >> 1 );
|
||||
}
|
||||
|
||||
public final int readVarLengthUnsigned()
|
||||
{
|
||||
int v = 0;
|
||||
int shift = 0;
|
||||
for(;;)
|
||||
{
|
||||
int i7 = ab[aboffset++] & 0xff;
|
||||
v |= (( i7 & 0x7f ) << shift);
|
||||
if ( ( i7 & 0x80 ) == 0 ) break;
|
||||
shift += 7;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
public final void readFully( byte[] ta )
|
||||
{
|
||||
System.arraycopy( ab, aboffset, ta, 0, ta.length );
|
||||
aboffset += ta.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder( "[" );
|
||||
for( int i=0; i<ab.length; i++ ) sb.append( i == 0 ? " " : ", " ).append( Integer.toString( ab[i] ) );
|
||||
sb.append( " ]" );
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
111
brouter-util/src/main/java/btools/util/ByteDataWriter.java
Normal file
111
brouter-util/src/main/java/btools/util/ByteDataWriter.java
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
/**
|
||||
* fast data-writing to a byte-array
|
||||
*
|
||||
* @author ab
|
||||
*/
|
||||
package btools.util;
|
||||
|
||||
|
||||
public final class ByteDataWriter
|
||||
{
|
||||
private byte[] ab;
|
||||
private int aboffset;
|
||||
|
||||
public ByteDataWriter( byte[] byteArray )
|
||||
{
|
||||
ab = byteArray;
|
||||
}
|
||||
|
||||
public void writeInt( int v )
|
||||
{
|
||||
ab[aboffset++] = (byte)( (v >> 24) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 16) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 8) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v ) & 0xff );
|
||||
}
|
||||
|
||||
public void writeLong( long v )
|
||||
{
|
||||
ab[aboffset++] = (byte)( (v >> 56) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 48) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 40) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 32) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 24) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 16) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 8) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v ) & 0xff );
|
||||
}
|
||||
|
||||
public void writeBoolean( boolean v)
|
||||
{
|
||||
ab[aboffset++] = (byte)( v ? 1 : 0 );
|
||||
}
|
||||
|
||||
public void writeByte( int v )
|
||||
{
|
||||
ab[aboffset++] = (byte)( (v ) & 0xff );
|
||||
}
|
||||
|
||||
public void writeShort( int v )
|
||||
{
|
||||
ab[aboffset++] = (byte)( (v >> 8) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v ) & 0xff );
|
||||
}
|
||||
|
||||
public void write( byte[] sa )
|
||||
{
|
||||
System.arraycopy( sa, 0, ab, aboffset, sa.length );
|
||||
aboffset += sa.length;
|
||||
}
|
||||
|
||||
public void write( byte[] sa, int offset, int len )
|
||||
{
|
||||
System.arraycopy( sa, offset, ab, aboffset, len );
|
||||
aboffset += len;
|
||||
}
|
||||
|
||||
public void ensureCapacity( int len )
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
public byte[] toByteArray()
|
||||
{
|
||||
byte[] c = new byte[aboffset];
|
||||
System.arraycopy( ab, 0, c, 0, aboffset );
|
||||
return c;
|
||||
}
|
||||
|
||||
public int writeVarLengthSigned( int v )
|
||||
{
|
||||
return writeVarLengthUnsigned( v < 0 ? ( (-v) << 1 ) | 1 : v << 1 );
|
||||
}
|
||||
|
||||
public int writeVarLengthUnsigned( int v )
|
||||
{
|
||||
int start = aboffset;
|
||||
do
|
||||
{
|
||||
int i7 = v & 0x7f;
|
||||
v >>= 7;
|
||||
if ( v != 0 ) i7 |= 0x80;
|
||||
ab[aboffset++] = (byte)( i7 & 0xff );
|
||||
}
|
||||
while( v != 0 );
|
||||
return aboffset - start;
|
||||
}
|
||||
|
||||
public int size()
|
||||
{
|
||||
return aboffset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder( "[" );
|
||||
for( int i=0; i<ab.length; i++ ) sb.append( i == 0 ? " " : ", " ).append( Integer.toString( ab[i] ) );
|
||||
sb.append( " ]" );
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
|
@ -22,7 +22,8 @@ public class Crc32
|
|||
public static int crc( byte[] ab, int offset, int len )
|
||||
{
|
||||
int crc = 0xFFFFFFFF;
|
||||
for( int j=offset; j<len; j++ )
|
||||
int end = offset + len;
|
||||
for( int j=offset; j<end; j++ )
|
||||
{
|
||||
crc = (crc >>> 8) ^ crctable[(crc ^ ab[j]) & 0xff];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
/**
|
||||
* DataInputStream extended by varlength diff coding
|
||||
*
|
||||
* @author ab
|
||||
*/
|
||||
package btools.util;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
|
||||
public final class DiffCoderDataInputStream extends DataInputStream
|
||||
{
|
||||
private long[] lastValues = new long[10];
|
||||
|
||||
public DiffCoderDataInputStream( InputStream is )
|
||||
{
|
||||
super( is );
|
||||
}
|
||||
|
||||
public long readDiffed( int idx ) throws IOException
|
||||
{
|
||||
long d = readSigned();
|
||||
long v = lastValues[idx] + d;
|
||||
lastValues[idx] = v;
|
||||
return v;
|
||||
}
|
||||
|
||||
public long readSigned() throws IOException
|
||||
{
|
||||
long v = readUnsigned();
|
||||
return ( v & 1 ) == 0 ? v >> 1 : -(v >> 1 );
|
||||
}
|
||||
|
||||
public long readUnsigned() throws IOException
|
||||
{
|
||||
long v = 0;
|
||||
int shift = 0;
|
||||
for(;;)
|
||||
{
|
||||
long i7 = readByte() & 0xff;
|
||||
v |= (( i7 & 0x7f ) << shift);
|
||||
if ( ( i7 & 0x80 ) == 0 ) break;
|
||||
shift += 7;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
/**
|
||||
* DataOutputStream extended by varlength diff coding
|
||||
*
|
||||
* @author ab
|
||||
*/
|
||||
package btools.util;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
|
||||
public final class DiffCoderDataOutputStream extends DataOutputStream
|
||||
{
|
||||
private long[] lastValues = new long[10];
|
||||
|
||||
public DiffCoderDataOutputStream( OutputStream os )
|
||||
{
|
||||
super( os );
|
||||
}
|
||||
|
||||
public void writeDiffed( long v, int idx ) throws IOException
|
||||
{
|
||||
long d = v - lastValues[idx];
|
||||
lastValues[idx] = v;
|
||||
writeSigned( d );
|
||||
}
|
||||
|
||||
public void writeSigned( long v ) throws IOException
|
||||
{
|
||||
writeUnsigned( v < 0 ? ( (-v) << 1 ) | 1 : v << 1 );
|
||||
}
|
||||
|
||||
public void writeUnsigned( long v ) throws IOException
|
||||
{
|
||||
do
|
||||
{
|
||||
long i7 = v & 0x7f;
|
||||
v >>= 7;
|
||||
if ( v != 0 ) i7 |= 0x80;
|
||||
writeByte( (byte)( i7 & 0xff ) );
|
||||
}
|
||||
while( v != 0 );
|
||||
}
|
||||
}
|
||||
|
|
@ -9,19 +9,19 @@ import org.junit.Test;
|
|||
public class BitCoderContextTest
|
||||
{
|
||||
@Test
|
||||
public void distanceEncodeDecodeTest()
|
||||
public void varBitsEncodeDecodeTest()
|
||||
{
|
||||
byte[] ab = new byte[4000];
|
||||
BitCoderContext ctx = new BitCoderContext( ab );
|
||||
for( int i=0; i<1000; i++ )
|
||||
{
|
||||
ctx.encodeDistance( i );
|
||||
ctx.encodeVarBits( i );
|
||||
}
|
||||
ctx = new BitCoderContext( ab );
|
||||
|
||||
for( int i=0; i<1000; i++ )
|
||||
{
|
||||
int value = ctx.decodeDistance();
|
||||
int value = ctx.decodeVarBits();
|
||||
Assert.assertTrue( "distance value mismatch", value == i );
|
||||
}
|
||||
}
|
||||
|
|
|
|||
28
brouter-util/src/test/java/btools/util/ByteDataIOTest.java
Normal file
28
brouter-util/src/test/java/btools/util/ByteDataIOTest.java
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package btools.util;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.HashSet;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ByteDataIOTest
|
||||
{
|
||||
@Test
|
||||
public void varLengthEncodeDecodeTest()
|
||||
{
|
||||
byte[] ab = new byte[4000];
|
||||
ByteDataWriter w = new ByteDataWriter( ab );
|
||||
for( int i=0; i<1000; i++ )
|
||||
{
|
||||
w.writeVarLengthUnsigned( i );
|
||||
}
|
||||
ByteDataReader r = new ByteDataReader( ab );
|
||||
|
||||
for( int i=0; i<1000; i++ )
|
||||
{
|
||||
int value = r.readVarLengthUnsigned();
|
||||
Assert.assertTrue( "value mismatch", value == i );
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue