Package net.handle.hdllib
Class Encoder
java.lang.Object
net.handle.hdllib.Encoder
The static functions in this class are used to translate message objects and
records to and from their byte-array representation in which they are sent
over the network.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final byte
static final byte
static final byte
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final String
static final byte
static final byte
static final byte
static final byte
static final int
static final int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final int
calcHandleValueSize
(byte[] values, int offset) Calculate the number of bytes required to store the specified valuestatic final int
calcStorageSize
(HandleValue value) Calculate the number of bytes required to store the specified valuestatic int
Calculate the size that a buffer would have to be in order to hold an encoded value of the given admin record.static final AddValueRequest
decodeAddValueRequest
(byte[] msg, int offset, MessageEnvelope env) Decode, create, and return an AddValueRequest object from the given bufferstatic AdminRecord
decodeAdminRecord
(byte[] data, int offset) static void
decodeAdminRecord
(byte[] data, int offset, AdminRecord admin) Decode an administrator record from the given byte array.static final CreateHandleRequest
decodeCreateHandleRequest
(byte[] msg, int offset, MessageEnvelope env, int opCode) Decode, create, and return a CreateHandleRequest from the given bufferstatic final DeleteHandleRequest
decodeDeleteHandleRequest
(byte[] msg, int offset, MessageEnvelope env) Decode, create, and return a DeleteHandleRequest from the given bufferstatic final void
decodeEnvelope
(byte[] udpPkt, MessageEnvelope msgEnv) Read the encoded value of the given buffer and populate the fields of the given message envelope object.static GenericRequest
decodeGenericRequest
(byte[] msg, int offset, int opCode, MessageEnvelope env) Decode and return a generic request method with the given encoding and opCode This returns a GenericRequest object which consists of all the normal message info along with a handle.static final GenericResponse
decodeGenericResponse
(byte[] msg, int loc, MessageEnvelope env) Decode the given generic message (ie no body, identified only by the opCode and responseCode) from the given buffer.static final HandleValue[]
Decode from the given input stream a set of handle values.static HandleValue[]
decodeHandleValue
(byte[][] handleValues) Deprecated.static final int
decodeHandleValue
(byte[] buf, int offset, HandleValue value) Populate the specified handle value with the values encoded in the given byte array and return the number of bytes read.static final HandleValue[]
decodeHandleValues
(byte[] buf) Decode from the given byte buffer, a set of handle values.static HandleValue[]
decodeHandleValues
(byte[][] handleValues) Converts a raw buffer into an array of HandleValue.Reads pairs of IP addresses into a hashtable.Reads SiteInfo/NA-list pairs into a hashtable.static final AbstractMessage
decodeMessage
(byte[] msg, int offset, MessageEnvelope envelope) decode a response message - given the message buffer and a separate 0 envelop, return a response object.static final ModifyValueRequest
decodeModifyValueRequest
(byte[] msg, int offset, MessageEnvelope env) Decode, create, and return a ModifyValueRequest object from the given bufferstatic void
decodeOpFlagsInToMessage
(AbstractMessage message, int opFlags) static final RemoveValueRequest
decodeRemoveValueRequest
(byte[] msg, int offset, MessageEnvelope env) Decode, create, and return a RemoveValueRequest object from the given bufferstatic ResolutionRequest
decodeResolutionRequest
(byte[] msg, int offset, MessageEnvelope env) Decode and create a resolution request object from the given buffer.static ResolutionResponse
decodeResolutionResponse
(byte[] msg, int offset, MessageEnvelope env) Decode and create a resolution response object from the given buffer.static ServiceReferralResponse
decodeServiceReferralResponse
(int responseCode, byte[] msg, int offset, MessageEnvelope env, int endOfBuf) static SiteInfo
decodeSiteInfoRecord
(byte[] data, int offset) Decode a site info record from a byte array.static void
decodeSiteInfoRecord
(byte[] data, int offset, SiteInfo site) Decode a site info record from a byte array.static final ValueReference[]
decodeValueReferenceList
(byte[] buf, int offset) Decode, create, and return a list of handle value references (handle/index pairs) from the given buffer.static final void
dumpBytes
(byte[] buf) Display the contents of the given buffer in a somewhat human-readable format.static final void
dumpBytes
(byte[] buf, int len) Display the contents of the given buffer in a somewhat human-readable format.static final void
dumpBytes
(byte[] buf, int offset, int len) Display the contents of the given buffer in a somewhat human-readable format.static final byte[]
Encode an AddValueRequest object and return the buffer with the encoding.static byte[]
encodeAdminRecord
(AdminRecord admin) Encode the given admin record into a byte array and return it.static final byte[]
Encode the given CreateHandleRequest and return the resulting buffer.static final byte[]
static final byte[]
Encode the given DeleteHandleRequest and return the resulting buffer.static final void
encodeEnvelope
(MessageEnvelope msgEnv, byte[] udpPkt) Write the encoded value of the given message envelope to the given buffer.static byte[]
Encode a generic request (containing a handle, and the basic header info).static final byte[]
Encode the given generic message (identified only by the opCode, and responseCode) and return the resulting buffer.static final byte[]
encodeGlobalValues
(HandleValue[] values) Encode the given handle values into a buffer that can be used as the global (or root) service/site information.static final int
encodeHandleValue
(byte[] buf, int offset, HandleValue value) Encode the values of the handle into the specified array starting at offset.static byte[]
encodeHandleValue
(HandleValue value) static byte[][]
encodeHandleValues
(HandleValue[] values) static final byte[]
encodeLocalSites
(SiteInfo[] sites, String[][] na) Encode the given String[]/SiteInfo pairs into a buffer that can be used as the local service/site information.static final byte[]
Encode the given message object as a byte array and return the resulting buffer.static final byte[]
Encode a ModifyValueRequest object and return the buffer with the encoding.static final byte[]
Encode a RemoveValueRequest object and return the buffer with the encoding.static final byte[]
Encode the given ResolutionRequest and return the resulting bufferstatic final byte[]
Encode the given ResolutionResponse and return the resulting bufferstatic byte[]
encodeSecretKey
(byte[] secretKey, boolean hash) Encode the given secret key into a byte array, performing an SHA1 hash and lower-case hex encoding if the hash flag is set.static final byte[]
static byte[]
encodeSiteInfoRecord
(SiteInfo site) Encode the given site info record into a byte array and return the result.static final byte[]
encodeValueReferenceList
(ValueReference[] values) Encode the given list of handle value references (handle/index pairs) and return the resulting byte array.static final int
getHandleValueIndex
(byte[] buf, int offset) Get only the index from the encoded handle value starting at offset.static final byte
getHandleValuePermissions
(byte[] buf, int offset) Get only the permissions from the encoded handle value starting at offset.static final byte[]
getHandleValueType
(byte[] buf, int offset) Get only the type from the encoded handle value starting at offset.static final byte[]
readByteArray
(byte[] buf, int offset) Read a byte array from the given buffer starting at the specified location.static final int
readByteArrayArray
(byte[][] a, byte[] buf, int offset) This allocates and reads an array of byte arrays where the length of the array is already known.static final int
readInt
(byte[] buf, int offset) Read a 4-byte integer value from the given byte array starting at the specified location.static final int
readInt2
(byte[] buf, int offset) Read a 2-byte integer value from the given byte array starting at the specified location.static final int[]
readIntArray
(byte[] buf, int offset) Read an array of 4-byte integer values from the given buffer starting at the specified location.static final long
readLong
(byte[] buf, int offset) Read an 8-octet integer (java long) value from the given byte array starting at the specified location.static int
readOpCode
(byte[] msg, int offset) static final int
writeByteArray
(byte[] buf, int offset, byte[] bufToWrite) Write the given byte array to a given buffer starting at the specified location.static final int
writeByteArray
(byte[] buf, int offset, byte[] bufToWrite, int woffset, int length) Write the given byte array to a given buffer starting at the specified location.static final int
writeByteArrayArray
(byte[] buf, int offset, byte[][] bufToWrite) This writes an array of byte arrays to the given buffer.static final int
writeInt
(byte[] buf, int offset, int value) Write a 4-byte integer value into the given byte array starting at the specified location.static final int
writeInt2
(byte[] buf, int offset, int value) Write a 2-byte integer value into the given byte array starting at the specified location.static final int
writeIntArray
(byte[] buf, int offset, int[] bufToWrite) This writes a given array of integers to the given buffer, starting at the specified location.static final void
writeLocalAddresses
(Map<?, ?> map, OutputStream out) Writes pairs of IP addresses into a hashtable.static final int
writeLong
(byte[] buf, int offset, long value) Write an 8-octet integer (java long) value into the given byte array starting at the specified location.
-
Field Details
-
INT_SIZE
public static final int INT_SIZE- See Also:
-
INT2_SIZE
public static final int INT2_SIZE- See Also:
-
LONG_SIZE
public static final int LONG_SIZE- See Also:
-
MSG_FLAG_AUTH
public static final int MSG_FLAG_AUTH- See Also:
-
MSG_FLAG_CERT
public static final int MSG_FLAG_CERT- See Also:
-
MSG_FLAG_ENCR
public static final int MSG_FLAG_ENCR- See Also:
-
MSG_FLAG_RECU
public static final int MSG_FLAG_RECU- See Also:
-
MSG_FLAG_CACR
public static final int MSG_FLAG_CACR- See Also:
-
MSG_FLAG_CONT
public static final int MSG_FLAG_CONT- See Also:
-
MSG_FLAG_KPAL
public static final int MSG_FLAG_KPAL- See Also:
-
MSG_FLAG_PUBL
public static final int MSG_FLAG_PUBL- See Also:
-
MSG_FLAG_RRDG
public static final int MSG_FLAG_RRDG- See Also:
-
MSG_FLAG_OVRW
public static final int MSG_FLAG_OVRW- See Also:
-
MSG_FLAG_MINT
public static final int MSG_FLAG_MINT- See Also:
-
MSG_FLAG_DNRF
public static final int MSG_FLAG_DNRF- See Also:
-
ENV_FLAG_COMPRESSED
public static final byte ENV_FLAG_COMPRESSED- See Also:
-
ENV_FLAG_ENCRYPTED
public static final byte ENV_FLAG_ENCRYPTED- See Also:
-
ENV_FLAG_TRUNCATED
public static final byte ENV_FLAG_TRUNCATED- See Also:
-
PERM_ADMIN_READ
public static final byte PERM_ADMIN_READ- See Also:
-
PERM_ADMIN_WRITE
public static final byte PERM_ADMIN_WRITE- See Also:
-
PERM_PUBLIC_READ
public static final byte PERM_PUBLIC_READ- See Also:
-
PERM_PUBLIC_WRITE
public static final byte PERM_PUBLIC_WRITE- See Also:
-
MSG_INVALID_ARRAY_SIZE
- See Also:
-
SESSION_FLAG_CERT
public static final int SESSION_FLAG_CERT- See Also:
-
SESSION_FLAG_ENCR
public static final int SESSION_FLAG_ENCR- See Also:
-
-
Constructor Details
-
Encoder
public Encoder()
-
-
Method Details
-
readLong
public static final long readLong(byte[] buf, int offset) Read an 8-octet integer (java long) value from the given byte array starting at the specified location. -
writeLong
public static final int writeLong(byte[] buf, int offset, long value) Write an 8-octet integer (java long) value into the given byte array starting at the specified location. -
readInt2
public static final int readInt2(byte[] buf, int offset) Read a 2-byte integer value from the given byte array starting at the specified location. -
writeInt2
public static final int writeInt2(byte[] buf, int offset, int value) Write a 2-byte integer value into the given byte array starting at the specified location. -
readInt
public static final int readInt(byte[] buf, int offset) Read a 4-byte integer value from the given byte array starting at the specified location. -
writeInt
public static final int writeInt(byte[] buf, int offset, int value) Write a 4-byte integer value into the given byte array starting at the specified location. -
readByteArray
Read a byte array from the given buffer starting at the specified location. This method first reads a 4-octet integer and then reads that many bytes from the buffer.- Throws:
HandleException
-
writeByteArray
public static final int writeByteArray(byte[] buf, int offset, byte[] bufToWrite) Write the given byte array to a given buffer starting at the specified location. This first writes the length of the array as a 4-octet integer, and then writes the bytes of the array. -
writeByteArray
public static final int writeByteArray(byte[] buf, int offset, byte[] bufToWrite, int woffset, int length) Write the given byte array to a given buffer starting at the specified location. This first writes the length of the array as a 4-octet integer, and then writes the bytes of the array. -
writeByteArrayArray
public static final int writeByteArrayArray(byte[] buf, int offset, byte[][] bufToWrite) This writes an array of byte arrays to the given buffer. This first writes the number of arrays as a 4-octet integer, and then writes each individual byte array using a call to writeByteArray. -
writeIntArray
public static final int writeIntArray(byte[] buf, int offset, int[] bufToWrite) This writes a given array of integers to the given buffer, starting at the specified location. This first writes the length of the integer array as a 4-octet integer, then writes each integer in the array using a call to writeInt. This will return the number of bytes that were read. -
readIntArray
Read an array of 4-byte integer values from the given buffer starting at the specified location. This method first reads a 4-octet integer and then reads that many integer values from the buffer.- Throws:
HandleException
-
readByteArrayArray
public static final int readByteArrayArray(byte[][] a, byte[] buf, int offset) throws HandleException This allocates and reads an array of byte arrays where the length of the array is already known. For each byte array in 'a', this reads a byte array using a call to readByteArray. This will return the number of bytes that were read.- Throws:
HandleException
-
dumpBytes
public static final void dumpBytes(byte[] buf) Display the contents of the given buffer in a somewhat human-readable format. This is only used for debugging. -
dumpBytes
public static final void dumpBytes(byte[] buf, int len) Display the contents of the given buffer in a somewhat human-readable format. This is only used for debugging. -
dumpBytes
public static final void dumpBytes(byte[] buf, int offset, int len) Display the contents of the given buffer in a somewhat human-readable format. This is only used for debugging. -
readOpCode
public static int readOpCode(byte[] msg, int offset) -
decodeMessage
public static final AbstractMessage decodeMessage(byte[] msg, int offset, MessageEnvelope envelope) throws HandleException decode a response message - given the message buffer and a separate 0 envelop, return a response object. If the message is a certified message, the signature for the message is read and put into the message object.- Throws:
HandleException
-
decodeOpFlagsInToMessage
-
decodeSiteInfoRecord
Decode a site info record from a byte array.- Throws:
HandleException
-
decodeSiteInfoRecord
public static void decodeSiteInfoRecord(byte[] data, int offset, SiteInfo site) throws HandleException Decode a site info record from a byte array.- Throws:
HandleException
-
encodeSiteInfoRecord
Encode the given site info record into a byte array and return the result. -
decodeAdminRecord
- Throws:
HandleException
-
decodeAdminRecord
public static void decodeAdminRecord(byte[] data, int offset, AdminRecord admin) throws HandleException Decode an administrator record from the given byte array.- Throws:
HandleException
-
encodeAdminRecord
Encode the given admin record into a byte array and return it. -
encodeSecretKey
Encode the given secret key into a byte array, performing an SHA1 hash and lower-case hex encoding if the hash flag is set. If the hash flag is not set this may return the same secretKey array that was passed as a parameter.- Throws:
Exception
-
calculateAdminRecordSize
Calculate the size that a buffer would have to be in order to hold an encoded value of the given admin record. -
encodeGenericRequest
Encode a generic request (containing a handle, and the basic header info). -
decodeGenericRequest
public static GenericRequest decodeGenericRequest(byte[] msg, int offset, int opCode, MessageEnvelope env) throws HandleException Decode and return a generic request method with the given encoding and opCode This returns a GenericRequest object which consists of all the normal message info along with a handle.- Throws:
HandleException
-
encodeHandleValue
-
encodeHandleValues
-
encodeHandleValue
Encode the values of the handle into the specified array starting at offset.- Returns:
- the number of bytes written to the array.;
-
getHandleValueType
Get only the type from the encoded handle value starting at offset.- Throws:
HandleException
-
getHandleValueIndex
public static final int getHandleValueIndex(byte[] buf, int offset) Get only the index from the encoded handle value starting at offset. -
getHandleValuePermissions
public static final byte getHandleValuePermissions(byte[] buf, int offset) Get only the permissions from the encoded handle value starting at offset. -
calcStorageSize
Calculate the number of bytes required to store the specified value -
calcHandleValueSize
public static final int calcHandleValueSize(byte[] values, int offset) Calculate the number of bytes required to store the specified value -
decodeHandleValues
Converts a raw buffer into an array of HandleValue.- Parameters:
handleValues
-- Returns:
- null in the case the handleValues parameter is null;
- Throws:
HandleException
-
decodeHandleValue
@Deprecated public static HandleValue[] decodeHandleValue(byte[][] handleValues) throws HandleException Deprecated.- Throws:
HandleException
-
decodeHandleValue
public static final int decodeHandleValue(byte[] buf, int offset, HandleValue value) throws HandleException Populate the specified handle value with the values encoded in the given byte array and return the number of bytes read.- Throws:
HandleException
-
encodeMessage
Encode the given message object as a byte array and return the resulting buffer. The isResponse flag is necessary to determine the type of object being encoded (not really, maybe this can be cleaned up).- Throws:
HandleException
-
encodeEnvelope
Write the encoded value of the given message envelope to the given buffer. -
decodeEnvelope
public static final void decodeEnvelope(byte[] udpPkt, MessageEnvelope msgEnv) throws HandleException Read the encoded value of the given buffer and populate the fields of the given message envelope object.- Throws:
HandleException
-
decodeAddValueRequest
public static final AddValueRequest decodeAddValueRequest(byte[] msg, int offset, MessageEnvelope env) throws HandleException Decode, create, and return an AddValueRequest object from the given buffer- Throws:
HandleException
-
encodeAddValueRequest
Encode an AddValueRequest object and return the buffer with the encoding. -
decodeModifyValueRequest
public static final ModifyValueRequest decodeModifyValueRequest(byte[] msg, int offset, MessageEnvelope env) throws HandleException Decode, create, and return a ModifyValueRequest object from the given buffer- Throws:
HandleException
-
encodeModifyValueRequest
Encode a ModifyValueRequest object and return the buffer with the encoding. -
decodeRemoveValueRequest
public static final RemoveValueRequest decodeRemoveValueRequest(byte[] msg, int offset, MessageEnvelope env) throws HandleException Decode, create, and return a RemoveValueRequest object from the given buffer- Throws:
HandleException
-
encodeRemoveValueRequest
Encode a RemoveValueRequest object and return the buffer with the encoding. -
decodeDeleteHandleRequest
public static final DeleteHandleRequest decodeDeleteHandleRequest(byte[] msg, int offset, MessageEnvelope env) throws HandleException Decode, create, and return a DeleteHandleRequest from the given buffer- Throws:
HandleException
-
encodeDeleteHandleRequest
Encode the given DeleteHandleRequest and return the resulting buffer. -
decodeGenericResponse
Decode the given generic message (ie no body, identified only by the opCode and responseCode) from the given buffer. -
encodeGenericResponse
Encode the given generic message (identified only by the opCode, and responseCode) and return the resulting buffer. -
decodeCreateHandleRequest
public static final CreateHandleRequest decodeCreateHandleRequest(byte[] msg, int offset, MessageEnvelope env, int opCode) throws HandleException Decode, create, and return a CreateHandleRequest from the given buffer- Throws:
HandleException
-
encodeCreateHandleRequest
Encode the given CreateHandleRequest and return the resulting buffer. -
decodeResolutionRequest
public static ResolutionRequest decodeResolutionRequest(byte[] msg, int offset, MessageEnvelope env) throws HandleException Decode and create a resolution request object from the given buffer.- Throws:
HandleException
-
decodeResolutionResponse
public static ResolutionResponse decodeResolutionResponse(byte[] msg, int offset, MessageEnvelope env) throws HandleException Decode and create a resolution response object from the given buffer.- Throws:
HandleException
-
decodeServiceReferralResponse
public static ServiceReferralResponse decodeServiceReferralResponse(int responseCode, byte[] msg, int offset, MessageEnvelope env, int endOfBuf) throws HandleException - Throws:
HandleException
-
encodeResolutionRequest
Encode the given ResolutionRequest and return the resulting buffer -
encodeResolutionResponse
Encode the given ResolutionResponse and return the resulting buffer -
encodeServiceReferralResponse
-
decodeValueReferenceList
public static final ValueReference[] decodeValueReferenceList(byte[] buf, int offset) throws HandleException Decode, create, and return a list of handle value references (handle/index pairs) from the given buffer.- Throws:
HandleException
-
encodeValueReferenceList
Encode the given list of handle value references (handle/index pairs) and return the resulting byte array. -
encodeGlobalValues
Encode the given handle values into a buffer that can be used as the global (or root) service/site information. -
encodeLocalSites
Encode the given String[]/SiteInfo pairs into a buffer that can be used as the local service/site information. For the SiteInfo object at sites[i] the corresponding prefixes should be listed in na[i].- Throws:
HandleException
-
decodeLocalSites
Reads SiteInfo/NA-list pairs into a hashtable. Each NA value is used as a key to a SiteInfo[].- Throws:
HandleException
-
decodeLocalAddresses
Reads pairs of IP addresses into a hashtable.- Throws:
HandleException
-
writeLocalAddresses
Writes pairs of IP addresses into a hashtable.- Throws:
IOException
-
decodeGlobalValues
Decode from the given input stream a set of handle values. The stream should contain data in the format output from the encodeGlobalValues() function.- Throws:
HandleException
-
decodeHandleValues
Decode from the given byte buffer, a set of handle values.- Throws:
HandleException
-
encodeCreateHandleResponse
-
decodeHandleValues(byte[][])
.