Package org.postgresql.util
Class ByteConverter
- java.lang.Object
-
- org.postgresql.util.ByteConverter
-
public class ByteConverter extends java.lang.Object
Helper methods to parse java base types from byte arrays.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
ByteConverter.PositiveShorts
Simple stack structure for non-negativeshort
values.
-
Field Summary
Fields Modifier and Type Field Description private static java.math.BigInteger
BI_MAX_LONG
private static java.math.BigInteger[]
BI_TEN_POWERS
private static java.math.BigInteger
BI_TEN_THOUSAND
private static int[]
INT_TEN_POWERS
private static int
LONG_BYTES
private static long[]
LONG_TEN_POWERS
private static int
NUMERIC_DSCALE_MASK
private static short
NUMERIC_NAN
private static short
NUMERIC_NEG
private static short
NUMERIC_NINF
private static short
NUMERIC_PINF
private static short
NUMERIC_POS
private static int
SHORT_BYTES
-
Constructor Summary
Constructors Modifier Constructor Description private
ByteConverter()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
bool(byte[] bytes, int idx)
Parses a boolean value from the byte array.static void
bool(byte[] target, int idx, boolean value)
Encodes a boolean value to the byte array.static int
bytesToInt(byte[] bytes)
Convert a variable length array of bytes to an integerstatic float
float4(byte[] bytes, int idx)
Parses a float value from the byte array.static void
float4(byte[] target, int idx, float value)
Encodes a int value to the byte array.static double
float8(byte[] bytes, int idx)
Parses a double value from the byte array.static void
float8(byte[] target, int idx, double value)
Encodes a int value to the byte array.static short
int2(byte[] bytes, int idx)
Parses a short value from the byte array.static void
int2(byte[] target, int idx, int value)
Encodes a int value to the byte array.static int
int4(byte[] bytes, int idx)
Parses an int value from the byte array.static void
int4(byte[] target, int idx, int value)
Encodes a int value to the byte array.static long
int8(byte[] bytes, int idx)
Parses a long value from the byte array.static void
int8(byte[] target, int idx, long value)
Encodes a long value to the byte array.static java.lang.Number
numeric(byte[] bytes)
Convert a variable length array of bytes to an integerstatic java.lang.Number
numeric(byte[] bytes, int pos, int numBytes)
Convert a variable length array of bytes to aNumber
.static byte[]
numeric(java.math.BigDecimal nbr)
Converts a non-nullBigDecimal
to binary format forOid.NUMERIC
.private static java.math.BigInteger
tenPower(int exponent)
-
-
-
Field Detail
-
NUMERIC_DSCALE_MASK
private static final int NUMERIC_DSCALE_MASK
- See Also:
- Constant Field Values
-
NUMERIC_POS
private static final short NUMERIC_POS
- See Also:
- Constant Field Values
-
NUMERIC_NEG
private static final short NUMERIC_NEG
- See Also:
- Constant Field Values
-
NUMERIC_NAN
private static final short NUMERIC_NAN
- See Also:
- Constant Field Values
-
NUMERIC_PINF
private static final short NUMERIC_PINF
- See Also:
- Constant Field Values
-
NUMERIC_NINF
private static final short NUMERIC_NINF
- See Also:
- Constant Field Values
-
SHORT_BYTES
private static final int SHORT_BYTES
- See Also:
- Constant Field Values
-
LONG_BYTES
private static final int LONG_BYTES
- See Also:
- Constant Field Values
-
INT_TEN_POWERS
private static final int[] INT_TEN_POWERS
-
LONG_TEN_POWERS
private static final long[] LONG_TEN_POWERS
-
BI_TEN_POWERS
private static final java.math.BigInteger[] BI_TEN_POWERS
-
BI_TEN_THOUSAND
private static final java.math.BigInteger BI_TEN_THOUSAND
-
BI_MAX_LONG
private static final java.math.BigInteger BI_MAX_LONG
-
-
Method Detail
-
bytesToInt
public static int bytesToInt(byte[] bytes)
Convert a variable length array of bytes to an integer- Parameters:
bytes
- array of bytes that can be decoded as an integer- Returns:
- integer
-
numeric
public static java.lang.Number numeric(byte[] bytes)
Convert a variable length array of bytes to an integer- Parameters:
bytes
- array of bytes that can be decoded as an integer- Returns:
- integer
-
numeric
public static java.lang.Number numeric(byte[] bytes, int pos, int numBytes)
Convert a variable length array of bytes to aNumber
. The result will always be aBigDecimal
or a specialDouble
value.- Parameters:
bytes
- array of bytes to be decoded from binary numeric representation.pos
- index of the start position of the bytes array for numbernumBytes
- number of bytes to use, length is already encoded in the binary format but this is used for double checking- Returns:
- BigDecimal representation of numeric or one of the special double values
Double.NaN
,Double.NEGATIVE_INFINITY
andDouble.POSITIVE_INFINITY
.
-
numeric
public static byte[] numeric(java.math.BigDecimal nbr)
Converts a non-nullBigDecimal
to binary format forOid.NUMERIC
.- Parameters:
nbr
- The instance to represent in binary.- Returns:
- The binary representation of nbr.
-
tenPower
private static java.math.BigInteger tenPower(int exponent)
-
int8
public static long int8(byte[] bytes, int idx)
Parses a long value from the byte array.- Parameters:
bytes
- The byte array to parse.idx
- The starting index of the parse in the byte array.- Returns:
- parsed long value.
-
int4
public static int int4(byte[] bytes, int idx)
Parses an int value from the byte array.- Parameters:
bytes
- The byte array to parse.idx
- The starting index of the parse in the byte array.- Returns:
- parsed int value.
-
int2
public static short int2(byte[] bytes, int idx)
Parses a short value from the byte array.- Parameters:
bytes
- The byte array to parse.idx
- The starting index of the parse in the byte array.- Returns:
- parsed short value.
-
bool
public static boolean bool(byte[] bytes, int idx)
Parses a boolean value from the byte array.- Parameters:
bytes
- The byte array to parse.idx
- The starting index to read from bytes.- Returns:
- parsed boolean value.
-
float4
public static float float4(byte[] bytes, int idx)
Parses a float value from the byte array.- Parameters:
bytes
- The byte array to parse.idx
- The starting index of the parse in the byte array.- Returns:
- parsed float value.
-
float8
public static double float8(byte[] bytes, int idx)
Parses a double value from the byte array.- Parameters:
bytes
- The byte array to parse.idx
- The starting index of the parse in the byte array.- Returns:
- parsed double value.
-
int8
public static void int8(byte[] target, int idx, long value)
Encodes a long value to the byte array.- Parameters:
target
- The byte array to encode to.idx
- The starting index in the byte array.value
- The value to encode.
-
int4
public static void int4(byte[] target, int idx, int value)
Encodes a int value to the byte array.- Parameters:
target
- The byte array to encode to.idx
- The starting index in the byte array.value
- The value to encode.
-
int2
public static void int2(byte[] target, int idx, int value)
Encodes a int value to the byte array.- Parameters:
target
- The byte array to encode to.idx
- The starting index in the byte array.value
- The value to encode.
-
bool
public static void bool(byte[] target, int idx, boolean value)
Encodes a boolean value to the byte array.- Parameters:
target
- The byte array to encode to.idx
- The starting index in the byte array.value
- The value to encode.
-
float4
public static void float4(byte[] target, int idx, float value)
Encodes a int value to the byte array.- Parameters:
target
- The byte array to encode to.idx
- The starting index in the byte array.value
- The value to encode.
-
float8
public static void float8(byte[] target, int idx, double value)
Encodes a int value to the byte array.- Parameters:
target
- The byte array to encode to.idx
- The starting index in the byte array.value
- The value to encode.
-
-