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 Classes
    Modifier and Type
    Class
    Description
    static class 
    org.apache.hadoop.io.BytesWritable.Comparator
    A Comparator optimized for BytesWritable.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create 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 Type
    Method
    Description
    byte[]
    Get a copy of the bytes that is exactly the length of the data.
    boolean
    equals(Object right_obj)
    Are the two byte sequences equal?
    byte[]
    get()
    Deprecated.
    Use getBytes() instead.
    byte[]
    Get the data backing the BytesWritable.
    int
    Get the capacity, which is the maximum size that could handled without resizing the backing storage.
    int
    Get the current size of the buffer.
    int
    Deprecated.
    Use getLength() instead.
    int
    Return a hash of the bytes returned from {#getBytes()}.
    void
    Deserialize the fields of this object from in.
    void
    set(byte[] newData, int offset, int length)
    Set the value to a copy of the given byte range.
    void
    set(BytesWritable newData)
    Set the BytesWritable to the contents of the given newData.
    void
    setCapacity(int capacity)
    Change the capacity of the backing storage.
    void
    setSize(int size)
    Change the size of the buffer.
    Generate the stream of bytes as hex pairs separated by ' '.
    void
    Serialize the fields of this object to out.

    Methods inherited from class org.apache.hadoop.io.BinaryComparable

    compareTo, compareTo

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods 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. See getBytes() for faster access to the underlying array.
      Returns:
      copyBytes.
    • getBytes

      public byte[] getBytes()
      Get the data backing the BytesWritable. Please use copyBytes() if you need the returned array to be precisely the length of the data.
      Specified by:
      getBytes in class BinaryComparable
      Returns:
      The data is only valid between 0 and getLength() - 1.
    • get

      @Deprecated public byte[] get()
      Deprecated.
      Use getBytes() 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:
      getLength in class BinaryComparable
      Returns:
      length.
    • getSize

      @Deprecated public int getSize()
      Deprecated.
      Use getLength() 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

      public void set(BytesWritable newData)
      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 in
      offset - the offset in newData to start at
      length - the number of bytes to copy
    • readFields

      public void readFields(DataInput in) throws IOException
      Description copied from interface: Writable
      Deserialize the fields of this object from in.

      For efficiency, implementations should attempt to re-use storage in the existing object where possible.

      Specified by:
      readFields in interface Writable
      Parameters:
      in - DataInput to deseriablize this object from.
      Throws:
      IOException - any other problem for readFields.
    • write

      public void write(DataOutput out) throws IOException
      Description copied from interface: Writable
      Serialize the fields of this object to out.
      Specified by:
      write in interface Writable
      Parameters:
      out - DataOuput to serialize this object into.
      Throws:
      IOException - any other problem for write.
    • equals

      public boolean equals(Object right_obj)
      Are the two byte sequences equal?
      Overrides:
      equals in class BinaryComparable
    • hashCode

      public int hashCode()
      Description copied from class: BinaryComparable
      Return a hash of the bytes returned from {#getBytes()}.
      Overrides:
      hashCode in class BinaryComparable
      See Also:
    • toString

      public String toString()
      Generate the stream of bytes as hex pairs separated by ' '.
      Overrides:
      toString in class Object