com.alexkasko.unsafe.bytearray
public abstract class ByteArrayTool extends Object
sun.misc.Unsafe
with assert
boundary checks. To enable boundary check use -ea
java switch,
without boundary checks invalid input will crash (segfault) JVM.
Fallback implementation uses standard byte array access and bit shifting.
Unsafe implementation uses native endianness (LE on most platforms). Fallback implementation uses Little Endian byte order.Constructor and Description |
---|
ByteArrayTool() |
Modifier and Type | Method and Description |
---|---|
static ByteArrayTool |
bitshift()
Instantiates bitshift tool
|
abstract void |
copy(byte[] src,
int srcPos,
byte[] dest,
int destPos,
int length)
Copies part of one array into another.
|
static ByteArrayTool |
get()
Instantiates unsafe tool, if proper
sun.misc.Unsafe implementation is available. |
abstract byte |
getByte(byte[] data,
int offset)
Gets byte
|
abstract int |
getInt(byte[] data,
int offset)
Gets four bytes as int
|
abstract long |
getLong(byte[] data,
int offset)
Gets long
|
abstract short |
getShort(byte[] data,
int offset)
Gets two bytes as short
|
abstract short |
getUnsignedByte(byte[] data,
int offset)
Gets one byte (stored as one signed byte), converts it to unsigned
and returns it as short
|
abstract long |
getUnsignedInt(byte[] data,
int offset)
Gets unsigned int (stored as 4 bytes) and returns it as long
|
abstract int |
getUnsignedShort(byte[] data,
int offset)
Gets unsigned short (stored as two bytes) and returns it as int
|
abstract boolean |
isUnsafe()
Whether current tool instance uses
sun.misc.Unsafe |
abstract void |
putByte(byte[] data,
int offset,
byte value)
Puts byte
|
abstract void |
putInt(byte[] data,
int offset,
int value)
Puts int as four bytes
|
abstract void |
putLong(byte[] data,
int offset,
long value)
Puts long as eight bytes
|
abstract void |
putShort(byte[] data,
int offset,
short value)
Puts short as two bytes
|
abstract void |
putUnsignedByte(byte[] data,
int offset,
short value)
Puts short with value from 0 to 255 inclusive into byte array as one
signed byte
|
abstract void |
putUnsignedInt(byte[] data,
int offset,
long value)
Puts long value from 0 to 4294967295 inclusive as four bytes
|
abstract void |
putUnsignedShort(byte[] data,
int offset,
int value)
Puts int with value from 0 to 65535 inclusive as two bytes
|
static ByteArrayTool |
unsafe()
Instantiates unsafe tool
|
public ByteArrayTool()
public static ByteArrayTool get()
sun.misc.Unsafe
implementation is available.
Otherwise instantiates fallback (bitshift) implementation.public static ByteArrayTool unsafe() throws Exception
Exception
- if proper sun.misc.Unsafe
implementation is not availablepublic static ByteArrayTool bitshift()
public abstract boolean isUnsafe()
sun.misc.Unsafe
public abstract byte getByte(byte[] data, int offset)
data
- byte arrayoffset
- byte array indexpublic abstract void putByte(byte[] data, int offset, byte value)
data
- byte arrayoffset
- byte array indexvalue
- valuepublic abstract short getUnsignedByte(byte[] data, int offset)
data
- byte arrayoffset
- byte array indexpublic abstract void putUnsignedByte(byte[] data, int offset, short value)
data
- byte arrayoffset
- byte array indexvalue
- unsigned bytepublic abstract short getShort(byte[] data, int offset)
data
- byte arrayoffset
- byte array offsetpublic abstract void putShort(byte[] data, int offset, short value)
data
- byte arrayoffset
- byte array offsetvalue
- short valuepublic abstract int getUnsignedShort(byte[] data, int offset)
data
- byte arrayoffset
- byte array offsetpublic abstract void putUnsignedShort(byte[] data, int offset, int value)
data
- byte arrayoffset
- byte array offsetvalue
- unsigned short as intpublic abstract int getInt(byte[] data, int offset)
data
- byte arrayoffset
- byte array offsetpublic abstract void putInt(byte[] data, int offset, int value)
data
- byte arrayoffset
- byte array offsetvalue
- int valuepublic abstract long getUnsignedInt(byte[] data, int offset)
data
- byte arrayoffset
- byte array offsetpublic abstract void putUnsignedInt(byte[] data, int offset, long value)
data
- byte arrayoffset
- byte array offsetvalue
- unsigned int as longpublic abstract long getLong(byte[] data, int offset)
data
- byte arrayoffset
- byte array offsetpublic abstract void putLong(byte[] data, int offset, long value)
data
- byte arrayoffset
- byte array offsetvalue
- long valuepublic abstract void copy(byte[] src, int srcPos, byte[] dest, int destPos, int length)
System.arraycopy(Object, int, Object, int, int)
that is used by fallback implementation usessrc
- source arraysrcPos
- source array positiondest
- destination arraydestPos
- destination array positionlength
- length to copyCopyright © 2014. All Rights Reserved.