コード例 #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
  public static void rawReadData(@NotNull WireIn wireIn, @NotNull ReadMarshallable dataConsumer) {
    final Bytes<?> bytes = wireIn.bytes();
    int header = bytes.readInt();
    assert Wires.isReady(header) && Wires.isData(header);
    final int len = Wires.lengthOf(header);

    long limit0 = bytes.readLimit();
    long limit = bytes.readPosition() + (long) len;
    try {
      bytes.readLimit(limit);
      dataConsumer.readMarshallable(wireIn);
    } finally {
      bytes.readLimit(limit0);
    }
  }
コード例 #3
0
  public static boolean readData(
      @NotNull WireIn wireIn,
      @Nullable ReadMarshallable metaDataConsumer,
      @Nullable ReadMarshallable dataConsumer) {
    final Bytes<?> bytes = wireIn.bytes();
    boolean read = false;
    while (bytes.readRemaining() >= 4) {
      long position = bytes.readPosition();
      int header = bytes.readVolatileInt(position);
      if (!isKnownLength(header)) return read;
      bytes.readSkip(4);
      final boolean ready = Wires.isReady(header);
      final int len = Wires.lengthOf(header);
      if (Wires.isData(header)) {
        if (dataConsumer == null) {
          return false;

        } else {
          ((InternalWireIn) wireIn).setReady(ready);
          bytes.readWithLength(len, b -> dataConsumer.readMarshallable(wireIn));
          return true;
        }
      } else {

        if (metaDataConsumer == null) {
          // skip the header
          bytes.readSkip(len);
        } else {
          // bytes.readWithLength(len, b -> metaDataConsumer.accept(wireIn));
          // inlined to avoid garbage
          if ((long) len > bytes.readRemaining()) throw new BufferUnderflowException();
          long limit0 = bytes.readLimit();
          long limit = bytes.readPosition() + (long) len;
          try {
            bytes.readLimit(limit);
            metaDataConsumer.readMarshallable(wireIn);
          } finally {
            bytes.readLimit(limit0);
            bytes.readPosition(limit);
          }
        }

        if (dataConsumer == null) return true;
        read = true;
      }
    }
    return read;
  }
コード例 #4
0
  public static boolean readData(
      long offset,
      @NotNull WireIn wireIn,
      @Nullable ReadMarshallable metaDataConsumer,
      @Nullable ReadMarshallable dataConsumer) {
    final Bytes bytes = wireIn.bytes();

    long position = bytes.readPosition();
    long limit = bytes.readLimit();
    try {
      bytes.readLimit(bytes.isElastic() ? bytes.capacity() : bytes.realCapacity());
      bytes.readPosition(offset);
      return readData(wireIn, metaDataConsumer, dataConsumer);
    } finally {
      bytes.readLimit(limit);
      bytes.readPosition(position);
    }
  }
コード例 #5
0
ファイル: Bytes.java プロジェクト: bowlofstew/Chronicle-Bytes
  /**
   * The buffer is not modified by this call
   *
   * @param buffer the buffer to use
   * @param position the position to create the string from
   * @param len the number of characters to show in the string
   * @return a string contain the text from offset {@code position}
   */
  static String toString(@NotNull final Bytes buffer, long position, long len)
      throws BufferUnderflowException, IORuntimeException {
    final long pos = buffer.readPosition();
    final long limit = buffer.readLimit();
    buffer.readPosition(position);
    buffer.readLimit(position + len);

    try {

      final StringBuilder builder = new StringBuilder();
      while (buffer.readRemaining() > 0) {
        builder.append((char) buffer.readByte());
      }

      // remove the last comma
      return builder.toString();
    } finally {
      buffer.readLimit(limit);
      buffer.readPosition(pos);
    }
  }
コード例 #6
0
  @NotNull
  static String fromSizePrefixedBlobs(@NotNull Bytes bytes, long position, long length) {
    StringBuilder sb = new StringBuilder();

    final long limit0 = bytes.readLimit();
    final long position0 = bytes.readPosition();
    try {
      bytes.readPosition(position);
      long limit2 = Math.min(limit0, position + length);
      bytes.readLimit(limit2);
      long missing = position + length - limit2;
      while (bytes.readRemaining() >= 4) {
        long header = bytes.readUnsignedInt();
        int len = Wires.lengthOf(header);
        if (len > bytes.readRemaining())
          throw new RuntimeException(
              "Are you sure this was written with writeDocument and has a 4 byte size prefix, "
                  + len
                  + " > "
                  + bytes.readRemaining());
        String type =
            Wires.isData(header)
                ? Wires.isReady(header) ? "!!data" : "!!not-ready-data!"
                : Wires.isReady(header) ? "!!meta-data" : "!!not-ready-meta-data!";
        boolean binary = bytes.readByte(bytes.readPosition()) < ' ';

        sb.append("--- ").append(type).append(binary ? " #binary" : "");
        if (missing > 0) sb.append(" # missing: ").append(missing);
        if (len > bytes.readRemaining())
          sb.append(" # len: ").append(len).append(", remaining: ").append(bytes.readRemaining());
        sb.append("\n");

        Bytes textBytes = bytes;

        if (binary) {
          Bytes bytes2 = Bytes.elasticByteBuffer();
          TextWire textWire = new TextWire(bytes2);
          long readLimit = bytes.readLimit();

          try {
            bytes.readLimit(bytes.readPosition() + len);
            new BinaryWire(bytes).copyTo(textWire);
          } finally {
            bytes.readLimit(readLimit);
          }
          textBytes = bytes2;
          len = (int) textBytes.readRemaining();
        }
        try {
          for (int i = 0; i < len; i++) {
            int ch = textBytes.readUnsignedByte();
            sb.append((char) ch);
          }
        } catch (Exception e) {
          sb.append(" ").append(e);
        }
        if (sb.charAt(sb.length() - 1) != '\n') sb.append('\n');
      }

      return sb.toString();
    } finally {
      bytes.readLimit(limit0);
      bytes.readPosition(position0);
    }
  }
コード例 #7
0
 @Stage("Segment")
 public Bytes segmentBytesForRead() {
   segmentBytes.readLimit(segmentBytes.capacity());
   return segmentBytes;
 }