statistical encoding
This commit is contained in:
parent
f8dee5b7d1
commit
ccf6641bad
41 changed files with 4543 additions and 1965 deletions
|
|
@ -6,33 +6,30 @@
|
|||
package btools.util;
|
||||
|
||||
|
||||
public final class ByteDataWriter
|
||||
public class ByteDataWriter extends ByteDataReader
|
||||
{
|
||||
private byte[] ab;
|
||||
private int aboffset;
|
||||
|
||||
public ByteDataWriter( byte[] byteArray )
|
||||
{
|
||||
ab = byteArray;
|
||||
super ( byteArray );
|
||||
}
|
||||
|
||||
public void writeInt( int v )
|
||||
{
|
||||
ab[aboffset++] = (byte)( (v >> 24) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 24) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 16) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 8) & 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 >> 56) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 48) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 40) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 40) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 32) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 24) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 24) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 16) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 8) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v >> 8) & 0xff );
|
||||
ab[aboffset++] = (byte)( (v ) & 0xff );
|
||||
}
|
||||
|
||||
|
|
@ -51,31 +48,85 @@ public final class ByteDataWriter
|
|||
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 )
|
||||
public void writeVarBytes( byte[] sa )
|
||||
{
|
||||
// TODO
|
||||
if ( sa == null )
|
||||
{
|
||||
writeVarLengthUnsigned( 0 );
|
||||
}
|
||||
else
|
||||
{
|
||||
int len = sa.length;
|
||||
writeVarLengthUnsigned( len );
|
||||
write( sa, 0, len );
|
||||
}
|
||||
}
|
||||
|
||||
public void writeModeAndDesc( boolean isReverse, byte[] sa )
|
||||
{
|
||||
int len = sa == null ? 0 : sa.length;
|
||||
int sizecode = len << 1 | ( isReverse ? 1 : 0 );
|
||||
writeVarLengthUnsigned( sizecode );
|
||||
if ( len > 0 )
|
||||
{
|
||||
write( sa, 0, len );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public byte[] toByteArray()
|
||||
{
|
||||
byte[] c = new byte[aboffset];
|
||||
System.arraycopy( ab, 0, c, 0, aboffset );
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Just reserves a single byte and return it' offset.
|
||||
* Used in conjunction with injectVarLengthUnsigned
|
||||
* to efficiently write a size prefix
|
||||
*
|
||||
* @return the offset of the placeholder
|
||||
*/
|
||||
public int writeSizePlaceHolder()
|
||||
{
|
||||
return aboffset++;
|
||||
}
|
||||
|
||||
public void injectSize( int sizeoffset )
|
||||
{
|
||||
int size = 0;
|
||||
int datasize = aboffset-sizeoffset-1;
|
||||
int v = datasize;
|
||||
do
|
||||
{
|
||||
v >>= 7;
|
||||
size++;
|
||||
}
|
||||
while( v != 0 );
|
||||
if ( size > 1 ) // doesn't fit -> shift the data after the placeholder
|
||||
{
|
||||
System.arraycopy( ab, sizeoffset+1, ab, sizeoffset+size, datasize );
|
||||
}
|
||||
aboffset = sizeoffset;
|
||||
writeVarLengthUnsigned( datasize );
|
||||
aboffset = sizeoffset + size + datasize;
|
||||
}
|
||||
|
||||
public int writeVarLengthSigned( int v )
|
||||
{
|
||||
return writeVarLengthUnsigned( v < 0 ? ( (-v) << 1 ) | 1 : v << 1 );
|
||||
|
|
@ -83,29 +134,21 @@ public final class ByteDataWriter
|
|||
|
||||
public int writeVarLengthUnsigned( int v )
|
||||
{
|
||||
int start = aboffset;
|
||||
do
|
||||
{
|
||||
int i7 = v & 0x7f;
|
||||
v >>= 7;
|
||||
if ( v != 0 ) i7 |= 0x80;
|
||||
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;
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue