Example usage for java.nio ByteBuffer slice

List of usage examples for java.nio ByteBuffer slice

Introduction

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

Prototype

public abstract ByteBuffer slice();

Source Link

Document

Returns a sliced buffer that shares its content with this buffer.

Usage

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * buffer?/*from ww w  . j  a  v  a 2 s.co m*/
 * 
 * @param buffer
 * @return
 */
public static byte[] toArray(ByteBuffer buffer) {
    // ?heap buffer
    if (buffer.hasArray()) {
        byte[] array = buffer.array();
        int from = buffer.arrayOffset() + buffer.position();
        return Arrays.copyOfRange(array, from, from + buffer.remaining());
    }
    //  direct buffer
    else {
        byte[] to = new byte[buffer.remaining()];
        buffer.slice().get(to);
        return to;
    }
}

From source file:com.openteach.diamond.network.waverider.network.Packet.java

/**
 * ByteBuffer??/*w  w w.jav  a  2  s  .co m*/
 * @param buffer
 * @return
 */
public static Packet unmarshall(ByteBuffer buffer) {
    if (buffer.remaining() < getHeaderSize()) {
        throw new RuntimeException("Wrong packet.");
    }

    Packet packet = new Packet();
    byte[] str = new byte[NetWorkConstants.WAVERIDER_MAGIC.getBytes().length];
    buffer.get(str);
    packet.setMagic(new String(str));

    if (!NetWorkConstants.WAVERIDER_MAGIC.equals(packet.getMagic())) {
        throw new RuntimeException("Wrong packet.");
    }

    packet.setSequence(buffer.getLong());
    packet.setType(buffer.getLong());
    packet.setLength(buffer.getInt());
    packet.setPayLoad(buffer.slice());
    return packet;
}

From source file:com.spotify.heroic.metric.datastax.schema.legacy.MapSerializer.java

private <T> T next(ByteBuffer buffer, TypeSerializer<T> serializer) throws IOException {
    final short segment = buffer.getShort();
    final ByteBuffer slice = buffer.slice();
    slice.limit(segment);// ww w  .  j  a  v  a  2s .  c o  m
    final T value = serializer.deserialize(slice);

    buffer.position(buffer.position() + segment);
    return value;
}

From source file:org.apache.cassandra.db.marshal.UUIDType.java

public UUID compose(ByteBuffer bytes) {

    bytes = bytes.slice();
    if (bytes.remaining() < 16)
        return new UUID(0, 0);
    return new UUID(bytes.getLong(), bytes.getLong());
}

From source file:edu.tamu.tcat.crypto.spongycastle.SecureTokenImpl.java

@Override
public String getToken(ByteBuffer content) throws TokenException {
    try {//w ww.j a va  2 s  .c  o  m
        byte[] token = createToken(content.slice());
        byte[] iv = createIV();
        Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, iv);
        int outputSize = cipher.getOutputSize(token.length);
        byte[] encrypted = new byte[outputSize + (ivSize / 8)];
        System.arraycopy(iv, 0, encrypted, 0, iv.length);
        cipher.doFinal(token, 0, token.length, encrypted, iv.length);
        String encoded = Base64.encodeBase64URLSafeString(encrypted);
        return encoded;
    } catch (NoSuchAlgorithmException e) {
        throw new TokenException("Missing algorithm", e);
    } catch (IllegalBlockSizeException e) {
        throw new TokenException(
                "Should never happen but is thrown because Sun/Oracle doesn't understand that encryption/decryption modes are different and should really have different APIs.  This is a decrypt only problem",
                e);
    } catch (BadPaddingException e) {
        throw new TokenException(
                "Should never happen but is thrown because Sun/Oracle doesn't understand that encryption/decryption modes are different and should really have different APIs.  This is a decrypt only problem",
                e);
    } catch (ShortBufferException e) {
        throw new TokenException("Should never happen", e);
    }
}

From source file:edu.tamu.tcat.crypto.bouncycastle.SecureTokenImpl.java

@Override
public String getToken(ByteBuffer content) throws TokenException {
    try {//from w ww.j  av a  2  s.  c o m
        byte[] token = createToken(content.slice());
        byte[] iv = createIV();
        Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, iv);
        int outputSize = cipher.getOutputSize(token.length);
        // The token value returned contains the IV followed by the encrypted payload
        byte[] encrypted = new byte[outputSize + (ivSize / 8)];
        System.arraycopy(iv, 0, encrypted, 0, iv.length);
        cipher.doFinal(token, 0, token.length, encrypted, iv.length);
        String encoded = Base64.encodeBase64URLSafeString(encrypted);
        return encoded;
    } catch (NoSuchAlgorithmException e) {
        throw new TokenException("Missing algorithm", e);
    } catch (IllegalBlockSizeException e) {
        throw new TokenException(
                "Should never happen but is thrown because Sun/Oracle doesn't understand that encryption/decryption modes are different and should really have different APIs.  This is a decrypt only problem",
                e);
    } catch (BadPaddingException e) {
        throw new TokenException(
                "Should never happen but is thrown because Sun/Oracle doesn't understand that encryption/decryption modes are different and should really have different APIs.  This is a decrypt only problem",
                e);
    } catch (ShortBufferException e) {
        throw new TokenException("Should never happen", e);
    }
}

From source file:com.koda.integ.hbase.util.CacheableSerializer.java

@Override
public Cacheable read(ByteBuffer buf) throws IOException {
    if (deserializer == null || deserializer.get() == null) {
        return null;
    }/*from  w w  w  .j a  v  a 2s .  co m*/
    ByteBuffer bbuf = buf.slice();
    bbuf.order(buf.order());
    return deserializer.get().deserialize(bbuf);
}

From source file:com.mellanox.jxio.Msg.java

private ByteBuffer createSubBuffer(int position, int limit, ByteBuffer buf) {
    ByteBuffer sub;/*from   www. j ava2  s.  co  m*/
    buf.position(position);
    buf.limit(limit);
    sub = buf.slice();
    return sub;
}

From source file:org.apache.hadoop.hbase.io.hfile.slab.Slab.java

private void allocateAndSlice(int size, int sliceSize) {
    ByteBuffer newSlab = ByteBuffer.allocateDirect(size);
    slabs.add(newSlab);/*ww  w. ja  v a 2  s.c  o  m*/
    for (int j = 0; j < newSlab.capacity(); j += sliceSize) {
        newSlab.limit(j + sliceSize).position(j);
        ByteBuffer aSlice = newSlab.slice();
        buffers.add(aSlice);
        heapSize += ClassSize.estimateBase(aSlice.getClass(), false);
    }
}

From source file:org.apache.cassandra.db.marshal.TimeUUIDType.java

public void validate(ByteBuffer bytes) throws MarshalException {
    if (bytes.remaining() != 16 && bytes.remaining() != 0)
        throw new MarshalException(String.format("TimeUUID should be 16 or 0 bytes (%d)", bytes.remaining()));
    ByteBuffer slice = bytes.slice();
    // version is bits 4-7 of byte 6.
    if (bytes.remaining() > 0) {
        slice.position(6);/*from  w ww.j  ava2 s.  com*/
        if ((slice.get() & 0xf0) != 0x10)
            throw new MarshalException("Invalid version for TimeUUID type.");
    }
}