Class MessagePackReader


  • public class MessagePackReader
    extends java.lang.Object
    A simple MessagePack reader. See https://github.com/msgpack/msgpack/blob/master/spec.md with very few dependencies. Nuances: Does not implement unsigned int32 or int64 (delivers simple integers in all cases). Does not use doubles; just floats Note: homogeneousArrays == true will deliver null for empty array. Use in MMTF: BufferedInputStream bs = [whatever] GenericBinaryDocument binaryDoc = new javajs.util.BinaryDocument(); binaryDoc.setStream(bs, true); map = (new MessagePackReader(binaryDoc, true)).readMap(); entities = (Object[]) map.get("entityList"); float[] x = (float[]) decode((byte[]) map.get("xCoordList"))
    Author:
    Bob Hanson hansonr@stolaf.edu
    • Constructor Summary

      Constructors 
      Constructor Description
      MessagePackReader​(javajs.api.GenericBinaryDocument binaryDoc, boolean isHomogeneousArrays)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object decode​(byte[] b)
      This single method takes care of all MMTF needs.
      private java.lang.Object getArray​(int n)  
      static float[] getFloats​(byte[] b, int n, float divisor)
      mmtf type 1 and 11 byte[4] to float32
      static int[] getInts​(byte[] b, int n)
      mmtf types 2-4 Decode a byte array into a byte, short, or int array.
      private java.lang.Object getMap​(int n)  
      java.lang.Object getNext​(java.lang.Object array, int pt)  
      java.util.Map<java.lang.String,​java.lang.Object> readMap()  
      static int[] rldecode32​(byte[] b, int n)
      mmtf type 7 Decode an array of int32 using run-length decoding.
      static int[] rldecode32Delta​(byte[] b, int n)
      mmtf type 8 Decode an array of int32 using run-length decoding of a difference array.
      static char[] rldecode32ToChar​(byte[] b, int n)
      mmtf type 6 Decode an array of int32 using run-length decoding to one char per int.
      static java.lang.String[] rldecode32ToStr​(byte[] b)
      mmtf type 5 Decode each four bytes as a 1- to 4-character string label where a 0 byte indicates end-of-string.
      static float[] rldecodef​(byte[] b, int n, float divisor)
      mmtf type 9 Decode an array of int32 using run-length decoding and divide by a divisor to give a float32.
      static int[] unpack​(byte[] b, int nBytes, int n)
      mmtf type 14 and 15 Unpack an array of int8 or int16 to int32.
      static float[] unpack16Deltaf​(byte[] b, int n, float divisor)
      mmtf type 10 Decode an array of int16 using run-length decoding of a difference array.
      static float[] unpackf​(byte[] b, int nBytes, int n, float divisor)
      mmtf type 12 and 13 Unpack an array of int8 or int16 to int32 and divide to give a float32.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MessagePackReader

        public MessagePackReader​(javajs.api.GenericBinaryDocument binaryDoc,
                                 boolean isHomogeneousArrays)
    • Method Detail

      • readMap

        public java.util.Map<java.lang.String,​java.lang.Object> readMap()
                                                                       throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getNext

        public java.lang.Object getNext​(java.lang.Object array,
                                        int pt)
                                 throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getArray

        private java.lang.Object getArray​(int n)
                                   throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getMap

        private java.lang.Object getMap​(int n)
                                 throws java.lang.Exception
        Throws:
        java.lang.Exception
      • decode

        public static java.lang.Object decode​(byte[] b)
        This single method takes care of all MMTF needs. See https://github.com/rcsb/mmtf/blob/master/spec.md
        Parameters:
        b -
        Returns:
        array of int, char, or float, depending upon the type
      • getFloats

        public static float[] getFloats​(byte[] b,
                                        int n,
                                        float divisor)
        mmtf type 1 and 11 byte[4] to float32
        Parameters:
        b -
        n -
        divisor -
        Returns:
        array of floats
      • getInts

        public static int[] getInts​(byte[] b,
                                    int n)
        mmtf types 2-4 Decode a byte array into a byte, short, or int array.
        Parameters:
        b -
        n -
        Returns:
        array of integers
      • rldecode32ToStr

        public static java.lang.String[] rldecode32ToStr​(byte[] b)
        mmtf type 5 Decode each four bytes as a 1- to 4-character string label where a 0 byte indicates end-of-string.
        Parameters:
        b - a byte array
        Returns:
        String[]
      • rldecode32ToChar

        public static char[] rldecode32ToChar​(byte[] b,
                                              int n)
        mmtf type 6 Decode an array of int32 using run-length decoding to one char per int.
        Parameters:
        b -
        n -
        Returns:
        array of characters
      • rldecode32

        public static int[] rldecode32​(byte[] b,
                                       int n)
        mmtf type 7 Decode an array of int32 using run-length decoding.
        Parameters:
        b -
        n -
        Returns:
        array of integers
      • rldecode32Delta

        public static int[] rldecode32Delta​(byte[] b,
                                            int n)
        mmtf type 8 Decode an array of int32 using run-length decoding of a difference array.
        Parameters:
        b -
        n -
        Returns:
        array of integers
      • rldecodef

        public static float[] rldecodef​(byte[] b,
                                        int n,
                                        float divisor)
        mmtf type 9 Decode an array of int32 using run-length decoding and divide by a divisor to give a float32.
        Parameters:
        b -
        n -
        divisor -
        Returns:
        array of floats
      • unpack16Deltaf

        public static float[] unpack16Deltaf​(byte[] b,
                                             int n,
                                             float divisor)
        mmtf type 10 Decode an array of int16 using run-length decoding of a difference array.
        Parameters:
        b -
        n -
        divisor -
        Returns:
        array of floats
      • unpackf

        public static float[] unpackf​(byte[] b,
                                      int nBytes,
                                      int n,
                                      float divisor)
        mmtf type 12 and 13 Unpack an array of int8 or int16 to int32 and divide to give a float32. untested
        Parameters:
        b -
        nBytes -
        n -
        divisor -
        Returns:
        array of floats
      • unpack

        public static int[] unpack​(byte[] b,
                                   int nBytes,
                                   int n)
        mmtf type 14 and 15 Unpack an array of int8 or int16 to int32. untested
        Parameters:
        b -
        nBytes -
        n -
        Returns:
        array of integers