org.h2.mvstore
Class DataUtils

java.lang.Object
  extended by org.h2.mvstore.DataUtils

public class DataUtils
extends java.lang.Object

Utility methods


Nested Class Summary
static class DataUtils.MapEntry<K,V>
          An entry of a map.
 
Field Summary
static int COMPRESSED_VAR_INT_MAX
          The maximum integer that needs less space when using variable size encoding (only 3 bytes instead of 4).
static long COMPRESSED_VAR_LONG_MAX
          The maximum long that needs less space when using variable size encoding (only 7 bytes instead of 8).
static int ERROR_CLOSED
          The object is already closed.
static int ERROR_FILE_CORRUPT
          The file is corrupt or (for encrypted files) the encryption key is wrong.
static int ERROR_FILE_LOCKED
          The file is locked.
static int ERROR_INTERNAL
          An internal error occurred.
static int ERROR_READING_FAILED
          An error occurred while reading from the file.
static int ERROR_SERIALIZATION
          An error occurred when serializing or de-serializing.
static int ERROR_TRANSACTION_CORRUPT
          The transaction store is corrupt.
static int ERROR_TRANSACTION_LOCKED
          An entry is still locked by another transaction.
static int ERROR_TRANSACTION_STILL_OPEN
          A very old transaction is still open.
static int ERROR_UNSUPPORTED_FORMAT
          The file format is not supported.
static int ERROR_WRITING_FAILED
          An error occurred when trying to write to the file.
static java.nio.charset.Charset LATIN
          The ISO Latin character encoding format.
static int MAX_VAR_INT_LEN
          The maximum length of a variable size int.
static int MAX_VAR_LONG_LEN
          The maximum length of a variable size long.
static int PAGE_COMPRESSED
          The bit mask for compressed pages (compression level fast).
static int PAGE_COMPRESSED_HIGH
          The bit mask for compressed pages (compression level high).
static int PAGE_LARGE
          The marker size of a very large page.
static int PAGE_MEMORY
          The estimated number of bytes used per page object.
static int PAGE_MEMORY_CHILD
          The estimated number of bytes used per child entry.
static int PAGE_TYPE_LEAF
          The type for leaf page.
static int PAGE_TYPE_NODE
          The type for node page.
static java.nio.charset.Charset UTF8
          The UTF-8 character encoding format.
 
Constructor Summary
DataUtils()
           
 
Method Summary
static java.lang.StringBuilder appendMap(java.lang.StringBuilder buff, java.util.HashMap<java.lang.String,?> map)
          Append a map to the string builder, sorted by key.
static void appendMap(java.lang.StringBuilder buff, java.lang.String key, java.lang.Object value)
          Append a key-value pair to the string builder.
static void checkArgument(boolean test, java.lang.String message, java.lang.Object... arguments)
          Throw an IllegalArgumentException if the argument is invalid.
static void copyExcept(java.lang.Object src, java.lang.Object dst, int oldSize, int removeIndex)
          Copy the elements of an array, and remove one element.
static void copyWithGap(java.lang.Object src, java.lang.Object dst, int oldSize, int gapIndex)
          Copy the elements of an array, with a gap.
static int encodeLength(int len)
          Convert the length to a length code 0..31.
static java.nio.ByteBuffer ensureCapacity(java.nio.ByteBuffer buff, int len)
          Ensure the byte buffer has the given capacity, plus 1 KB.
static short getCheckValue(int x)
          Calculate a check value for the given integer.
static int getErrorCode(java.lang.String m)
          Get the error code from an exception message.
static int getFletcher32(byte[] bytes, int length)
          Calculate the Fletcher32 checksum.
static int getPageChunkId(long pos)
          Get the chunk id from the position.
static int getPageMaxLength(long pos)
          Get the maximum length for the given code.
static int getPageOffset(long pos)
          Get the offset from the position.
