readValueOfType method

  1. @override
dynamic readValueOfType(
  1. int type,
  2. ReadBuffer buffer
)
override

Reads a value of the indicated type from buffer.

The codec can be extended by overriding this method, calling super for types that the extension does not handle. See the discussion at writeValue.

Implementation

@override
dynamic readValueOfType(int type, ReadBuffer buffer) {
  switch (type) {
    case _valueNull:
      return null;
    case _valueTrue:
      return true;
    case _valueFalse:
      return false;
    case _valueInt32:
      return buffer.getInt32();
    case _valueInt64:
      return buffer.getInt64();
    case _valueFloat64:
      return buffer.getFloat64();
    case _valueLargeInt:
    case _valueString:
      final int length = readSize(buffer);
      return utf8.decoder.convert(buffer.getUint8List(length));
    case _valueUint8List:
      final int length = readSize(buffer);
      return buffer.getUint8List(length);
    case _valueInt32List:
      final int length = readSize(buffer);
      return buffer.getInt32List(length);
    case _valueInt64List:
      final int length = readSize(buffer);
      return buffer.getInt64List(length);
    case _valueFloat64List:
      final int length = readSize(buffer);
      return buffer.getFloat64List(length);
    case _valueArray:
      final int length = readSize(buffer);
      final dynamic result = List<dynamic>(length);
      for (int i = 0; i < length; i++) {
        result[i] = readValue(buffer);
      }
      return result;
    case _valueList:
      final int length = readSize(buffer);
      final dynamic result = List<dynamic>(length);
      for (int i = 0; i < length; i++) {
        result[i] = readValue(buffer);
      }
      return result;
    case _valueMap:
      final int length = readSize(buffer);
      final dynamic result = <dynamic, dynamic>{};
      for (int i = 0; i < length; i++) {
        result[readValue(buffer)] = readValue(buffer);
      }
      return result;
    case _valueEnum:
      return buffer.getInt32();
    case _valueRef:
      final int length = readSize(buffer);
      final String refId = utf8.decoder.convert(buffer.getUint8List(length));

      if (refId == null) return null;

      Ref ref = Ref()
        ..refId = refId
        ..tag__ = tag;
      if (Platform.isAndroid && androidCaster != null) {
        ref = androidCaster(ref, refId.split(':')[0]);
      } else if (Platform.isIOS && iosCaster != null) {
        ref = iosCaster(ref, refId.split(':')[0]);
      }

      // 如果有ScopedReleasePool, 则使用ScopedReleasePool里的释放池
      // 否则使用全局的释放池
      if (gReleasePoolStack.peek() != null) {
        log('添加对象 $ref 到局部释放池');
        gReleasePoolStack.peek().add(ref);
      } else {
        log('添加对象 $ref 到全局释放池');
        gGlobalReleasePool.add(ref);
      }
      return ref;
    default:
      throw const FormatException('Message corrupted');
  }
}