Example usage for java.nio ByteBuffer put

List of usage examples for java.nio ByteBuffer put

Introduction

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

Prototype

public ByteBuffer put(ByteBuffer src) 

Source Link

Document

Writes all the remaining bytes of the src byte buffer to this buffer's current position, and increases both buffers' position by the number of bytes copied.

Usage

From source file:com.jadarstudios.rankcapes.bukkit.network.packet.S0PacketPlayerCapesUpdate.java

@Override
public void write(ByteBuffer data) throws BufferOverflowException, ReadOnlyBufferException {
    data.put((byte) this.type.ordinal());

    String players = JSONValue.toJSONString(this.playersMap);
    writeString(players, data);/* ww w  .  j av a2s. co  m*/
}

From source file:Main.java

public static byte[] aesIGEencrypt(byte[] tmpAESiv, byte[] tmpAesKey, byte[] data) {
    try {/*  w w w. j  ava 2  s  .  c  om*/

        ByteBuffer out = ByteBuffer.allocate(data.length);

        byte[] ivp = Arrays.copyOfRange(tmpAESiv, 0, tmpAESiv.length / 2);
        byte[] iv2p = Arrays.copyOfRange(tmpAESiv, tmpAESiv.length / 2, tmpAESiv.length);

        int len = data.length / AES_BLOCK_SIZE;

        byte[] xorInput = null;
        byte[] xorOutput = null;

        SecretKeySpec keySpec = null;
        keySpec = new SecretKeySpec(tmpAesKey, "AES");
        Cipher cipher = null;
        cipher = Cipher.getInstance("AES/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);

        byte[] input = null;
        byte[] output = null;

        for (int i = 0; i < len; i++) {

            input = Arrays.copyOfRange(data, i * AES_BLOCK_SIZE, (i + 1) * AES_BLOCK_SIZE);
            xorInput = xor(input, ivp);
            output = cipher.doFinal(xorInput);
            xorOutput = xor(output, iv2p);
            out.put(xorOutput);

            ivp = xorOutput;
            iv2p = input;
        }
        return out.array();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:backtype.storm.messaging.TaskMessage.java

public ByteBuffer serialize() {
    ByteBuffer bb = ByteBuffer.allocate(_message.length + 4);
    bb.putShort((short) _task);
    bb.putShort(remoteTuple);/*ww w  . jav  a 2  s .c om*/
    bb.put(_message);
    return bb;
}

From source file:eu.pursuit.core.ScopeID.java

public void fill(ByteBuffer buffer) {
    for (ByteIdentifier id : list) {
        buffer.put(id.getId());
    }
}

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();//from  www  . j  a va  2 s.  c o m
    staging.writePrimaryStaging(buffer, "Hello World".getBytes().length);

    buffer.clear();
    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:de.rwhq.serializer.FixedStringSerializer.java

@Override
public byte[] serialize(final String o) {
    final byte[] bytes = o.getBytes();

    if (bytes.length > (length - 1)) {
        throw new IllegalArgumentException("String is too long to be serialized");
    }/*from  www  . jav  a 2  s.  c  om*/

    final ByteBuffer buf = ByteBuffer.allocate(length);
    buf.putShort((short) bytes.length);
    buf.put(bytes);
    return buf.array();
}

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();/*  w w  w  .j  a v  a 2s  .co  m*/
    staging.writeTempReadBytes(buffer);
    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();//  w  w w.ja  va  2  s .  c om
    staging.writeTempWriteBytes(buffer);
    Assert.assertTrue(staging.hasTempWriteBytes());

    buffer.clear();
    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:com.github.neoio.net.message.staging.file.TestFileMessageStaging.java

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

    buffer.put("Hello World".getBytes("US-ASCII"));
    buffer.flip();/*from w  w  w.  ja  va 2s  . co  m*/
    Assert.assertEquals(11, staging.writeTempReadBytes(buffer));
    Assert.assertTrue(staging.hasTempReadBytes());
    buffer.clear();
    Assert.assertEquals(11, staging.readTempReadBytes(buffer));
    buffer.put("Hello World".getBytes("US-ASCII"));
    buffer.flip();
    staging.writePrimaryStaging(buffer, 22);
    buffer.clear();
    staging.getPrimaryStage().read(buffer);
    Assert.assertEquals("Hello WorldHello World", new String(buffer.array(), 0, 22));
}

From source file:org.wso2.carbon.http2.transport.util.HTTP2Decoder.java

@Override
public int read(ByteBuffer dst) throws IOException {
    complete = false;//w w w. j  a va 2s  .  c om
    byte[] data = ByteBufUtil.getBytes(dataFrame.content());
    dst.put(data);
    complete = true;
    return data.length;
}