static long getPagePos(int chunkId, int offset, int length, int type)
          Get the position of this page.
static int getPageType(long pos)
          Get the page type from the position.
static int getVarIntLen(int x)
          Get the length of the variable size int.
static int getVarLongLen(long x)
          Get the length of the variable size long.
static byte[] newBytes(int len)
          Create an array of bytes with the given size.
static java.util.ConcurrentModificationException newConcurrentModificationException(java.lang.String message)
          Create a new ConcurrentModificationException.
static java.lang.IllegalArgumentException newIllegalArgumentException(java.lang.String message, java.lang.Object... arguments)
          Create a new IllegalArgumentException.
static java.lang.IllegalStateException newIllegalStateException(int errorCode, java.lang.String message, java.lang.Object... arguments)
          Create a new IllegalStateException.
static java.lang.UnsupportedOperationException newUnsupportedOperationException(java.lang.String message)
          Create a new UnsupportedOperationException.
static int parseHexInt(java.lang.String x)
          Parse an unsigned, hex long.
static long parseHexLong(java.lang.String x)
          Parse an unsigned, hex long.
static java.util.HashMap<java.lang.String,java.lang.String> parseMap(java.lang.String s)
          Parse a key-value pair list.
static void readFully(java.nio.channels.FileChannel file, long pos, java.nio.ByteBuffer dst)
          Read from a file channel until the buffer is full.
static int readHexInt(java.util.HashMap<java.lang.String,? extends java.lang.Object> map, java.lang.String key, int defaultValue)
          Read a hex int value from a map.
static long readHexLong(java.util.HashMap<java.lang.String,? extends java.lang.Object> map, java.lang.String key, long defaultValue)
          Read a hex long value from a map.
static java.lang.String readString(java.nio.ByteBuffer buff, int len)
          Read a string.
static int readVarInt(java.nio.ByteBuffer buff)
          Read a variable size int.
static long readVarLong(java.nio.ByteBuffer buff)
          Read a variable size long.
static void writeFully(java.nio.channels.FileChannel file, long pos, java.nio.ByteBuffer src)
          Write to a file channel.
static java.nio.ByteBuffer writeStringData(java.nio.ByteBuffer buff, java.lang.String s, int len)
          Write characters from a string (without the length).
static void writeVarInt(java.nio.ByteBuffer buff, int x)
          Write a variable size int.
static void writeVarInt(java.io.OutputStream out, int x)
          Write a variable size int.
static void writeVarLong(java.nio.ByteBuffer buff, long x)
          Write a variable size long.
static void writeVarLong(java.io.OutputStream out, long x)
          Write a variable size long.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ERROR_READING_FAILED

public static final int ERROR_READING_FAILED
An error occurred while reading from the file.

See Also:
Constant Field Values

ERROR_WRITING_FAILED

public static final int ERROR_WRITING_FAILED
An error occurred when trying to write to the file.

See Also:
Constant Field Values

ERROR_INTERNAL

public static final int ERROR_INTERNAL
An internal error occurred. This could be a bug, or a memory corruption (for example caused by out of memory).

See Also:
Constant Field Values

ERROR_CLOSED

public static final int ERROR_CLOSED
The object is already closed.

See Also:
Constant Field Values

ERROR_UNSUPPORTED_FORMAT

public static final int ERROR_UNSUPPORTED_FORMAT
The file format is not supported.

See Also:
Constant Field Values

ERROR_FILE_CORRUPT

public static final int ERROR_FILE_CORRUPT
The file is corrupt or (for encrypted files) the encryption key is wrong.

See Also:
Constant Field Values

ERROR_FILE_LOCKED

public static final int ERROR_FILE_LOCKED
The file is locked.

See Also:
Constant Field Values

ERROR_SERIALIZATION

public static final int ERROR_SERIALIZATION
An error occurred when serializing or de-serializing.

See Also:
Constant Field Values

ERROR_TRANSACTION_CORRUPT

