Example usage for java.nio ByteBuffer flip

List of usage examples for java.nio ByteBuffer flip

Introduction

In this page you can find the example usage for java.nio ByteBuffer flip.

Prototype

public final Buffer flip() 

Source Link

Document

Flips this buffer.

Usage

From source file:org.apache.hadoop.ipc.ServerRpcSSLEngineImpl.java

private ByteBuffer enlargeUnwrappedBuffer(ByteBuffer buffer, byte missedByte) {
    buffer.flip();
    ByteBuffer newBuffer = ByteBuffer.allocate(Math.min(buffer.capacity() * 2, maxUnWrappedDataLength));
    newBuffer.put(buffer);//w ww  .  ja va  2  s. c  o  m
    newBuffer.put(missedByte);
    buffer = null;
    return newBuffer;
}

From source file:org.cloudfoundry.caldecott.server.controller.TunnelController.java

@RequestMapping(value = "/{id}/out/{seq}", method = RequestMethod.GET)
@ResponseBody/*from   ww w .j a  v  a  2s  .c  o m*/
public ByteBuffer read(@PathVariable TunnelId id, @PathVariable int seq) {
    // FIXME async
    // Tunnel tunnel = this.tunnels.get(id);
    // BlockingCallback<ByteBuffer> callback = BlockingCallback.forClass(ByteBuffer.class);
    // tunnel.read(seq, callback);
    // return callback.get();
    ByteBuffer buffer = ByteBuffer.allocateDirect(100);
    buffer.put("hello".getBytes());
    buffer.flip();
    return buffer;
}

From source file:org.apache.kylin.measure.extendedcolumn.ExtendedColumnSerializerTest.java

@Test
public void testOverflow() {
    String text = StringUtils.repeat("h", 21);
    ExtendedColumnSerializer serializer = new ExtendedColumnSerializer(DataType.getType("extendedcolumn(20)"));
    MeasureIngester<ByteArray> ingester = measureType.newIngester();
    ByteArray array = ingester.valueOf(new String[] { null, text }, null, null);

    ByteBuffer buffer = ByteBuffer.allocate(serializer.maxLength());
    serializer.serialize(array, buffer);
    buffer.flip();
    ByteArray des = serializer.deserialize(buffer);
    Assert.assertTrue(new ByteArray(StringUtils.repeat("h", 20).getBytes()).equals(des));
}

From source file:org.apache.kylin.measure.extendedcolumn.ExtendedColumnSerializerTest.java

@Test
public void testNormal() {
    String text = StringUtils.repeat("h", 20);

    ExtendedColumnSerializer serializer = new ExtendedColumnSerializer(DataType.getType("extendedcolumn(20)"));
    MeasureIngester<ByteArray> ingester = measureType.newIngester();
    ByteArray array = ingester.valueOf(new String[] { null, text }, null, null);

    ByteBuffer buffer = ByteBuffer.allocate(serializer.maxLength());
    serializer.serialize(array, buffer);
    buffer.flip();
    ByteArray des = serializer.deserialize(buffer);
    Assert.assertTrue(new ByteArray(text.getBytes()).equals(des));
}

From source file:org.apache.kylin.measure.extendedcolumn.ExtendedColumnSerializerTest.java

@Test
public void testSerDesNull() {
    ExtendedColumnSerializer serializer = new ExtendedColumnSerializer(DataType.getType("extendedcolumn(20)"));
    MeasureIngester<ByteArray> ingester = measureType.newIngester();
    ByteArray array = ingester.valueOf(new String[] { null, null }, null, null);
    Assert.assertTrue(new ByteArray().equals(array));

    ByteBuffer buffer = ByteBuffer.allocate(serializer.maxLength());
    serializer.serialize(array, buffer);
    buffer.flip();
    int length = serializer.peekLength(buffer);
    Assert.assertTrue(length == 1);/*w  ww  .j ava2s  . c o m*/
    ByteArray des = serializer.deserialize(buffer);
    Assert.assertTrue(new ByteArray().equals(des));
}

