org.apache.hadoop.hbase.util
Class ByteBufferUtils

java.lang.Object
  extended by org.apache.hadoop.hbase.util.ByteBufferUtils

@InterfaceAudience.Public
@InterfaceStability.Evolving
public final class ByteBufferUtils
extends Object

Utility functions for working with byte buffers, such as reading/writing variable-length long numbers.


Method Summary
static boolean arePartsEqual(ByteBuffer buffer, int offsetLeft, int lengthLeft, int offsetRight, int lengthRight)
          Check whether two parts in the same buffer are equal.
static void copyBufferToStream(OutputStream out, ByteBuffer in, int offset, int length)
          Copy data from a buffer to an output stream.
static void copyFromBufferToBuffer(ByteBuffer out, ByteBuffer in, int sourceOffset, int length)
          Copy from one buffer to another from given offset
static void copyFromStreamToBuffer(ByteBuffer out, DataInputStream in, int length)
          Copy the given number of bytes from the given stream and put it at the current position of the given buffer, updating the position in the buffer.
static ByteBuffer drainInputStreamToBuffer(InputStream is)
          Copy from the InputStream to a new heap ByteBuffer until the InputStream is exhausted.
static void extendLimit(ByteBuffer buffer, int numBytes)
           
static int findCommonPrefix(byte[] left, int leftOffset, int leftLength, byte[] right, int rightOffset, int rightLength)
          Find length of common prefix in two arrays.
static int findCommonPrefix(ByteBuffer buffer, int offsetLeft, int offsetRight, int limit)
          Find length of common prefix of two parts in the buffer
static int intFitsIn(int value)
          Check how many bytes is required to store value.
static int longFitsIn(long value)
          Check how many bytes are required to store value.
static void moveBufferToStream(OutputStream out, ByteBuffer in, int length)
          Copy the data to the output stream and update position in buffer.
static int putCompressedInt(OutputStream out, int value)
          Put in buffer integer using 7 bit encoding.
static void putInt(OutputStream out, int value)
          Put in output stream 32 bit integer (Big Endian byte order).
static int putLong(OutputStream out, long value, int fitInBytes)
           
static int readCompressedInt(ByteBuffer buffer)
          Read integer from buffer coded in 7 bits and increment position.
static int readCompressedInt(InputStream input)
          Read integer from stream coded in 7 bits and increment position.
static long readLong(ByteBuffer in, int fitInBytes)
          Read long which was written to fitInBytes bytes and increment position.
static long readLong(InputStream in, int fitInBytes)
          Read long which was written to fitInBytes bytes and increment position.
static long readVLong(ByteBuffer in)
          Similar to WritableUtils.readVLong(DataInput) but reads from a ByteBuffer.
static void skip(ByteBuffer buffer, int length)
          Increment position in buffer.
static byte[] toBytes(ByteBuffer buffer, int startPosition)
          Copy the bytes from position to limit into a new byte[] of the exact length and sets the position and limit back to their original values (though not thread safe).
static void writeVLong(ByteBuffer out, long i)
          Similar to WritableUtils.writeVLong(java.io.DataOutput, long), but writes to a ByteBuffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

writeVLong

public static void writeVLong(ByteBuffer out,
                              long i)
Similar to WritableUtils.writeVLong(java.io.DataOutput, long), but writes to a ByteBuffer.


readVLong

public static long readVLong(ByteBuffer in)
Similar to WritableUtils.readVLong(DataInput) but reads from a ByteBuffer.


putCompressedInt

public static int putCompressedInt(OutputStream out,
                                   int value)
                            throws IOException
Put in buffer integer using 7 bit encoding. For each written byte: 7 bits are used to store value 1 bit is used to indicate whether there is next bit.

Parameters:
value - Int to be compressed.
out - Where to put compressed data
Returns:
Number of bytes written.
Throws:
IOException - on stream error

putInt

public static void putInt(OutputStream out,
                          int value)
                   throws IOException
Put in output stream 32 bit integer (Big Endian byte order).

Parameters:
out - Where to put integer.
value - Value of integer.
Throws:
IOException - On stream error.

moveBufferToStream

public static void moveBufferToStream(OutputStream out,
                                      ByteBuffer in,
                                      int length)
                               throws IOException
Copy the data to the output stream and update position in buffer.

Parameters:
out - the stream to write bytes to
in - the buffer to read bytes from
length - the number of bytes to copy
Throws:
IOException

copyBufferToStream

public static void copyBufferToStream(OutputStream out,
                                      ByteBuffer in,
                                      int offset,
                                      int length)
                               throws IOException
