コード例 #1
0
  @NotNull
  @Override
  public CharSequence read(Bytes bytes, @Nullable CharSequence ignored) {
    long size = bytes.readStopBit();
    if (size == NULL_LENGTH) return null;
    if (size < 0 || size > Integer.MAX_VALUE)
      throw new IllegalStateException("Invalid length: " + size);
    if (size == 0) return "";

    char[] chars = new char[(int) size];
    int length = bytes.readInt();
    long limit = bytes.readLimit();
    try {
      bytes.readLimit(bytes.readPosition() + length);
      DataInputStream dis =
          new DataInputStream(
              new BufferedInputStream(new InflaterInputStream(bytes.inputStream())));
      for (int i = 0; i < size; i++) chars[i] = (char) (dis.readByte() & 0xff);

    } catch (IOException e) {
      throw new IORuntimeException(e);
    } finally {
      bytes.readPosition(bytes.readLimit());
      bytes.readLimit(limit);
    }
    try {
      return STRING_FACTORY.fromChars(chars);
    } catch (Exception e) {
      throw new AssertionError(e);
    }
  }
コード例 #2
0
 @NotNull
 @Override
 public A read(Bytes in, A using) {
   if (using == null) using = new A();
   using.str_ = in.readUtf8();
   int size = (int) in.readStopBit();
   if (size >= 0) {
     if (using.list_ == null) {
       using.list_ = new ArrayList<>(size);
     } else {
       using.list_.clear();
       if (using.list_ instanceof ArrayList) ((ArrayList) using.list_).ensureCapacity(size);
     }
     for (int i = 0; i < size; i++) {
       B b = new B();
       b.readMarshallable(in);
       using.list_.add(b);
     }
   } else {
     assert size == -1;
     using.list_ = null;
   }
   return using;
 }