Loading…

Kryptel/Java

Conversions class

Declaration

package com.kryptel.bslx;

public final class Conversions {

  public static void ToBytes(byte[] dest, int to, short[] src, int from, int len)

  public static void ToBytes(byte[] dest, int to, int[] src, int from, int len)

  public static void ToBytes(byte[] dest, int to, long[] src, int from, int len)

  public static void FromBytes(short[] dest, int to, byte[] src, int from, int len)

  public static void FromBytes(int[] dest, int to, byte[] src, int from, int len)

  public static void FromBytes(long[] dest, int to, byte[] src, int from, int len)


  public static byte[] UuidToBytes(UUID uuid)

  public static void UuidToBytes(byte[] dest, int start, UUID uuid)

  public static UUID UuidFromBytes(byte[] src, int start)


  public static short GetAsShort(byte[] src, int start)

  public static int GetAsInt(byte[] src, int start)

  public static long GetAsLong(byte[] src, int start)
  
  
  public static byte[] ShortAsBytes(short val)
  public static void ShortAsBytes(short val, byte[] dst, int start)
  
  public static byte[] IntAsBytes(int val)
  public static void IntAsBytes(int val, byte[] dst, int start)
  
  public static byte[] LongAsBytes(long val)
  public static void LongAsBytes(long val, byte[] dst, int start)
}

Description

This class provides conversions to and from byte array missing in the standard Java library. All the conversion functions assume little-endian order of bytes.

ToBytes / FromBytes

public static void ToBytes(byte[] dest, int to, short[] src, int from, int len)

public static void ToBytes(byte[] dest, int to, int[] src, int from, int len)

public static void ToBytes(byte[] dest, int to, long[] src, int from, int len)

public static void FromBytes(short[] dest, int to, byte[] src, int from, int len)

public static void FromBytes(int[] dest, int to, byte[] src, int from, int len)

public static void FromBytes(long[] dest, int to, byte[] src, int from, int len)

Functions in this group convert a short, int, or long array to an array of bytes and back. Note that position arguments to and from specify bytes, not elements. For example, if dest is array of ints, and to is 5, then the data will by stored starting from the second byte of the second array element,

UuidToBytes / UuidFromBytes

public static byte[] UuidToBytes(UUID uuid)

public static void UuidToBytes(byte[] dest, int start, UUID uuid)

public static UUID UuidFromBytes(byte[] src, int start)

UuidToBytes functions convert UUID to a 16-byte array; UuidFromBytes constructs UUID from sixteen bytes.

GetAs*

public static short GetAsShort(byte[] src, int start)

public static int GetAsInt(byte[] src, int start)

public static long GetAsLong(byte[] src, int start)

These functions are simplified forms of corresponding FromBytes functions, returning a single scalar value. They accept 2, 4, or 8 bytes and return short, int, or long, respectively.

*AsBytes

public static byte[] ShortAsBytes(short val)
public static void ShortAsBytes(short val, byte[] dst, int start)

public static byte[] IntAsBytes(int val)
public static void IntAsBytes(int val, byte[] dst, int start)

public static byte[] LongAsBytes(long val)
public static void LongAsBytes(long val, byte[] dst, int start)

These functions complement the GetAs* function performing the reverse operation, i.e. converting a scalar value to an array of bytes.