Package org.apache.lucene.util
Class BytesRef
- java.lang.Object
-
- org.apache.lucene.util.BytesRef
-
- All Implemented Interfaces:
Cloneable,Comparable<BytesRef>
public final class BytesRef extends Object implements Comparable<BytesRef>, Cloneable
Represents byte[], as a slice (offset + length) into an existing byte[].Important note: Unless otherwise noted, Lucene uses this class to represent terms that are encoded as UTF8 bytes in the index. To convert them to a Java
String(which is UTF16), useutf8ToString(). Using code likenew String(bytes, offset, length)to do this is wrong, as it does not respect the correct character set and may return wrong results (depending on the platform's defaults)!- WARNING: This API is experimental and might change in incompatible ways in the next release.
-
-
Field Summary
Fields Modifier and Type Field Description byte[]bytesThe contents of the BytesRef.static byte[]EMPTY_BYTESAn empty byte array for convenienceintlengthLength of used bytes.intoffsetOffset of first valid byte.
-
Constructor Summary
Constructors Constructor Description BytesRef()Create a BytesRef withEMPTY_BYTESBytesRef(byte[] bytes)This instance will directly reference bytes w/o making a copy.BytesRef(byte[] bytes, int offset, int length)This instance will directly reference bytes w/o making a copy.BytesRef(int capacity)Create a BytesRef pointing to a new array of sizecapacity.BytesRef(CharSequence text)Initialize the byte[] from the UTF8 bytes for the provided String.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidappend(BytesRef other)Appends the bytes from the givenBytesRefbooleanbytesEquals(BytesRef other)Expert: compares the bytes against another BytesRef, returning true if the bytes are equal.BytesRefclone()intcompareTo(BytesRef other)Unsigned byte order comparisonvoidcopyBytes(BytesRef other)Copies the bytes from the givenBytesRefvoidcopyChars(char[] text, int offset, int length)Copies the UTF8 bytes for this string.voidcopyChars(CharSequence text)Copies the UTF8 bytes for this string.static BytesRefdeepCopyOf(BytesRef other)Creates a new BytesRef that points to a copy of the bytes fromotherbooleanendsWith(BytesRef other)booleanequals(Object other)static Comparator<BytesRef>getUTF8SortedAsUnicodeComparator()static Comparator<BytesRef>getUTF8SortedAsUTF16Comparator()voidgrow(int newLength)Used to grow the reference array.inthashCode()Calculates the hash code as required by TermsHash during indexing.booleanstartsWith(BytesRef other)StringtoString()Returns hex encoded bytes, eg [0x6c 0x75 0x63 0x65 0x6e 0x65]Stringutf8ToString()Interprets stored bytes as UTF8 bytes, returning the resulting string
-
-
-
Constructor Detail
-
BytesRef
public BytesRef()
Create a BytesRef withEMPTY_BYTES
-
BytesRef
public BytesRef(byte[] bytes, int offset, int length)This instance will directly reference bytes w/o making a copy. bytes should not be null.
-
BytesRef
public BytesRef(byte[] bytes)
This instance will directly reference bytes w/o making a copy. bytes should not be null
-
BytesRef
public BytesRef(int capacity)
Create a BytesRef pointing to a new array of sizecapacity. Offset and length will both be zero.
-
BytesRef
public BytesRef(CharSequence text)
Initialize the byte[] from the UTF8 bytes for the provided String.- Parameters:
text- This must be well-formed unicode text, with no unpaired surrogates.
-
-
Method Detail
-
copyChars
public void copyChars(CharSequence text)
Copies the UTF8 bytes for this string.- Parameters:
text- Must be well-formed unicode text, with no unpaired surrogates.
-
copyChars
public void copyChars(char[] text, int offset, int length)Copies the UTF8 bytes for this string.- Parameters:
text- Must be well-formed unicode text, with no unpaired surrogates.
-
bytesEquals
public boolean bytesEquals(BytesRef other)
Expert: compares the bytes against another BytesRef, returning true if the bytes are equal.- Parameters:
other- Another BytesRef, should not be null.- NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.
-
startsWith
public boolean startsWith(BytesRef other)
-
endsWith
public boolean endsWith(BytesRef other)
-
hashCode
public int hashCode()
Calculates the hash code as required by TermsHash during indexing.It is defined as:
int hash = 0; for (int i = offset; i < offset + length; i++) { hash = 31*hash + bytes[i]; }
-
utf8ToString
public String utf8ToString()
Interprets stored bytes as UTF8 bytes, returning the resulting string
-
toString
public String toString()
Returns hex encoded bytes, eg [0x6c 0x75 0x63 0x65 0x6e 0x65]
-
copyBytes
public void copyBytes(BytesRef other)
Copies the bytes from the givenBytesRefNOTE: if this would exceed the array size, this method creates a new reference array.
-
append
public void append(BytesRef other)
Appends the bytes from the givenBytesRefNOTE: if this would exceed the array size, this method creates a new reference array.
-
grow
public void grow(int newLength)
Used to grow the reference array. In general this should not be used as it does not take the offset into account.- NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.
-
compareTo
public int compareTo(BytesRef other)
Unsigned byte order comparison- Specified by:
compareToin interfaceComparable<BytesRef>
-
getUTF8SortedAsUnicodeComparator
public static Comparator<BytesRef> getUTF8SortedAsUnicodeComparator()
-
getUTF8SortedAsUTF16Comparator
public static Comparator<BytesRef> getUTF8SortedAsUTF16Comparator()
-
-