Package com.esotericsoftware.kryo.io

Examples of com.esotericsoftware.kryo.io.UnsafeMemoryOutput


    final public void write (Output output, Object object) {
      if (output instanceof UnsafeOutput) {
        UnsafeOutput unsafeOutput = (UnsafeOutput)output;
        unsafeOutput.writeBytes(object, offset, len);
      } else if (output instanceof UnsafeMemoryOutput) {
        UnsafeMemoryOutput unsafeOutput = (UnsafeMemoryOutput)output;
        unsafeOutput.writeBytes(object, offset, len);
      } else {
        long off;
        Unsafe unsafe = unsafe();
        for (off = offset; off < offset + len - 8; off += 8) {
          output.writeLong(unsafe.getLong(object, off));
View Full Code Here


      ByteBufferInput inputBuffer = new ByteBufferInput(outputBuffer.getByteBuffer());
      inputBuffer.readInt();
      inputBuffer.release();

      outputBuffer.release();
      outputBuffer = new UnsafeMemoryOutput(bufAddress, 4096);
      outputBuffer.writeInt(10);

      inputBuffer = new UnsafeMemoryInput(outputBuffer.getByteBuffer());
      inputBuffer.readInt();
      inputBuffer.release();
View Full Code Here

    }
  }

  public void testOutputStream () throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    UnsafeMemoryOutput output = new UnsafeMemoryOutput(buffer, 2);
    output.writeBytes(new byte[] {11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26});
    output.writeBytes(new byte[] {31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46});
    output.writeBytes(new byte[] {51, 52, 53, 54, 55, 56, 57, 58});
    output.writeBytes(new byte[] {61, 62, 63, 64, 65});
    output.flush();

    assertEquals(new byte[] { //
      11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, //
        31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, //
        51, 52, 53, 54, 55, 56, 57, 58, //
View Full Code Here

    System.arraycopy(temp, 512, temp2, 0, count);
    assertEquals(bytes, temp2);
  }

  public void testWriteBytes () throws IOException {
    UnsafeMemoryOutput buffer = new UnsafeMemoryOutput(512);
    buffer.writeBytes(new byte[] {11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26});
    buffer.writeBytes(new byte[] {31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46});
    buffer.writeByte(51);
    buffer.writeBytes(new byte[] {52, 53, 54, 55, 56, 57, 58});
    buffer.writeByte(61);
    buffer.writeByte(62);
    buffer.writeByte(63);
    buffer.writeByte(64);
    buffer.writeByte(65);
    buffer.flush();

    assertEquals(new byte[] { //
      11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, //
        31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, //
        51, 52, 53, 54, 55, 56, 57, 58, //
        61, 62, 63, 64, 65}, buffer.toBytes());
  }
View Full Code Here

        51, 52, 53, 54, 55, 56, 57, 58, //
        61, 62, 63, 64, 65}, buffer.toBytes());
  }

  public void testStrings () throws IOException {
    runStringTest(new UnsafeMemoryOutput(4096));
    runStringTest(new UnsafeMemoryOutput(897));
    runStringTest(new UnsafeMemoryOutput(new ByteArrayOutputStream()));

    UnsafeMemoryOutput write = new UnsafeMemoryOutput(21);
    String value = "abcdef\u00E1\u00E9\u00ED\u00F3\u00FA\u1234";
    write.writeString(value);
    Input read = new UnsafeMemoryInput(write.toBytes());
    assertEquals(value, read.readString());

    runStringTest(127);
    runStringTest(256);
    runStringTest(1024 * 1023);
 
View Full Code Here

    runStringTest(1024 * 1026);
    runStringTest(1024 * 1024 * 2);
  }

  public void runStringTest (int length) throws IOException {
    UnsafeMemoryOutput write = new UnsafeMemoryOutput(1024, -1);
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < length; i++)
      buffer.append((char)i);

    String value = buffer.toString();
    write.writeString(value);
    write.writeString(value);
    Input read = new UnsafeMemoryInput(write.toBytes());
    assertEquals(value, read.readString());
    assertEquals(value, read.readStringBuilder().toString());

    write.clear();
    write.writeString(buffer);
    write.writeString(buffer);
    read = new UnsafeMemoryInput(write.toBytes());
    assertEquals(value, read.readStringBuilder().toString());
    assertEquals(value, read.readString());

    if (length <= 127) {
      write.clear();
      write.writeAscii(value);
      write.writeAscii(value);
      read = new UnsafeMemoryInput(write.toBytes());
      assertEquals(value, read.readStringBuilder().toString());
      assertEquals(value, read.readString());
    }
  }
View Full Code Here

    for (int i = 0; i < 127; i++)
      assertEquals(String.valueOf((char)i) + "abc", read.readStringBuilder().toString());
  }

  public void testCanReadInt () throws IOException {
    UnsafeMemoryOutput write = new UnsafeMemoryOutput(new ByteArrayOutputStream());

    Input read = new UnsafeMemoryInput(write.toBytes());
    assertEquals(false, read.canReadInt());

    write.writeVarInt(400, true);

    read = new UnsafeMemoryInput(write.toBytes());
    assertEquals(true, read.canReadInt());
    read.setLimit(read.limit() - 1);
    assertEquals(false, read.canReadInt());
  }
View Full Code Here

    read.setLimit(read.limit() - 1);
    assertEquals(false, read.canReadInt());
  }

  public void testInts () throws IOException {
    runIntTest(new UnsafeMemoryOutput(4096));
    runIntTest(new UnsafeMemoryOutput(new ByteArrayOutputStream()));
  }
View Full Code Here

      assertEquals(value, read.readInt(false));
    }
  }

  public void testLongs () throws IOException {
    runLongTest(new UnsafeMemoryOutput(4096));
    runLongTest(new UnsafeMemoryOutput(new ByteArrayOutputStream()));
  }
View Full Code Here

      assertEquals("Element " + i, value, read.readLong(false));
    }
  }

  public void testShorts () throws IOException {
    runShortTest(new UnsafeMemoryOutput(4096));
    runShortTest(new UnsafeMemoryOutput(new ByteArrayOutputStream()));
  }
View Full Code Here

TOP

Related Classes of com.esotericsoftware.kryo.io.UnsafeMemoryOutput

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.