@ -1,9 +1,7 @@
// kelondrob yteArray.java
// B yteArray.java
// (C) 2007 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
// first published 30.03.2007 on http://yacy.net
//
// This is a part of YaCy, a peer-to-peer based web search engine
//
// $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
// $LastChangedRevision: 1986 $
// $LastChangedBy: orbiter $
@ -26,156 +24,40 @@
package de.anomic.kelondro.util ;
import java.io.IOException ;
import java.io.UnsupportedEncodingException ;
import java.util.HashMap ;
import de.anomic.kelondro.io.RandomAccessInterface ;
import de.anomic.kelondro.order.Base64Order ;
import de.anomic.kelondro.order.ByteOrder ;
import de.anomic.kelondro.order.NaturalOrder ;
// this class is a experimental replacement of byte[]. It should be used
// if frequent System.arraycopy usage is common for byte[] data types
// when replaced by this class, all copies share the same byte[] but can
// access a different part of the byte array
/ * *
* this class is a experimental replacement of byte [ ] .
* It can be used if a byte [ ] shall be stored within a HashMap or HashSet
* which is faster than TreeMap but is generally not possible because storing
* a byte [ ] in a Hashtable does not work because the hash computation does not
* work for byte [ ] . This class extends byte [ ] with a cached hashing function ,
* so it can be used in hashtables .
* /
public class ByteArray {
private byte [ ] buffer ;
private int offset ;
private int length ;
private int hash ;
public ByteArray ( final int initLength ) {
this . buffer = new byte [ initLength ] ;
this . length = 0 ;
this . offset = 0 ;
}
public ByteArray ( final byte [ ] bb ) {
this . buffer = bb ;
this . length = bb . length ;
this . offset = 0 ;
}
public ByteArray ( final byte [ ] bb , final int offset , final int length ) {
this . buffer = bb ;
this . length = length ;
this . offset = offset ;
}
public ByteArray ( final ByteArray ba , final int offset , final int length ) {
this . buffer = ba . buffer ;
this . length = length ;
this . offset = ba . offset + offset ;
}
public void ensureSize ( final int needed ) {
if ( buffer . length - offset > = needed ) return ;
byte [ ] newbuffer = new byte [ needed ] ;
System . arraycopy ( buffer , offset , newbuffer , 0 , length ) ;
buffer = newbuffer ;
offset = 0 ;
}
public void trim ( final int needed ) {
if ( buffer . length - offset < needed ) return ;
if ( buffer . length - offset = = length ) return ;
byte [ ] newbuffer = new byte [ needed ] ;
System . arraycopy ( buffer , offset , newbuffer , 0 , length ) ;
buffer = newbuffer ;
offset = 0 ;
}
public final void removeShift ( final int pos , final int dist , final int upBound ) {
assert ( pos + dist > = 0 ) : "pos = " + pos + ", dist = " + dist ;
assert ( pos > = 0 ) : "pos = " + pos ;
assert ( this . offset + upBound < = buffer . length ) : "upBound = " + upBound + ", buffer.length = " + buffer . length ;
assert ( this . offset + upBound - dist < = buffer . length ) : "dist = " + dist + ", upBound = " + upBound + ", buffer.length = " + buffer . length ;
System . arraycopy ( buffer , this . offset + pos + dist , buffer , this . offset + pos , upBound - pos - dist ) ;
}
public final void swap ( final int i , final int j , final int size ) {
if ( this . offset + this . length + size < = buffer . length ) {
// there is space in the chunkcache that we can use as buffer
System . arraycopy ( buffer , this . offset + size * i , buffer , buffer . length - size , size ) ;
System . arraycopy ( buffer , this . offset + size * j , buffer , this . offset + size * i , size ) ;
System . arraycopy ( buffer , buffer . length - size , buffer , this . offset + size * j , size ) ;
} else {
// allocate a chunk to use as buffer
final byte [ ] a = new byte [ size ] ;
System . arraycopy ( buffer , this . offset + size * i , a , 0 , size ) ;
System . arraycopy ( buffer , this . offset + size * j , buffer , this . offset + size * i , size ) ;
System . arraycopy ( a , 0 , buffer , this . offset + size * j , size ) ;
}
}
public void clear ( ) {
length = 0 ;
offset = 0 ;
this . hash = 0 ;
}
public int length ( ) {
return length;
return buffer . length ;
}
public byte [ ] asBytes ( ) {
final byte [ ] tmp = new byte [ length ] ;
System . arraycopy ( buffer , offset , tmp , 0 , length ) ;
return tmp ;
return this . buffer ;
}
public byte readByte ( final int pos ) {
return buffer [ this . offset + pos ] ;
}
public long readLongB64e ( final int pos , final int length ) {
return Base64Order . enhancedCoder . decodeLong ( buffer , this . offset + pos , length ) ;
}
public long readLongB256 ( final int pos , final int length ) {
return NaturalOrder . decodeLong ( buffer , this . offset + pos , length ) ;
}
public byte [ ] readBytes ( final int from_pos , final int length ) {
final byte [ ] buf = new byte [ length ] ;
System . arraycopy ( buffer , this . offset + from_pos , buf , 0 , length ) ;
return buf ;
}
public String readString ( final int from_pos , final int length ) {
try {
return new String ( buffer , this . offset + from_pos , length , "UTF-8" ) ;
} catch ( final UnsupportedEncodingException e ) {
return "" ;
}
}
public String readString ( final int from_pos , final int length , final String encoding ) {
try {
return new String ( buffer , this . offset + from_pos , length , encoding ) ;
} catch ( final UnsupportedEncodingException e ) {
return "" ;
}
}
public void readToRA ( final int from_pos , final RandomAccessInterface to_file , final int len ) throws IOException {
to_file . write ( this . buffer , from_pos , len ) ;
}
public void write ( final int to_position , final byte b ) {
buffer [ this . offset + to_position ] = b ;
}
public void write ( final int to_position , final byte [ ] from_array , final int from_offset , final int from_length ) {
System . arraycopy ( from_array , from_offset , this . buffer , this . offset + to_position , from_length ) ;
}
public void write ( final int to_position , final ByteArray from_array ) {
System . arraycopy ( from_array . buffer , from_array . offset , this . buffer , this . offset + to_position , from_array . length ) ;
}
public void write ( final int to_position , final ByteArray from_array , final int from_offset , final int from_length ) {
System . arraycopy ( from_array . buffer , from_array . offset + from_offset , this . buffer , this . offset + to_position , from_length ) ;
return buffer [ pos ] ;
}
public static boolean equals ( final byte [ ] buffer , final byte [ ] pattern ) {
@ -187,16 +69,50 @@ public class ByteArray {
return true ;
}
public void reset ( ) {
this . length = 0 ;
this . offset = 0 ;
}
public int compareTo ( final ByteArray b , final ByteOrder order ) {
return order . compare ( this . buffer , this . offset , this . length , b . buffer , b . offset , b . length ) ;
return order . compare ( this . buffer , 0 , this . buffer . length , b . buffer , 0 , b . buffer . length ) ;
}
public int compareTo ( final int aoffset , final int alength , final ByteArray b , final int boffset , final int blength , final ByteOrder order ) {
return order . compare ( this . buffer , this . offset + aoffset , alength , b . buffer , b . offset + boffset , blength ) ;
return order . compare ( this . buffer , aoffset , alength , b . buffer , boffset , blength ) ;
}
public int hashCode ( ) {
if ( this . hash ! = 0 ) return this . hash ;
int l = this . buffer . length ;
int h = 0 ;
while ( - - l > = 0 ) h = 31 * h + buffer [ l ] ;
this . hash = h ;
return h ;
}
public boolean equals ( Object other ) {
ByteArray b = ( ByteArray ) other ;
if ( buffer = = null & & b = = null ) return true ;
if ( buffer = = null | | b = = null ) return false ;
if ( this . buffer . length ! = b . buffer . length ) return false ;
int l = this . buffer . length ;
while ( - - l > = 0 ) if ( this . buffer [ l ] ! = b . buffer [ l ] ) return false ;
return true ;
}
public static void main ( String [ ] args ) {
ByteArray a0 = new ByteArray ( "abc" . getBytes ( ) ) ;
ByteArray a1 = new ByteArray ( "abc" . getBytes ( ) ) ;
ByteArray b = new ByteArray ( "bbb" . getBytes ( ) ) ;
System . out . println ( "a0 " + ( ( a0 . equals ( a1 ) ) ? "=" : "!=" ) + " a1" ) ;
System . out . println ( "a0 " + ( ( a0 . equals ( b ) ) ? "=" : "!=" ) + " b" ) ;
HashMap < ByteArray , Integer > map = new HashMap < ByteArray , Integer > ( ) ;
map . put ( a0 , 1 ) ;
//map.put(a1, 1);
map . put ( b , 2 ) ;
System . out . println ( "map.size() = " + map . size ( ) ) ;
System . out . println ( "hashCode(a0) = " + a0 . hashCode ( ) ) ;
System . out . println ( "hashCode(a1) = " + a1 . hashCode ( ) ) ;
System . out . println ( "hashCode(b) = " + b . hashCode ( ) ) ;
System . out . println ( "a0 " + ( ( map . containsKey ( a0 ) ) ? "in" : "not in" ) + " map" ) ;
System . out . println ( "a1 " + ( ( map . containsKey ( a1 ) ) ? "in" : "not in" ) + " map" ) ;
System . out . println ( "b " + ( ( map . containsKey ( b ) ) ? "in" : "not in" ) + " map" ) ;
}
}