예제 #1
0
  @Nullable
  public <T> T test(Object source, @NotNull Class<T> destinationType) {

    final BinaryWire wire = new BinaryWire(Bytes.elasticByteBuffer());

    if (source instanceof String) wire.writeValue().text((String) source);
    else if (source instanceof Long) wire.writeValue().int64((Long) source);
    else if (source instanceof Integer) wire.writeValue().int32((Integer) source);
    else if (source instanceof Short) wire.writeValue().int16((Short) source);
    else if (source instanceof Byte) wire.writeValue().int8((Byte) source);
    else if (source instanceof Float) wire.writeValue().float32((Float) source);
    else if (source instanceof Double) wire.writeValue().float64((Double) source);

    if (String.class.isAssignableFrom(destinationType)) return (T) wire.getValueIn().text();

    if (Long.class.isAssignableFrom(destinationType)) return (T) (Long) wire.getValueIn().int64();

    if (Integer.class.isAssignableFrom(destinationType))
      return (T) (Integer) wire.getValueIn().int32();

    if (Short.class.isAssignableFrom(destinationType)) return (T) (Short) wire.getValueIn().int16();

    if (Byte.class.isAssignableFrom(destinationType)) return (T) (Byte) wire.getValueIn().int8();

    if (Float.class.isAssignableFrom(destinationType))
      return (T) (Float) wire.getValueIn().float32();

    if (Double.class.isAssignableFrom(destinationType))
      return (T) (Double) wire.getValueIn().float64();

    throw new UnsupportedOperationException("");
  }
 @Test
 public void testCreateReadAnyFirstFIELDLESS_BINARYWire() throws Exception {
   final Bytes<ByteBuffer> bytes = Bytes.elasticByteBuffer();
   final String expected = "world";
   FIELDLESS_BINARY.apply(bytes).write((() -> "hello")).text(expected);
   Assert.assertEquals(expected, READ_ANY.apply(bytes).read((() -> "hello")).text());
 }
 @Test
 public void testReadAny() throws Exception {
   final Bytes<ByteBuffer> t = Bytes.elasticByteBuffer();
   final Wire wire = TEXT.apply(t);
   wire.write((() -> "hello")).text("world");
   Assert.assertEquals("world", READ_ANY.apply(t).read(() -> "hello").text());
 }
  @Test
  public void testConverMarshallableToTextName() {

    TestMarshallable testMarshallable = new TestMarshallable();
    testMarshallable.setName("hello world");

    Bytes<ByteBuffer> byteBufferBytes = Bytes.elasticByteBuffer();

    ByteBuffer byteBuffer = byteBufferBytes.underlyingObject();
    System.out.println(byteBuffer.getClass());

    Wire textWire = new TextWire(byteBufferBytes);
    textWire.bytes().readPosition();

    textWire.writeDocument(false, d -> d.write(() -> "any-key").marshallable(testMarshallable));

    String value = Wires.fromSizePrefixedBlobs(textWire.bytes());

    // String replace = value.replace("\n", "\\n");

    System.out.println(byteBufferBytes.toHexString());
    System.out.println(value);

    //  Assert.assertTrue(replace.length() > 1);
  }
  /** see WIRE-37 issue when using numbers as keys in binary wire */
  @Test
  public void testMarshall() {

    Bytes bytes = Bytes.elasticByteBuffer();
    Wire wire = new BinaryWire(bytes);

    MyMarshallable x = new MyMarshallable();
    x.text.append("text");

    wire.write(() -> "key").typedMarshallable(x);

    final ValueIn read = wire.read(() -> "key");
    final MyMarshallable result = read.typedMarshallable();

    System.out.println(result.toString());

    Assert.assertEquals("text", result.text.toString());
  }
예제 #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);
    }
  }