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>

      @InterfaceAudience.Public
       @InterfaceStability.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 and Description
    --- | ---
    `static class` | `BytesWritable.Comparator` A Comparator optimized for BytesWritable.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `BytesWritable()` 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

    All Methods Instance Methods Concrete Methods Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `byte[]` | `copyBytes()` 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[]` | `getBytes()` Get the data backing the BytesWritable.
    `int` | `getCapacity()` Get the capacity, which is the maximum size that could handled without resizing the backing storage.
    `int` | `getLength()` Get the current size of the buffer.
    `int` | `getSize()` Deprecated. Use `getLength()` instead.
    `int` | `hashCode()` Return a hash of the bytes returned from {#getBytes()}.
    `void` | `readFields(DataInput in)` 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 new_cap)` Change the capacity of the backing storage.
    `void` | `setSize(int size)` Change the size of the buffer.
    `String` | `toString()` Generate the stream of bytes as hex pairs separated by ' '.
    `void` | `write(DataOutput out)` 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 Detail

    - #### 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 Detail

    - #### 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.

    - #### 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.

    - #### getLength

          public int getLength()

      Get the current size of the buffer.
      Specified by:
        `getLength` in class `BinaryComparable`

    - #### getSize

          @Deprecated
          public int getSize()

      Deprecated. Use `getLength()` instead.
      Get the 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 new_cap)

      Change the capacity of the backing storage. The data is preserved.
      Parameters:
        `new_cap` - 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`

    - #### 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`

    - #### 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:
        `WritableComparator.hashBytes(byte[],int)`

    - #### equals

          public boolean equals(Object right_obj)

      Are the two byte sequences equal?
      Overrides:
        `equals` in class `BinaryComparable`

    - #### toString

          public String toString()

      Generate the stream of bytes as hex pairs separated by ' '.
      Overrides:
        `toString` in class `Object`

Copyright © 2008–2022 Apache Software Foundation. All rights reserved.