From source file:MainClass.java

public void run() {

    ByteBuffer sizeb = ByteBuffer.allocate(4);
    try {/*from   w w w.j a  va2  s  . co  m*/
        while (sizeb.hasRemaining())
            in.read(sizeb);
        sizeb.flip();
        int howMany = sizeb.getInt();
        sizeb.clear();

        for (int i = 0; i < howMany; i++) {
            while (sizeb.hasRemaining())
                in.read(sizeb);
            sizeb.flip();
            int length = sizeb.getInt();
            sizeb.clear();

            ByteBuffer data = ByteBuffer.allocate(length);
            while (data.hasRemaining())
                in.read(data);

            BigInteger result = new BigInteger(data.array());
            System.out.println(result);
        }
    } catch (IOException ex) {
        System.err.println(ex);
    } finally {
        try {
            in.close();
        } catch (Exception ex) {
            // We tried
        }
    }
}

From source file:com.github.neoio.net.message.staging.file.TestFileMessageStaging.java

@Test
public void test_primaryStage() throws Exception {
    ByteBuffer buffer = ByteBuffer.allocate(1024);

    buffer.put("Hello World".getBytes());
    buffer.flip();
    staging.writePrimaryStaging(buffer, "Hello World".getBytes().length);

    buffer.clear();//from w  w  w .  j  a va2s .  c  o m
    staging.getPrimaryStage().read(buffer);
    Assert.assertEquals("Hello World".getBytes().length, staging.getPrimaryStageSize());
    Assert.assertEquals("Hello World", new String(buffer.array(), 0, buffer.position()));

    staging.resetPrimaryStage();
    Assert.assertEquals(0, staging.getPrimaryStageSize());
}

From source file:com.github.neoio.net.message.staging.file.TestFileMessageStaging.java

@Test
public void test_tempRead() {
    ByteBuffer buffer = ByteBuffer.allocate(1024);

    buffer.put("Hello World".getBytes());
    buffer.flip();
    staging.writeTempReadBytes(buffer);/*from w  ww  . j  a v a 2s .  c  o  m*/
    Assert.assertTrue(staging.hasTempReadBytes());

    buffer.clear();
    staging.readTempReadBytes(buffer);
    Assert.assertEquals("Hello World",
            new String(ArrayUtils.subarray(buffer.array(), 0, "Hello World".getBytes().length)));
    staging.resetTempReadBytes();

    Assert.assertFalse(staging.hasTempReadBytes());
}

From source file:com.github.neoio.net.message.staging.file.TestFileMessageStaging.java

@Test
public void test_tempWrite() {
    ByteBuffer buffer = ByteBuffer.allocate(1024);

    buffer.put("Hello World".getBytes());
    buffer.flip();
    staging.writeTempWriteBytes(buffer);
    Assert.assertTrue(staging.hasTempWriteBytes());

    buffer.clear();/*from  w  ww  .  j ava 2s.com*/
    staging.readTempWriteBytes(buffer);
    Assert.assertEquals("Hello World",
            new String(ArrayUtils.subarray(buffer.array(), 0, "Hello World".getBytes().length)));
    staging.resetTempWriteBytes();

    Assert.assertFalse(staging.hasTempWriteBytes());
}

From source file:io.druid.segment.writeout.WriteOutBytesTest.java

private void verifyContents(WriteOutBytes writeOutBytes, String expected) throws IOException {
    Assert.assertEquals(expected, IOUtils.toString(writeOutBytes.asInputStream(), StandardCharsets.US_ASCII));
    ByteBuffer bb = ByteBuffer.allocate((int) writeOutBytes.size());
    writeOutBytes.readFully(0, bb);/*from ww w .  j a  v  a  2s  .c o m*/
    bb.flip();
    Assert.assertEquals(expected, StringUtils.fromUtf8(bb));
}