// converts from entry type to type code private static int typeToCode(Parameters.EntryType type) { int code = 0; switch (type) { case Parameters.EntryType.BOOLEAN: code = 1; break; case Parameters.EntryType.INTEGER: code = 2; break; case Parameters.EntryType.LONG: code = 3; break; case Parameters.EntryType.DOUBLE: code = 4; break; case Parameters.EntryType.STRING: code = 5; break; case Parameters.EntryType.BINARY: code = 6; break; case Parameters.EntryType.BOOLEAN_ARRAY: code = 7; break; case Parameters.EntryType.INTEGER_ARRAY: code = 8; break; case Parameters.EntryType.LONG_ARRAY: code = 9; break; case Parameters.EntryType.DOUBLE_ARRAY: code = 10; break; case Parameters.EntryType.STRING_ARRAY: code = 11; break; case Parameters.EntryType.BINARY_ARRAY: code = 12; break; case Parameters.EntryType.NESTED_PARAMETERS: code = 13; break; case Parameters.EntryType.NESTED_PARAMETERS_ARRAY: code = 14; break; } return(code); }
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; } } }