Class Encoder


  • public abstract class Encoder
    extends java.lang.Object
    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.
    • Constructor Detail

      • Encoder

        public Encoder()
    • Method Detail

      • 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

        public static final byte[] readByteArray​(byte[] buf,
                                                 int offset)
                                          throws HandleException
        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

        public static final int[] readIntArray​(byte[] buf,
                                               int offset)
                                        throws HandleException
        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

        public static void decodeOpFlagsInToMessage​(AbstractMessage message,
                                                    int opFlags)
      • decodeSiteInfoRecord

        public static SiteInfo decodeSiteInfoRecord​(byte[] data,
                                                    int offset)
                                             throws HandleException
        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

        public static byte[] encodeSiteInfoRecord​(SiteInfo site)
        Encode the given site info record into a byte array and return the result.
      • 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

        public static byte[] encodeAdminRecord​(AdminRecord admin)
        Encode the given admin record into a byte array and return it.
      • encodeSecretKey

        public static byte[] encodeSecretKey​(byte[] secretKey,
                                             boolean hash)
                                      throws java.lang.Exception
        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:
        java.lang.Exception
      • calculateAdminRecordSize

        public static int calculateAdminRecordSize​(AdminRecord admin)
        Calculate the size that a buffer would have to be in order to hold an encoded value of the given admin record.
      • encodeGenericRequest

        public static byte[] encodeGenericRequest​(AbstractRequest req)
        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

        public static byte[] encodeHandleValue​(HandleValue value)
      • encodeHandleValues

        public static byte[][] encodeHandleValues​(HandleValue[] values)
      • encodeHandleValue

        public static final int encodeHandleValue​(byte[] buf,
                                                  int offset,
                                                  HandleValue value)
        Encode the values of the handle into the specified array starting at offset.
        Returns:
        the number of bytes written to the array.;
      • getHandleValueType

        public static final byte[] getHandleValueType​(byte[] buf,
                                                      int offset)
                                               throws HandleException
        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

        public static final int calcStorageSize​(HandleValue value)
        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

        public static HandleValue[] decodeHandleValues​(byte[][] handleValues)
                                                throws HandleException
        Converts a raw buffer into an array of HandleValue.
        Parameters:
        handleValues -
        Returns:
        null in the case the handleValues parameter is null;
        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

        public static final byte[] encodeMessage​(AbstractMessage msg)
                                          throws HandleException
        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

        public static final void encodeEnvelope​(MessageEnvelope msgEnv,
                                                byte[] udpPkt)
        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
      • encodeAddValueRequest

        public static final byte[] encodeAddValueRequest​(AddValueRequest req)
        Encode an AddValueRequest object and return the buffer with the encoding.
      • encodeModifyValueRequest

        public static final byte[] encodeModifyValueRequest​(ModifyValueRequest req)
        Encode a ModifyValueRequest object and return the buffer with the encoding.
      • encodeRemoveValueRequest

        public static final byte[] encodeRemoveValueRequest​(RemoveValueRequest req)
        Encode a RemoveValueRequest object and return the buffer with the encoding.
      • encodeDeleteHandleRequest

        public static final byte[] encodeDeleteHandleRequest​(DeleteHandleRequest req)
        Encode the given DeleteHandleRequest and return the resulting buffer.
      • decodeGenericResponse

        public 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.
      • encodeGenericResponse

        public static final byte[] encodeGenericResponse​(AbstractMessage res)
        Encode the given generic message (identified only by the opCode, and responseCode) and return the resulting buffer.
      • encodeCreateHandleRequest

        public static final byte[] encodeCreateHandleRequest​(CreateHandleRequest req)
        Encode the given CreateHandleRequest and return the resulting buffer.
      • encodeResolutionRequest

        public static final byte[] encodeResolutionRequest​(ResolutionRequest req)
        Encode the given ResolutionRequest and return the resulting buffer
      • encodeResolutionResponse

        public static final byte[] encodeResolutionResponse​(ResolutionResponse res)
        Encode the given ResolutionResponse and return the resulting buffer
      • encodeServiceReferralResponse

        public static final byte[] encodeServiceReferralResponse​(ServiceReferralResponse res)
      • 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

        public static final byte[] encodeValueReferenceList​(ValueReference[] values)
        Encode the given list of handle value references (handle/index pairs) and return the resulting byte array.
      • encodeGlobalValues

        public 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.
      • encodeLocalSites

        public static final byte[] encodeLocalSites​(SiteInfo[] sites,
                                                    java.lang.String[][] na)
                                             throws HandleException
        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

        public static final java.util.Map<java.lang.String,​SiteInfo[]> decodeLocalSites​(java.io.InputStream in)
                                                                                       throws HandleException
        Reads SiteInfo/NA-list pairs into a hashtable. Each NA value is used as a key to a SiteInfo[].
        Throws:
        HandleException
      • decodeLocalAddresses

        public static final java.util.Map<java.lang.String,​java.lang.String> decodeLocalAddresses​(java.io.InputStream in)
                                                                                                 throws HandleException
        Reads pairs of IP addresses into a hashtable.
        Throws:
        HandleException
      • writeLocalAddresses

        public static final void writeLocalAddresses​(java.util.Map<?,​?> map,
                                                     java.io.OutputStream out)
                                              throws java.io.IOException
        Writes pairs of IP addresses into a hashtable.
        Throws:
        java.io.IOException
      • decodeGlobalValues

        public static final HandleValue[] decodeGlobalValues​(java.io.InputStream in)
                                                      throws HandleException
        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
      • encodeCreateHandleResponse

        public static final byte[] encodeCreateHandleResponse​(CreateHandleResponse res)