Exemplo n.º 1
0
        // reads long value from buffers
        private static long consumeLong(ByteIterator bytes)
        {
            long lower       = consumeInt(bytes);
            long lowerMasked = (lower & 0x00000000ffffffffL);

            return(lowerMasked + ((long)consumeInt(bytes) << 32));
        }
Exemplo n.º 2
0
        public static void Deserialize(Parameters parameters,
                IList<byte[]> buffers)
        {
            ByteIterator bytes = new ByteIterator(buffers);

            deserializeFromBytes(parameters, bytes);
        }
Exemplo n.º 3
0
        // reads a binary object
        private static byte[] consumeBinary(ByteIterator bytes)
        {
            int length = consumeInt(bytes);

            byte[] buf = consumeBytes(bytes, length);
            return(buf);
        }
Exemplo n.º 4
0
        public static void Deserialize(Parameters parameters,
                                       IList <byte[]> buffers)
        {
            ByteIterator bytes = new ByteIterator(buffers);

            deserializeFromBytes(parameters, bytes);
        }
Exemplo n.º 5
0
 // comsumes integer value from buffers
 private static int consumeInt(ByteIterator bytes)
 {
     byte[] buf =
     {
         bytes.next(),
         bytes.next(),
         bytes.next(),
         bytes.next()
     };
     return(readInt(buf, 0));
 }
Exemplo n.º 6
0
        // reads a string object
        private static string consumeString(ByteIterator bytes)
        {
            int length = consumeInt(bytes);

            byte[] buf = consumeBytes(bytes, length);
            try
            {
                return(encoding.GetString(buf));
            }
            catch (DecoderFallbackException e)
            {
                throw new UnexpectedValueException(e.Message);
            }
        }
Exemplo n.º 7
0
        // reads a byte array of given length
        private static byte[] consumeBytes(ByteIterator bytes, int length)
        {
            int paddedLength = roundTo4(length);

            byte[] buf = new byte[length];
            for (int i = 0; i != length; ++i)
            {
                buf[i] = bytes.next();
            }

            // consume the remaining padding bytes (if any)
            for (int i = length; i != paddedLength; ++i)
            {
                bytes.next();
            }

            return(buf);
        }
Exemplo n.º 8
0
        // reads double value from the buffer with given endianness
        private static double consumeDouble(ByteIterator bytes)
        {
            long longBits = consumeLong(bytes);

            return(BitConverter.Int64BitsToDouble(longBits));
        }
Exemplo n.º 9
0
        private static void deserializeFromBytes(Parameters parameters,
                                                 ByteIterator bytes)
        {
            int numOfEntries = consumeInt(bytes);

            for (int i = 0; i != numOfEntries; ++i)
            {
                string entryName = consumeString(bytes);

                int typeCode = consumeInt(bytes);
                Parameters.EntryType type = codeToType(typeCode);
                switch (type)
                {
                case Parameters.EntryType.BOOLEAN:
                {
                    int value = consumeInt(bytes);
                    parameters.SetBoolean(entryName, value != 0);
                }
                break;

                case Parameters.EntryType.INTEGER:
                {
                    int value = consumeInt(bytes);
                    parameters.SetInteger(entryName, value);
                }
                break;

                case Parameters.EntryType.LONG:
                {
                    long value = consumeLong(bytes);
                    parameters.SetLong(entryName, value);
                }
                break;

                case Parameters.EntryType.DOUBLE:
                {
                    double value = consumeDouble(bytes);
                    parameters.SetDouble(entryName, value);
                }
                break;

                case Parameters.EntryType.STRING:
                {
                    string value = consumeString(bytes);
                    parameters.SetString(entryName, value);
                }
                break;

                case Parameters.EntryType.BINARY:
                {
                    byte[] value = consumeBinary(bytes);
                    parameters.SetBinary(entryName, value);
                }
                break;

                case Parameters.EntryType.BOOLEAN_ARRAY:
                {
                    int arrayLength = consumeInt(bytes);
                    int bytesNeeded =
                        (arrayLength + BITS_IN_BYTE - 1)
                        / BITS_IN_BYTE;
                    byte[] packedArray =
                        consumeBytes(bytes, bytesNeeded);
                    bool[] array = new bool[arrayLength];
                    for (int j = 0; j != arrayLength; ++j)
                    {
                        int bytePosition = j / BITS_IN_BYTE;
                        int bitPosition  = j % BITS_IN_BYTE;
                        int mask         = 1 << bitPosition;
                        if ((packedArray[bytePosition] & mask) != 0)
                        {
                            array[j] = true;
                        }
                    }

                    parameters.SetBooleanArray(entryName, array);
                }
                break;

                case Parameters.EntryType.INTEGER_ARRAY:
                {
                    int   arrayLength = consumeInt(bytes);
                    int[] array       = new int[arrayLength];
                    for (int j = 0; j != arrayLength; ++j)
                    {
                        array[j] = consumeInt(bytes);
                    }

                    parameters.SetIntegerArray(entryName, array);
                }
                break;

                case Parameters.EntryType.LONG_ARRAY:
                {
                    int    arrayLength = consumeInt(bytes);
                    long[] array       = new long[arrayLength];
                    for (int j = 0; j != arrayLength; ++j)
                    {
                        array[j] = consumeLong(bytes);
                    }

                    parameters.SetLongArray(entryName, array);
                }
                break;

                case Parameters.EntryType.DOUBLE_ARRAY:
                {
                    int      arrayLength = consumeInt(bytes);
                    double[] array       = new double[arrayLength];
                    for (int j = 0; j != arrayLength; ++j)
                    {
                        array[j] = consumeDouble(bytes);
                    }

                    parameters.SetDoubleArray(entryName, array);
                }
                break;

                case Parameters.EntryType.STRING_ARRAY:
                {
                    int      arrayLength = consumeInt(bytes);
                    string[] array       = new string[arrayLength];
                    for (int j = 0; j != arrayLength; ++j)
                    {
                        array[j] = consumeString(bytes);
                    }

                    parameters.SetStringArray(entryName, array);
                }
                break;

                case Parameters.EntryType.BINARY_ARRAY:
                {
                    int      arrayLength = consumeInt(bytes);
                    byte[][] array       = new byte[arrayLength][];
                    for (int j = 0; j != arrayLength; ++j)
                    {
                        array[j] = consumeBinary(bytes);
                    }

                    parameters.SetBinaryArray(entryName, array);
                }
                break;

                case Parameters.EntryType.NESTED_PARAMETERS:
                {
                    Parameters nested = new Parameters();
                    deserializeFromBytes(nested, bytes);
                    parameters.SetNestedParameters(entryName, nested);
                }
                break;

                case Parameters.EntryType.NESTED_PARAMETERS_ARRAY:
                {
                    int          arrayLength = consumeInt(bytes);
                    Parameters[] nested      = new Parameters[arrayLength];
                    for (int j = 0; j != arrayLength; ++j)
                    {
                        nested[j] = new Parameters();
                        deserializeFromBytes(nested[j], bytes);
                    }
                    parameters.SetNestedArray(entryName, nested);
                }
                break;
                }
            }
        }