public static final int ERROR_TRANSACTION_CORRUPT
The transaction store is corrupt.

See Also:
Constant Field Values

ERROR_TRANSACTION_LOCKED

public static final int ERROR_TRANSACTION_LOCKED
An entry is still locked by another transaction.

See Also:
Constant Field Values

ERROR_TRANSACTION_STILL_OPEN

public static final int ERROR_TRANSACTION_STILL_OPEN
A very old transaction is still open.

See Also:
Constant Field Values

PAGE_TYPE_LEAF

public static final int PAGE_TYPE_LEAF
The type for leaf page.

See Also:
Constant Field Values

PAGE_TYPE_NODE

public static final int PAGE_TYPE_NODE
The type for node page.

See Also:
Constant Field Values

PAGE_COMPRESSED

public static final int PAGE_COMPRESSED
The bit mask for compressed pages (compression level fast).

See Also:
Constant Field Values

PAGE_COMPRESSED_HIGH

public static final int PAGE_COMPRESSED_HIGH
The bit mask for compressed pages (compression level high).

See Also:
Constant Field Values

MAX_VAR_INT_LEN

public static final int MAX_VAR_INT_LEN
The maximum length of a variable size int.

See Also:
Constant Field Values

MAX_VAR_LONG_LEN

public static final int MAX_VAR_LONG_LEN
The maximum length of a variable size long.

See Also:
Constant Field Values

COMPRESSED_VAR_INT_MAX

public static final int COMPRESSED_VAR_INT_MAX
The maximum integer that needs less space when using variable size encoding (only 3 bytes instead of 4).

See Also:
Constant Field Values

COMPRESSED_VAR_LONG_MAX

public static final long COMPRESSED_VAR_LONG_MAX
The maximum long that needs less space when using variable size encoding (only 7 bytes instead of 8).

See Also:
Constant Field Values

PAGE_MEMORY

public static final int PAGE_MEMORY
The estimated number of bytes used per page object.

See Also:
Constant Field Values

PAGE_MEMORY_CHILD

public static final int PAGE_MEMORY_CHILD
The estimated number of bytes used per child entry.

See Also:
Constant Field Values

PAGE_LARGE

public static final int PAGE_LARGE
The marker size of a very large page.

See Also:
Constant Field Values

UTF8

public static final java.nio.charset.Charset UTF8
The UTF-8 character encoding format.


LATIN

public static final java.nio.charset.Charset LATIN
The ISO Latin character encoding format.

Constructor Detail

DataUtils

public DataUtils()
Method Detail

getVarIntLen

public static int getVarIntLen(int x)
Get the length of the variable size int.

Parameters:
x - the value
Returns:
the length in bytes

getVarLongLen

public static int getVarLongLen(long x)
Get the length of the variable size long.

Parameters:
x - the value
Returns:
the length in bytes

readVarInt

public static int readVarInt(java.nio.ByteBuffer buff)
Read a variable size int.

Parameters:
buff - the source buffer
Returns:
the value

readVarLong

public static long readVarLong(java.nio.ByteBuffer buff)
Read a variable size long.

Parameters:
buff - the source buffer
Returns:
the value

writeVarInt

public static void writeVarInt(java.io.OutputStream out,
                               int x)
                        throws java.io.IOException
Write a variable size int.

Parameters:
out - the output stream
x - the value
Throws:
java.io.IOException

writeVarInt

public static void writeVarInt(java.nio.ByteBuffer buff,
                               int x)
Write a variable size int.

Parameters:
buff - the source buffer
x - the value

writeStringData

public static java.nio.ByteBuffer writeStringData(java.nio.ByteBuffer buff,
                                                  java.lang.String s,
                                                  int len)
Write characters from a string (without the length).

Parameters:
buff - the target buffer
s - the string
len - the number of characters
Returns:
the byte buffer

readString

public static java.lang.String readString(java.nio.ByteBuffer buff,
                                          int len)
