Example usage for java.nio ByteBuffer getChar

List of usage examples for java.nio ByteBuffer getChar

Introduction

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

Prototype

public abstract char getChar();

Source Link

Document

Returns the char at the current position and increases the position by 2.

Usage

From source file:com.googlecode.mp4parser.boxes.microsoft.XtraBox.java

private static String readUtf16String(ByteBuffer content, int length) {
    char s[] = new char[(length / 2) - 1];
    for (int i = 0; i < (length / 2) - 1; i++) {
        s[i] = content.getChar();
    }/*from www  .jav  a 2 s.  c  om*/
    content.getChar(); //Discard terminating null
    return new String(s);
}

From source file:gobblin.util.io.StreamUtilsTest.java

private void verifyBuffer(byte[] srcBytes, ByteBuffer buffer) throws IOException {
    buffer.put(srcBytes);/*from  ww  w  .  j a va  2 s  . c om*/
    buffer.flip();

    ByteArrayOutputStream bOs = new ByteArrayOutputStream();
    StreamUtils.byteBufferToOutputStream(buffer, bOs);
    Assert.assertEquals(bOs.toByteArray(), srcBytes);

    bOs = new ByteArrayOutputStream();
    buffer.rewind();

    // consume one character from the buf; make sure it is not included in the output by
    // byteBufferToOutputStream
    buffer.getChar();
    StreamUtils.byteBufferToOutputStream(buffer, bOs);
    byte[] offByTwo = bOs.toByteArray();
    Assert.assertEquals(offByTwo.length, srcBytes.length - 2);
    for (int i = 0; i < offByTwo.length; i++) {
        Assert.assertEquals(offByTwo[i], srcBytes[i + 2]);
    }
}

From source file:org.apache.carbondata.core.util.CarbonUtil.java

public static ValueEncoderMeta deserializeEncoderMetaNew(byte[] encodeMeta) {
    ByteBuffer buffer = ByteBuffer.wrap(encodeMeta);
    char measureType = buffer.getChar();
    ValueEncoderMeta valueEncoderMeta = new ValueEncoderMeta();
    valueEncoderMeta.setType(measureType);
    switch (measureType) {
    case CarbonCommonConstants.DOUBLE_MEASURE:
        valueEncoderMeta.setMaxValue(buffer.getDouble());
        valueEncoderMeta.setMinValue(buffer.getDouble());
        valueEncoderMeta.setUniqueValue(buffer.getDouble());
        break;//from   w  w  w  .  j a v a2 s  . co m
    case CarbonCommonConstants.BIG_DECIMAL_MEASURE:
        valueEncoderMeta.setMaxValue(0.0);
        valueEncoderMeta.setMinValue(0.0);
        valueEncoderMeta.setUniqueValue(0.0);
        break;
    case CarbonCommonConstants.BIG_INT_MEASURE:
        valueEncoderMeta.setMaxValue(buffer.getLong());
        valueEncoderMeta.setMinValue(buffer.getLong());
        valueEncoderMeta.setUniqueValue(buffer.getLong());
        break;
    default:
        throw new IllegalArgumentException("invalid measure type");
    }
    valueEncoderMeta.setDecimal(buffer.getInt());
    valueEncoderMeta.setDataTypeSelected(buffer.get());
    return valueEncoderMeta;
}

From source file:com.l2jfree.network.mmocore.ReadWriteThread.java

private boolean tryReadPacket2(T con, ByteBuffer buf) {
    // check if header could be processed
    if (buf.remaining() >= 2) {
        // parse all headers and get expected packet size
        final int size = (buf.getChar() - PACKET_HEADER_SIZE);

        // do we got enough bytes for the packet?
        if (size <= buf.remaining()) {
            // avoid parsing dummy packets (packets without body)
            if (size > 0) {
                int pos = buf.position();
                parseClientPacket(buf, size, con);
                buf.position(pos + size);
            } else {
                // let's report error to trigger protection
                getMMOController().report(ErrorMode.EMPTY_PACKET, con, null, null);
            }// w w w. j  av a  2 s  .  com

            return true;
        } else {
            // we dont have enough bytes for the packet so we need to read and revert the header
            buf.position(buf.position() - PACKET_HEADER_SIZE);
            return false;
        }
    } else {
        // we dont have enough data for header so we need to read
        return false;
    }
}

From source file:de.rwhq.btree.LeafNode.java

@Override
public void load() {
    final ByteBuffer buf = rawPage().bufferForReading(0);
    if (buf.getChar() != NODE_TYPE.serialize())
        throw new IllegalStateException("The RawPage " + rawPage.id() + " doesnt have the Leaf Node Type");

    numberOfEntries = rawPage().bufferForReading(Header.NUMBER_OF_KEYS.getOffset()).getInt();
    valid = true;/*from  w ww .  j a va2 s.co  m*/
}

From source file:de.rwhq.btree.InnerNode.java

@Override
public void load() throws IOException {
    final ByteBuffer buf = rawPage.bufferForReading(0);
    if (NodeType.deserialize(buf.getChar()) != NODE_TYPE)
        throw new IOException(
                "You are trying to load a InnerNode from a byte array, that does not contain an InnerNode");

    buf.position(Header.NUMBER_OF_KEYS.getOffset());
    numberOfKeys = buf.getInt();//from  w ww. j  a  v a2 s.  c  om
    valid = true;
}