Exemplo n.º 10
0
        // reads a byte array of given length
        private static byte[] consumeBytes(ByteIterator bytes, int length)
        {
            int paddedLength = roundTo4(length);

            byte[] buf = new byte[length];
            for(int i = 0; i != length; ++i)
            {
                buf[i] = bytes.next();
            }

            // consume the remaining padding bytes (if any)
            for(int i = length; i != paddedLength; ++i)
            {
                bytes.next();
            }

            return buf;
        }
Exemplo n.º 11
0
        private static void deserializeFromBytes(Parameters parameters,
                ByteIterator bytes)
        {
            int numOfEntries = consumeInt(bytes);
            for(int i = 0; i != numOfEntries; ++i)
            {
                string entryName = consumeString(bytes);

                int typeCode = consumeInt(bytes);
                Parameters.EntryType type = codeToType(typeCode);
                switch(type)
                {
                case Parameters.EntryType.BOOLEAN:
                    {
                        int value = consumeInt(bytes);
                        parameters.SetBoolean(entryName, value != 0);
                    }
                    break;
                case Parameters.EntryType.INTEGER:
                    {
                        int value = consumeInt(bytes);
                        parameters.SetInteger(entryName, value);
                    }
                    break;
                case Parameters.EntryType.LONG:
                    {
                        long value = consumeLong(bytes);
                        parameters.SetLong(entryName, value);
                    }
                    break;
                case Parameters.EntryType.DOUBLE:
                    {
                        double value = consumeDouble(bytes);
                        parameters.SetDouble(entryName, value);
                    }
                    break;
                case Parameters.EntryType.STRING:
                    {
                        string value = consumeString(bytes);
                        parameters.SetString(entryName, value);
                    }
                    break;
                case Parameters.EntryType.BINARY:
                    {
                        byte[] value = consumeBinary(bytes);
                        parameters.SetBinary(entryName, value);
                    }
                    break;
                case Parameters.EntryType.BOOLEAN_ARRAY:
                    {
                        int arrayLength = consumeInt(bytes);
                        int bytesNeeded =
                                (arrayLength + BITS_IN_BYTE - 1)
                                / BITS_IN_BYTE;
                        byte[] packedArray =
                            consumeBytes(bytes, bytesNeeded);
                        bool[] array = new bool[arrayLength];
                        for(int j = 0; j != arrayLength; ++j)
                        {
                            int bytePosition = j / BITS_IN_BYTE;
                            int bitPosition = j % BITS_IN_BYTE;
                            int mask = 1 << bitPosition;
                            if((packedArray[bytePosition] & mask) != 0)
                            {
                                array[j] = true;
                            }
                        }

                        parameters.SetBooleanArray(entryName, array);
                    }
                    break;
                case Parameters.EntryType.INTEGER_ARRAY:
                    {
                        int arrayLength = consumeInt(bytes);
                        int[] array = new int[arrayLength];
                        for(int j = 0; j != arrayLength; ++j)
                        {
                            array[j] = consumeInt(bytes);
                        }

                        parameters.SetIntegerArray(entryName, array);
                    }
                    break;
                case Parameters.EntryType.LONG_ARRAY:
                    {
                        int arrayLength = consumeInt(bytes);
                        long[] array = new long[arrayLength];
                        for(int j = 0; j != arrayLength; ++j)
                        {
                            array[j] = consumeLong(bytes);
                        }

                        parameters.SetLongArray(entryName, array);
                    }
                    break;
                case Parameters.EntryType.DOUBLE_ARRAY:
                    {
                        int arrayLength = consumeInt(bytes);
                        double[] array = new double[arrayLength];
                        for(int j = 0; j != arrayLength; ++j)
                        {
                            array[j] = consumeDouble(bytes);
                        }

                        parameters.SetDoubleArray(entryName, array);
                    }
                    break;
                case Parameters.EntryType.STRING_ARRAY:
                    {
                        int arrayLength = consumeInt(bytes);
                        string[] array = new string[arrayLength];
                        for(int j = 0; j != arrayLength; ++j)
                        {
                            array[j] = consumeString(bytes);
                        }

                        parameters.SetStringArray(entryName, array);
                    }
                    break;
                case Parameters.EntryType.BINARY_ARRAY:
                    {
                        int arrayLength = consumeInt(bytes);
                        byte[][] array = new byte[arrayLength][];
                        for(int j = 0; j != arrayLength; ++j)
                        {
                            array[j] = consumeBinary(bytes);
                        }

                        parameters.SetBinaryArray(entryName, array);
                    }
                    break;
                case Parameters.EntryType.NESTED_PARAMETERS:
                    {
                        Parameters nested = new Parameters();
                        deserializeFromBytes(nested, bytes);
                        parameters.SetNestedParameters(entryName, nested);
                    }
                    break;
                }
            }
        }