Read a string.

Parameters:
buff - the source buffer
len - the number of characters
Returns:
the value

writeVarLong

public static void writeVarLong(java.nio.ByteBuffer buff,
                                long x)
Write a variable size long.

Parameters:
buff - the target buffer
x - the value

writeVarLong

public static void writeVarLong(java.io.OutputStream out,
                                long x)
                         throws java.io.IOException
Write a variable size long.

Parameters:
out - the output stream
x - the value
Throws:
java.io.IOException

copyWithGap

public static void copyWithGap(java.lang.Object src,
                               java.lang.Object dst,
                               int oldSize,
                               int gapIndex)
Copy the elements of an array, with a gap.

Parameters:
src - the source array
dst - the target array
oldSize - the size of the old array
gapIndex - the index of the gap

copyExcept

public static void copyExcept(java.lang.Object src,
                              java.lang.Object dst,
                              int oldSize,
                              int removeIndex)
Copy the elements of an array, and remove one element.

Parameters:
src - the source array
dst - the target array
oldSize - the size of the old array
removeIndex - the index of the entry to remove

readFully

public static void readFully(java.nio.channels.FileChannel file,
                             long pos,
                             java.nio.ByteBuffer dst)
Read from a file channel until the buffer is full. The buffer is rewind after reading.

Parameters:
file - the file channel
pos - the absolute position within the file
dst - the byte buffer
Throws:
java.lang.IllegalStateException - if some data could not be read

writeFully

public static void writeFully(java.nio.channels.FileChannel file,
                              long pos,
                              java.nio.ByteBuffer src)
Write to a file channel.

Parameters:
file - the file channel
pos - the absolute position within the file
src - the source buffer

encodeLength

public static int encodeLength(int len)
Convert the length to a length code 0..31. 31 means more than 1 MB.

Parameters:
len - the length
Returns:
the length code

getPageChunkId

public static int getPageChunkId(long pos)
Get the chunk id from the position.

Parameters:
pos - the position
Returns:
the chunk id

getPageMaxLength

public static int getPageMaxLength(long pos)
Get the maximum length for the given code. For the code 31, PAGE_LARGE is returned.

Parameters:
pos - the position
Returns:
the maximum length

getPageOffset

public static int getPageOffset(long pos)
Get the offset from the position.

Parameters:
pos - the position
Returns:
the offset

getPageType

public static int getPageType(long pos)
Get the page type from the position.

Parameters:
pos - the position
Returns:
the page type (PAGE_TYPE_NODE or PAGE_TYPE_LEAF)

getPagePos

public static long getPagePos(int chunkId,
                              int offset,
                              int length,
                              int type)
Get the position of this page. The following information is encoded in the position: the chunk id, the offset, the maximum length, and the type (node or leaf).

Parameters:
chunkId - the chunk id
offset - the offset
length - the length
type - the page type (1 for node, 0 for leaf)
Returns:
the position

getCheckValue

public static short getCheckValue(int x)
Calculate a check value for the given integer. A check value is mean to verify the data is consistent with a high probability, but not meant to protect against media failure or deliberate changes.

Parameters:
x - the value
Returns:
the check value

appendMap

public static java.lang.StringBuilder appendMap(java.lang.StringBuilder buff,
                                                java.util.HashMap<java.lang.String,?> map)
Append a map to the string builder, sorted by key.

Parameters:
buff - the target buffer
map - the map
Returns:
the string builder

appendMap

public static void appendMap(java.lang.StringBuilder buff,
                             java.lang.String key,
                             java.lang.Object value)
Append a key-value pair to the string builder. Keys may not contain a colon. Values that contain a comma or a double quote are enclosed in double quotes, with special characters escaped using a backslash.

Parameters:
buff - the target buffer
key - the key
value - the value

parseMap

public static java.util.HashMap<java.lang.String,java.lang.String> parseMap(java.lang.String s)
Parse a key-value pair list.

