org.apache.kafka.common.utils
Class Utils

java.lang.Object
  extended by org.apache.kafka.common.utils.Utils

public class Utils
extends java.lang.Object


Field Summary
static java.lang.String NL
           
 
Constructor Summary
Utils()
           
 
Method Summary
static int abs(int n)
          Get the absolute value of the given number.
static java.lang.String formatAddress(java.lang.String host, java.lang.Integer port)
          Formats hostname and port number as a "host:port" address string, surrounding IPv6 addresses with braces '[', ']'
static java.lang.String getHost(java.lang.String address)
          Extracts the hostname from a "host:port" address string.
static java.lang.Integer getPort(java.lang.String address)
          Extracts the port number from a "host:port" address string.
static int murmur2(byte[] data)
          Generates 32 bit murmur2 hash from byte array
static java.lang.Object newInstance(java.lang.Class<?> c)
          Instantiate the class
static
<T> T
notNull(T t)
          Check that the parameter t is not null
static long readUnsignedInt(java.nio.ByteBuffer buffer)
          Read an unsigned integer from the current position in the buffer, incrementing the position by 4 bytes
static long readUnsignedInt(java.nio.ByteBuffer buffer, int index)
          Read an unsigned integer from the given position without modifying the buffers position
static int readUnsignedIntLE(byte[] buffer, int offset)
          Read an unsigned integer stored in little-endian format from a byte array at a given offset.
static int readUnsignedIntLE(java.io.InputStream in)
          Read an unsigned integer stored in little-endian format from the InputStream.
static byte[] toArray(java.nio.ByteBuffer buffer)
          Read the given byte buffer into a byte array
static byte[] toArray(java.nio.ByteBuffer buffer, int offset, int size)
          Read a byte array from the given offset and size in the buffer
static java.lang.String utf8(byte[] bytes)
          Turn the given UTF8 byte array into a string
static byte[] utf8(java.lang.String string)
          Turn a string into a utf8 byte[]
static int utf8Length(java.lang.CharSequence s)
          Get the length for UTF8-encoding a string without encoding it first
static void writetUnsignedInt(java.nio.ByteBuffer buffer, long value)
          Write the given long value as a 4 byte unsigned integer.
static void writeUnsignedInt(java.nio.ByteBuffer buffer, int index, long value)
          Write the given long value as a 4 byte unsigned integer.
static void writeUnsignedIntLE(byte[] buffer, int offset, int value)
          Write an unsigned integer in little-endian format to a byte array at a given offset.
static void writeUnsignedIntLE(java.io.OutputStream out, int value)
          Write an unsigned integer in little-endian format to the OutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NL

public static java.lang.String NL
Constructor Detail

Utils

public Utils()
Method Detail

utf8

public static java.lang.String utf8(byte[] bytes)
Turn the given UTF8 byte array into a string

Parameters:
bytes - The byte array
Returns:
The string

utf8

public static byte[] utf8(java.lang.String string)
Turn a string into a utf8 byte[]

Parameters:
string - The string
Returns:
The byte[]

readUnsignedInt

public static long readUnsignedInt(java.nio.ByteBuffer buffer)
Read an unsigned integer from the current position in the buffer, incrementing the position by 4 bytes

Parameters:
buffer - The buffer to read from
Returns:
The integer read, as a long to avoid signedness

readUnsignedInt

public static long readUnsignedInt(java.nio.ByteBuffer buffer,
                                   int index)
Read an unsigned integer from the given position without modifying the buffers position

Parameters:
buffer - the buffer to read from
index - the index from which to read the integer
Returns:
The integer read, as a long to avoid signedness

readUnsignedIntLE

public static int readUnsignedIntLE(java.io.InputStream in)
                             throws java.io.IOException
Read an unsigned integer stored in little-endian format from the InputStream.

Parameters:
in - The stream to read from
Returns:
The integer read (MUST BE TREATED WITH SPECIAL CARE TO AVOID SIGNEDNESS)
Throws:
java.io.IOException

readUnsignedIntLE

public static int readUnsignedIntLE(byte[] buffer,
                                    int offset)
Read an unsigned integer stored in little-endian format from a byte array at a given offset.

Parameters:
buffer - The byte array to read from
offset - The position in buffer to read from
Returns:
The integer read (MUST BE TREATED WITH SPECIAL CARE TO AVOID SIGNEDNESS)

writetUnsignedInt

public static void writetUnsignedInt(java.nio.ByteBuffer buffer,
                                     long value)
Write the given long value as a 4 byte unsigned integer. Overflow is ignored.

Parameters:
buffer - The buffer to write to
value - The value to write

writeUnsignedInt

public static void writeUnsignedInt(java.nio.ByteBuffer buffer,
                                    int index,
                                    long value)
Write the given long value as a 4 byte unsigned integer. Overflow is ignored.

Parameters:
buffer - The buffer to write to
index - The position in the buffer at which to begin writing
value - The value to write

writeUnsignedIntLE

public static void writeUnsignedIntLE(java.io.OutputStream out,
                                      int value)
                               throws java.io.IOException
Write an unsigned integer in little-endian format to the OutputStream.

Parameters:
out - The stream to write to
value - The value to write
Throws:
java.io.IOException

writeUnsignedIntLE

public static void writeUnsignedIntLE(byte[] buffer,
                                      int offset,
                                      int value)
Write an unsigned integer in little-endian format to a byte array at a given offset.

Parameters:
buffer - The byte array to write to
offset - The position in buffer to write to
value - The value to write

abs

public static int abs(int n)
Get the absolute value of the given number. If the number is Int.MinValue return 0. This is different from java.lang.Math.abs or scala.math.abs in that they return Int.MinValue (!).


utf8Length

public static int utf8Length(java.lang.CharSequence s)
Get the length for UTF8-encoding a string without encoding it first

Parameters:
s - The string to calculate the length for
Returns:
The length when serialized

toArray

public static byte[] toArray(java.nio.ByteBuffer buffer)
Read the given byte buffer into a byte array


toArray

public static byte[] toArray(java.nio.ByteBuffer buffer,
                             int offset,
                             int size)
Read a byte array from the given offset and size in the buffer


notNull

public static <T> T notNull(T t)
Check that the parameter t is not null

Parameters:
t - The object to check
Returns:
t if it isn't null
Throws:
java.lang.NullPointerException - if t is null.

newInstance

public static java.lang.Object newInstance(java.lang.Class<?> c)
Instantiate the class


murmur2

public static int murmur2(byte[] data)
Generates 32 bit murmur2 hash from byte array

Parameters:
data - byte array to hash
Returns:
32 bit hash of the given array

getHost

public static java.lang.String getHost(java.lang.String address)
Extracts the hostname from a "host:port" address string.

Parameters:
address - address string to parse
Returns:
hostname or null if the given address is incorrect

getPort

public static java.lang.Integer getPort(java.lang.String address)
Extracts the port number from a "host:port" address string.

Parameters:
address - address string to parse
Returns:
port number or null if the given address is incorrect

formatAddress

public static java.lang.String formatAddress(java.lang.String host,
                                             java.lang.Integer port)
Formats hostname and port number as a "host:port" address string, surrounding IPv6 addresses with braces '[', ']'

Parameters:
host - hostname
port - port number
Returns:
address string