Package org.apache.hadoop.io
Class BytesWritable
java.lang.Object
org.apache.hadoop.io.BinaryComparable
org.apache.hadoop.io.BytesWritable
- All Implemented Interfaces:
Comparable<BinaryComparable>,Writable,WritableComparable<BinaryComparable>
@Public
@Stable
public class BytesWritable
extends BinaryComparable
implements WritableComparable<BinaryComparable>
A byte sequence that is usable as a key or value.
It is resizable and distinguishes between the size of the sequence and
the current capacity. The hash function is the front of the md5 of the
buffer. The sort order is the same as memcmp.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classorg.apache.hadoop.io.BytesWritable.ComparatorA Comparator optimized for BytesWritable. -
Constructor Summary
ConstructorsConstructorDescriptionCreate a zero-size sequence.BytesWritable(byte[] bytes) Create a BytesWritable using the byte array as the initial value.BytesWritable(byte[] bytes, int length) Create a BytesWritable using the byte array as the initial value and length as the length. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]Get a copy of the bytes that is exactly the length of the data.booleanAre the two byte sequences equal?byte[]get()Deprecated.byte[]getBytes()Get the data backing the BytesWritable.intGet the capacity, which is the maximum size that could handled without resizing the backing storage.intGet the current size of the buffer.intgetSize()Deprecated.UsegetLength()instead.inthashCode()Return a hash of the bytes returned from {#getBytes()}.voidreadFields(DataInput in) Deserialize the fields of this object fromin.voidset(byte[] newData, int offset, int length) Set the value to a copy of the given byte range.voidset(BytesWritable newData) Set the BytesWritable to the contents of the given newData.voidsetCapacity(int capacity) Change the capacity of the backing storage.voidsetSize(int size) Change the size of the buffer.toString()Generate the stream of bytes as hex pairs separated by ' '.voidwrite(DataOutput out) Serialize the fields of this object toout.Methods inherited from class org.apache.hadoop.io.BinaryComparable
compareTo, compareToMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Comparable
compareTo
-
Constructor Details
-
BytesWritable
public BytesWritable()Create a zero-size sequence. -
BytesWritable
public BytesWritable(byte[] bytes) Create a BytesWritable using the byte array as the initial value.- Parameters:
bytes- This array becomes the backing storage for the object.
-
BytesWritable
public BytesWritable(byte[] bytes, int length) Create a BytesWritable using the byte array as the initial value and length as the length. Use this constructor if the array is larger than the value it represents.- Parameters:
bytes- This array becomes the backing storage for the object.length- The number of bytes to use from array.
-
-
Method Details
-
copyBytes
public byte[] copyBytes()Get a copy of the bytes that is exactly the length of the data. SeegetBytes()for faster access to the underlying array.- Returns:
- copyBytes.
-
getBytes
public byte[] getBytes()Get the data backing the BytesWritable. Please usecopyBytes()if you need the returned array to be precisely the length of the data.- Specified by:
getBytesin classBinaryComparable- Returns:
- The data is only valid between 0 and getLength() - 1.
-
get
Deprecated.UsegetBytes()instead.Get the data from the BytesWritable.- Returns:
- data from the BytesWritable.
-
getLength
public int getLength()Get the current size of the buffer.- Specified by:
getLengthin classBinaryComparable- Returns:
- length.
-
getSize
Deprecated.UsegetLength()instead.Get the current size of the buffer.- Returns:
- current size of the buffer.
-
setSize
public void setSize(int size) Change the size of the buffer. The values in the old range are preserved and any new values are undefined. The capacity is changed if it is necessary.- Parameters:
size- The new number of bytes
-
getCapacity
public int getCapacity()Get the capacity, which is the maximum size that could handled without resizing the backing storage.- Returns:
- The number of bytes
-
setCapacity
public void setCapacity(int capacity) Change the capacity of the backing storage. The data is preserved.- Parameters:
capacity- The new capacity in bytes.
-
set
Set the BytesWritable to the contents of the given newData.- Parameters:
newData- the value to set this BytesWritable to.
-
set
public void set(byte[] newData, int offset, int length) Set the value to a copy of the given byte range.- Parameters:
newData- the new values to copy inoffset- the offset in newData to start atlength- the number of bytes to copy
-
readFields
Description copied from interface:WritableDeserialize the fields of this object fromin.For efficiency, implementations should attempt to re-use storage in the existing object where possible.
- Specified by:
readFieldsin interfaceWritable- Parameters:
in-DataInputto deseriablize this object from.- Throws:
IOException- any other problem for readFields.
-
write
Description copied from interface:WritableSerialize the fields of this object toout.- Specified by:
writein interfaceWritable- Parameters:
out-DataOuputto serialize this object into.- Throws:
IOException- any other problem for write.
-
equals
Are the two byte sequences equal?- Overrides:
equalsin classBinaryComparable
-
hashCode
public int hashCode()Description copied from class:BinaryComparableReturn a hash of the bytes returned from {#getBytes()}.- Overrides:
hashCodein classBinaryComparable- See Also:
-
toString
Generate the stream of bytes as hex pairs separated by ' '.
-
getBytes()instead.