Parameters:
s - the list
Returns:
the map
Throws:
java.lang.IllegalStateException - if parsing failed

getFletcher32

public static int getFletcher32(byte[] bytes,
                                int length)
Calculate the Fletcher32 checksum.

Parameters:
bytes - the bytes
length - the message length (if odd, 0 is appended)
Returns:
the checksum

checkArgument

public static void checkArgument(boolean test,
                                 java.lang.String message,
                                 java.lang.Object... arguments)
Throw an IllegalArgumentException if the argument is invalid.

Parameters:
test - true if the argument is valid
message - the message
arguments - the arguments
Throws:
java.lang.IllegalArgumentException - if the argument is invalid

newIllegalArgumentException

public static java.lang.IllegalArgumentException newIllegalArgumentException(java.lang.String message,
                                                                             java.lang.Object... arguments)
Create a new IllegalArgumentException.

Parameters:
message - the message
arguments - the arguments
Returns:
the exception

newUnsupportedOperationException

public static java.lang.UnsupportedOperationException newUnsupportedOperationException(java.lang.String message)
Create a new UnsupportedOperationException.

Parameters:
message - the message
Returns:
the exception

newConcurrentModificationException

public static java.util.ConcurrentModificationException newConcurrentModificationException(java.lang.String message)
Create a new ConcurrentModificationException.

Parameters:
message - the message
Returns:
the exception

newIllegalStateException

public static java.lang.IllegalStateException newIllegalStateException(int errorCode,
                                                                       java.lang.String message,
                                                                       java.lang.Object... arguments)
Create a new IllegalStateException.

Parameters:
errorCode - the error code
message - the message
arguments - the arguments
Returns:
the exception

getErrorCode

public static int getErrorCode(java.lang.String m)
Get the error code from an exception message.

Parameters:
m - the message
Returns:
the error code, or 0 if none

newBytes

public static byte[] newBytes(int len)
Create an array of bytes with the given size. If this is not possible because not enough memory is available, an OutOfMemoryError with the requested size in the message is thrown.

This method should be used if the size of the array is user defined, or stored in a file, so wrong size data can be distinguished from regular out-of-memory.

Parameters:
len - the number of bytes requested
Returns:
the byte array
Throws:
java.lang.OutOfMemoryError - if the allocation was too large

ensureCapacity

public static java.nio.ByteBuffer ensureCapacity(java.nio.ByteBuffer buff,
                                                 int len)
Ensure the byte buffer has the given capacity, plus 1 KB. If not, a new, larger byte buffer is created and the data is copied.

Parameters:
buff - the byte buffer
len - the minimum remaining capacity
Returns:
the byte buffer (possibly a new one)

readHexLong

public static long readHexLong(java.util.HashMap<java.lang.String,? extends java.lang.Object> map,
                               java.lang.String key,
                               long defaultValue)
Read a hex long value from a map.

Parameters:
map - the map
key - the key
defaultValue - if the value is null
Returns:
the parsed value
Throws:
java.lang.IllegalStateException - if parsing fails

parseHexLong

public static long parseHexLong(java.lang.String x)
Parse an unsigned, hex long.

Parameters:
x - the string
Returns:
the parsed value
Throws:
java.lang.IllegalStateException - if parsing fails

parseHexInt

public static int parseHexInt(java.lang.String x)
Parse an unsigned, hex long.

Parameters:
x - the string
Returns:
the parsed value
Throws:
java.lang.IllegalStateException - if parsing fails

readHexInt

public static int readHexInt(java.util.HashMap<java.lang.String,? extends java.lang.Object> map,
                             java.lang.String key,
                             int defaultValue)
Read a hex int value from a map.

Parameters:
map - the map
key - the key
defaultValue - if the value is null
Returns:
the parsed value
Throws:
java.lang.IllegalStateException - if parsing fails