Exemplo n.º 12
0
 // reads a string object
 private static string consumeString(ByteIterator bytes)
 {
     int length = consumeInt(bytes);
     byte[] buf = consumeBytes(bytes, length);
     try
     {
         return encoding.GetString(buf);
     }
     catch(DecoderFallbackException e)
     {
         throw new UnexpectedValueException(e.Message);
     }
 }
Exemplo n.º 13
0
 // reads long value from buffers
 private static long consumeLong(ByteIterator bytes)
 {
     long lower = consumeInt(bytes);
     long lowerMasked = (lower & 0x00000000ffffffffL);
     return lowerMasked + ((long)consumeInt(bytes) << 32);
 }
Exemplo n.º 14
0
 // comsumes integer value from buffers
 private static int consumeInt(ByteIterator bytes)
 {
     byte[] buf = {
     bytes.next(),
     bytes.next(),
     bytes.next(),
     bytes.next()
     };
     return readInt(buf, 0);
 }
Exemplo n.º 15
0
 // reads double value from the buffer with given endianness
 private static double consumeDouble(ByteIterator bytes)
 {
     long longBits = consumeLong(bytes);
     return BitConverter.Int64BitsToDouble(longBits);
 }
Exemplo n.º 16
0
        private static byte[] Divide(byte[] numerator, byte[] denominator, out byte[] remainder)
        {
            if (denominator == null)
            {
                throw new DivideByZeroException();
            }

            var comparison = Compare(numerator, denominator);

            if (comparison < 0)
            {
                remainder = numerator;
                return(null);
            }
            else if (comparison == 0)
            {
                remainder = null;
                return(new[] { (byte)1 });
            }
            else
            {
                var result   = new List <byte>();
                var iterator = new ByteIterator(numerator.Reverse());
                IEnumerable <byte> subnumeratorDigits = new byte[0];

                while (iterator.TryNext(1, out var subdigit))
                {
                    subnumeratorDigits = subdigit.Concat(subnumeratorDigits);
                    var subnumerator = subnumeratorDigits.ToArray();

                    if (Compare(denominator, subnumerator) > 0)
                    {
                        result.Add(0);
                        continue;
                    }

                    //How many times can we increase the denominator till we are greater than the numerator
                    byte[] previous, accumulator = denominator;
                    int    compare, cnt = 0;
                    do
                    {
                        cnt++;
                        previous    = accumulator;
                        accumulator = Multiply(denominator, GetDigits(cnt).ToArray());
                    }while ((compare = Compare(accumulator, subnumerator)) < 0);

                    //add the quotient digits
                    if (compare == 0)
                    {
                        result.AddRange(GetDigits(cnt));
                        subnumeratorDigits = new byte[0];
                    }
                    else                     //if(compare > 0)
                    {
                        result.AddRange(GetDigits(cnt - 1));
                        subnumeratorDigits = Subtract(subnumerator, previous);
                    }
                }

                remainder = subnumeratorDigits.ToArray();

                return(StripLeadingZeros(result.AsEnumerable().Reverse().ToArray()));
            }
        }
Exemplo n.º 17
0
 // reads a binary object
 private static byte[] consumeBinary(ByteIterator bytes)
 {
     int length = consumeInt(bytes);
     byte[] buf = consumeBytes(bytes, length);
     return buf;
 }