Copy data from a buffer to an output stream. Does not update the position in the buffer.

Parameters:
out - the stream to write bytes to
in - the buffer to read bytes from
offset - the offset in the buffer (from the buffer's array offset) to start copying bytes from
length - the number of bytes to copy
Throws:
IOException

putLong

public static int putLong(OutputStream out,
                          long value,
                          int fitInBytes)
                   throws IOException
Throws:
IOException

longFitsIn

public static int longFitsIn(long value)
Check how many bytes are required to store value.

Parameters:
value - Value which size will be tested.
Returns:
How many bytes are required to store value.

intFitsIn

public static int intFitsIn(int value)
Check how many bytes is required to store value.

Parameters:
value - Value which size will be tested.
Returns:
How many bytes are required to store value.

readCompressedInt

public static int readCompressedInt(InputStream input)
                             throws IOException
Read integer from stream coded in 7 bits and increment position.

Returns:
the integer that has been read
Throws:
IOException

readCompressedInt

public static int readCompressedInt(ByteBuffer buffer)
Read integer from buffer coded in 7 bits and increment position.

Returns:
Read integer.

readLong

public static long readLong(InputStream in,
                            int fitInBytes)
                     throws IOException
Read long which was written to fitInBytes bytes and increment position.

Parameters:
fitInBytes - In how many bytes given long is stored.
Returns:
The value of parsed long.
Throws:
IOException

readLong

public static long readLong(ByteBuffer in,
                            int fitInBytes)
Read long which was written to fitInBytes bytes and increment position.

Parameters:
fitInBytes - In how many bytes given long is stored.
Returns:
The value of parsed long.

copyFromStreamToBuffer

public static void copyFromStreamToBuffer(ByteBuffer out,
                                          DataInputStream in,
                                          int length)
                                   throws IOException
Copy the given number of bytes from the given stream and put it at the current position of the given buffer, updating the position in the buffer.

Parameters:
out - the buffer to write data to
in - the stream to read data from
length - the number of bytes to read/write
Throws:
IOException

drainInputStreamToBuffer

public static ByteBuffer drainInputStreamToBuffer(InputStream is)
                                           throws IOException
Copy from the InputStream to a new heap ByteBuffer until the InputStream is exhausted.

Throws:
IOException

copyFromBufferToBuffer

public static void copyFromBufferToBuffer(ByteBuffer out,
                                          ByteBuffer in,
                                          int sourceOffset,
                                          int length)
Copy from one buffer to another from given offset

Parameters:
out - destination buffer
in - source buffer
sourceOffset - offset in the source buffer
length - how many bytes to copy

findCommonPrefix

public static int findCommonPrefix(ByteBuffer buffer,
                                   int offsetLeft,
                                   int offsetRight,
                                   int limit)
Find length of common prefix of two parts in the buffer

Parameters:
buffer - Where parts are located.
offsetLeft - Offset of the first part.
offsetRight - Offset of the second part.
limit - Maximal length of common prefix.
Returns:
Length of prefix.

findCommonPrefix

public static int findCommonPrefix(byte[] left,
                                   int leftOffset,
                                   int leftLength,
                                   byte[] right,
                                   int rightOffset,
                                   int rightLength)
Find length of common prefix in two arrays.

Parameters:
left - Array to be compared.
leftOffset - Offset in left array.
leftLength - Length of left array.
right - Array to be compared.
rightOffset - Offset in right array.
rightLength - Length of right array.

arePartsEqual

public static boolean arePartsEqual(ByteBuffer buffer,
                                    int offsetLeft,
                                    int lengthLeft,
                                    int offsetRight,
                                    int lengthRight)
Check whether two parts in the same buffer are equal.

Parameters:
buffer - In which buffer there are parts
offsetLeft - Beginning of first part.
lengthLeft - Length of the first part.
offsetRight - Beginning of the second part.
lengthRight - Length of the second part.
Returns:
True if equal

skip

public static void skip(ByteBuffer buffer,
                        int length)
Increment position in buffer.

Parameters:
buffer - In this buffer.
length - By that many bytes.

extendLimit

public static void extendLimit(ByteBuffer buffer,
                               int numBytes)

toBytes

public static byte[] toBytes(ByteBuffer buffer,
                             int startPosition)
Copy the bytes from position to limit into a new byte[] of the exact length and sets the position and limit back to their original values (though not thread safe).

Parameters:
buffer - copy from here
startPosition - put buffer.get(startPosition) into byte[0]
Returns:
a new byte[] containing the bytes in the specified range


Copyright © 2015 The Apache Software Foundation. All Rights Reserved.