Example usage for java.nio ByteBuffer getShort

List of usage examples for java.nio ByteBuffer getShort

Introduction

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

Prototype

public abstract short getShort();

Source Link

Document

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

Usage

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

public static int[] getIntArray(ByteBuffer data, int offset, int length) {
    if (length == 0) {
        return new int[0];
    }/*  w w  w.j  a va 2s  .  c  om*/
    data.position(offset);
    int[] intArray = new int[length / 2];
    int index = 0;
    while (index < intArray.length) {
        intArray[index++] = data.getShort();
    }
    return intArray;
}

From source file:org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.DlmsHelperService.java

public CosemDateTimeDto fromDateTimeValue(final byte[] dateTimeValue) {

    final ByteBuffer bb = ByteBuffer.wrap(dateTimeValue);

    final int year = bb.getShort() & 0xFFFF;
    final int monthOfYear = bb.get() & 0xFF;
    final int dayOfMonth = bb.get() & 0xFF;
    final int dayOfWeek = bb.get() & 0xFF;
    final int hourOfDay = bb.get() & 0xFF;
    final int minuteOfHour = bb.get() & 0xFF;
    final int secondOfMinute = bb.get() & 0xFF;
    final int hundredthsOfSecond = bb.get() & 0xFF;
    final int deviation = bb.getShort();
    final byte clockStatusValue = bb.get();

    final CosemDateDto date = new CosemDateDto(year, monthOfYear, dayOfMonth, dayOfWeek);
    final CosemTimeDto time = new CosemTimeDto(hourOfDay, minuteOfHour, secondOfMinute, hundredthsOfSecond);
    final ClockStatusDto clockStatus = new ClockStatusDto(clockStatusValue);
    return new CosemDateTimeDto(date, time, deviation, clockStatus);
}

From source file:io.druid.hll.HyperLogLogCollectorTest.java

@Test
public void testFoldWithUpperNibbleTriggersOffsetChange() throws Exception {
    byte[] arr1 = new byte[HyperLogLogCollector.getLatestNumBytesForDenseStorage()];
    Arrays.fill(arr1, (byte) 0x11);
    ByteBuffer buffer1 = ByteBuffer.wrap(arr1);
    buffer1.put(0, HLLCV1.VERSION);/*from w w w  . j a va2s .  c o  m*/
    buffer1.put(1, (byte) 0);
    buffer1.putShort(2, (short) (2047));
    buffer1.put(HLLCV1.HEADER_NUM_BYTES, (byte) 0x1);

    byte[] arr2 = new byte[HyperLogLogCollector.getLatestNumBytesForDenseStorage()];
    Arrays.fill(arr2, (byte) 0x11);
    ByteBuffer buffer2 = ByteBuffer.wrap(arr2);
    buffer2.put(0, HLLCV1.VERSION);
    buffer2.put(1, (byte) 0);
    buffer2.putShort(2, (short) (2048));

    HyperLogLogCollector collector = HyperLogLogCollector.makeCollector(buffer1);
    collector.fold(buffer2);

    ByteBuffer outBuffer = collector.toByteBuffer();

    Assert.assertEquals(outBuffer.get(), HLLCV1.VERSION);
    Assert.assertEquals(outBuffer.get(), 1);
    Assert.assertEquals(outBuffer.getShort(), 0);
    outBuffer.get();
    outBuffer.getShort();
    Assert.assertFalse(outBuffer.hasRemaining());
}

From source file:org.opendaylight.lispflowmapping.implementation.serializer.MapRegisterSerializer.java

public MapRegister deserialize(ByteBuffer registerBuffer) {
    try {//from   w w  w.  j  av  a 2s.  co  m
        MapRegisterBuilder builder = new MapRegisterBuilder();
        builder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());

        byte typeAndFlags = registerBuffer.get();
        boolean xtrSiteIdPresent = ByteUtil.extractBit(typeAndFlags, Flags.XTRSITEID);
        builder.setProxyMapReply(ByteUtil.extractBit(typeAndFlags, Flags.PROXY));
        builder.setXtrSiteIdPresent(xtrSiteIdPresent);

        registerBuffer.position(registerBuffer.position() + Length.RES);
        builder.setWantMapNotify(ByteUtil.extractBit(registerBuffer.get(), Flags.WANT_MAP_REPLY));
        byte recordCount = (byte) ByteUtil.getUnsignedByte(registerBuffer);
        builder.setNonce(registerBuffer.getLong());
        builder.setKeyId(registerBuffer.getShort());
        short authenticationLength = registerBuffer.getShort();
        byte[] authenticationData = new byte[authenticationLength];
        registerBuffer.get(authenticationData);
        builder.setAuthenticationData(authenticationData);

        for (int i = 0; i < recordCount; i++) {
            builder.getEidToLocatorRecord().add(new EidToLocatorRecordBuilder(
                    EidToLocatorRecordSerializer.getInstance().deserialize(registerBuffer)).build());
        }

        if (xtrSiteIdPresent) {
            byte[] xtrId = new byte[Length.XTRID_SIZE];
            registerBuffer.get(xtrId);
            byte[] siteId = new byte[Length.SITEID_SIZE];
            registerBuffer.get(siteId);
            builder.setXtrId(xtrId);
            builder.setSiteId(siteId);
        }
        registerBuffer.limit(registerBuffer.position());
        byte[] mapRegisterBytes = new byte[registerBuffer.position()];
        registerBuffer.position(0);
        registerBuffer.get(mapRegisterBytes);
        return builder.build();
    } catch (RuntimeException re) {
        throw new LispSerializationException(
                "Couldn't deserialize Map-Register (len=" + registerBuffer.capacity() + ")", re);
    }

}

From source file:io.druid.hll.HyperLogLogCollectorTest.java

@Test
public void testFoldWithDifferentOffsets2() throws Exception {
    ByteBuffer biggerOffset = makeCollectorBuffer(1, (byte) 0x01, 0x11);
    ByteBuffer smallerOffset = makeCollectorBuffer(0, (byte) 0x20, 0x00);

    HyperLogLogCollector collector = HyperLogLogCollector.makeLatestCollector();
    collector.fold(biggerOffset);//from   ww  w .  j  a va  2  s .  co m
    collector.fold(smallerOffset);

    ByteBuffer outBuffer = collector.toByteBuffer();

    Assert.assertEquals(outBuffer.get(), collector.getVersion());
    Assert.assertEquals(outBuffer.get(), 2);
    Assert.assertEquals(outBuffer.getShort(), 0);
    outBuffer.get();
    outBuffer.getShort();
    Assert.assertFalse(outBuffer.hasRemaining());

    collector = HyperLogLogCollector.makeLatestCollector();
    collector.fold(smallerOffset);
    collector.fold(biggerOffset);

    outBuffer = collector.toByteBuffer();

    Assert.assertEquals(outBuffer.get(), collector.getVersion());
    Assert.assertEquals(outBuffer.get(), 2);
    Assert.assertEquals(outBuffer.getShort(), 0);
    outBuffer.get();
    outBuffer.getShort();
    Assert.assertFalse(outBuffer.hasRemaining());
}

From source file:io.druid.hll.HyperLogLogCollectorTest.java

@Test
public void testSparseFoldWithDifferentOffsets1() throws Exception {
    ByteBuffer biggerOffset = makeCollectorBuffer(1, new byte[] { 0x11, 0x10 }, 0x11);
    ByteBuffer sparse = HyperLogLogCollector
            .makeCollector(makeCollectorBuffer(0, new byte[] { 0x00, 0x02 }, 0x00)).toByteBuffer();

    HyperLogLogCollector collector = HyperLogLogCollector.makeLatestCollector();
    collector.fold(biggerOffset);//from   w w w . j ava2 s .  c o  m
    collector.fold(sparse);

    ByteBuffer outBuffer = collector.toByteBuffer();

    Assert.assertEquals(outBuffer.get(), collector.getVersion());
    Assert.assertEquals(outBuffer.get(), 2);
    Assert.assertEquals(outBuffer.getShort(), 0);
    Assert.assertEquals(outBuffer.get(), 0);
    Assert.assertEquals(outBuffer.getShort(), 0);
    Assert.assertFalse(outBuffer.hasRemaining());

    collector = HyperLogLogCollector.makeLatestCollector();
    collector.fold(sparse);
    collector.fold(biggerOffset);

    outBuffer = collector.toByteBuffer();

    Assert.assertEquals(outBuffer.get(), collector.getVersion());
    Assert.assertEquals(outBuffer.get(), 2);
    Assert.assertEquals(outBuffer.getShort(), 0);
    Assert.assertEquals(outBuffer.get(), 0);
    Assert.assertEquals(outBuffer.getShort(), 0);
    Assert.assertFalse(outBuffer.hasRemaining());
}

From source file:org.apache.druid.hll.HyperLogLogCollectorTest.java

@Test
public void testFoldWithDifferentOffsets2() {
    ByteBuffer biggerOffset = makeCollectorBuffer(1, (byte) 0x01, 0x11);
    ByteBuffer smallerOffset = makeCollectorBuffer(0, (byte) 0x20, 0x00);

    HyperLogLogCollector collector = HyperLogLogCollector.makeLatestCollector();
    collector.fold(biggerOffset);/*from w  w  w .  j  a  v  a  2s  .  c o  m*/
    collector.fold(smallerOffset);

    ByteBuffer outBuffer = collector.toByteBuffer();

    Assert.assertEquals(outBuffer.get(), collector.getVersion());
    Assert.assertEquals(outBuffer.get(), 2);
    Assert.assertEquals(outBuffer.getShort(), 0);
    outBuffer.get();
    outBuffer.getShort();
    Assert.assertFalse(outBuffer.hasRemaining());

    collector = HyperLogLogCollector.makeLatestCollector();
    collector.fold(smallerOffset);
    collector.fold(biggerOffset);

    outBuffer = collector.toByteBuffer();

    Assert.assertEquals(outBuffer.get(), collector.getVersion());
    Assert.assertEquals(outBuffer.get(), 2);
    Assert.assertEquals(outBuffer.getShort(), 0);
    outBuffer.get();
    outBuffer.getShort();
    Assert.assertFalse(outBuffer.hasRemaining());
}

From source file:org.apache.druid.hll.HyperLogLogCollectorTest.java

@Test
public void testSparseFoldWithDifferentOffsets1() {
    ByteBuffer biggerOffset = makeCollectorBuffer(1, new byte[] { 0x11, 0x10 }, 0x11);
    ByteBuffer sparse = HyperLogLogCollector
            .makeCollector(makeCollectorBuffer(0, new byte[] { 0x00, 0x02 }, 0x00)).toByteBuffer();

    HyperLogLogCollector collector = HyperLogLogCollector.makeLatestCollector();
    collector.fold(biggerOffset);/*from  w ww  .  j a v a2  s .c  o  m*/
    collector.fold(sparse);

    ByteBuffer outBuffer = collector.toByteBuffer();

    Assert.assertEquals(outBuffer.get(), collector.getVersion());
    Assert.assertEquals(outBuffer.get(), 2);
    Assert.assertEquals(outBuffer.getShort(), 0);
    Assert.assertEquals(outBuffer.get(), 0);
    Assert.assertEquals(outBuffer.getShort(), 0);
    Assert.assertFalse(outBuffer.hasRemaining());

    collector = HyperLogLogCollector.makeLatestCollector();
    collector.fold(sparse);
    collector.fold(biggerOffset);

    outBuffer = collector.toByteBuffer();

    Assert.assertEquals(outBuffer.get(), collector.getVersion());
    Assert.assertEquals(outBuffer.get(), 2);
    Assert.assertEquals(outBuffer.getShort(), 0);
    Assert.assertEquals(outBuffer.get(), 0);
    Assert.assertEquals(outBuffer.getShort(), 0);
    Assert.assertFalse(outBuffer.hasRemaining());
}

From source file:io.druid.hll.HyperLogLogCollectorTest.java

@Test
public void testFoldWithDifferentOffsets1() throws Exception {
    ByteBuffer biggerOffset = makeCollectorBuffer(1, (byte) 0x00, 0x11);
    ByteBuffer smallerOffset = makeCollectorBuffer(0, (byte) 0x20, 0x00);

    HyperLogLogCollector collector = HyperLogLogCollector.makeLatestCollector();
    collector.fold(biggerOffset);//from   ww  w.j a  v  a  2 s  .  c om
    collector.fold(smallerOffset);

    ByteBuffer outBuffer = collector.toByteBuffer();

    Assert.assertEquals(outBuffer.get(), collector.getVersion());
    Assert.assertEquals(outBuffer.get(), 1);
    Assert.assertEquals(outBuffer.getShort(), 2047);
    outBuffer.get();
    outBuffer.getShort();
    Assert.assertEquals(outBuffer.get(), 0x10);
    while (outBuffer.hasRemaining()) {
        Assert.assertEquals(outBuffer.get(), 0x11);
    }

    collector = HyperLogLogCollector.makeLatestCollector();
    collector.fold(smallerOffset);
    collector.fold(biggerOffset);

    outBuffer = collector.toByteBuffer();

    Assert.assertEquals(outBuffer.get(), collector.getVersion());
    Assert.assertEquals(outBuffer.get(), 1);
    Assert.assertEquals(outBuffer.getShort(), 2047);
    Assert.assertEquals(outBuffer.get(), 0);
    Assert.assertEquals(outBuffer.getShort(), 0);
    Assert.assertEquals(outBuffer.get(), 0x10);
    while (outBuffer.hasRemaining()) {
        Assert.assertEquals(outBuffer.get(), 0x11);
    }
}

From source file:io.druid.hll.HyperLogLogCollectorTest.java

@Test
public void testFoldWithArbitraryInitialPositions() throws Exception {
    ByteBuffer biggerOffset = shiftedBuffer(makeCollectorBuffer(1, (byte) 0x00, 0x11), 10);
    ByteBuffer smallerOffset = shiftedBuffer(makeCollectorBuffer(0, (byte) 0x20, 0x00), 15);

    HyperLogLogCollector collector = HyperLogLogCollector.makeLatestCollector();
    collector.fold(biggerOffset);//ww  w . j  a  v  a 2 s.  c  o m
    collector.fold(smallerOffset);

    ByteBuffer outBuffer = collector.toByteBuffer();

    Assert.assertEquals(outBuffer.get(), collector.getVersion());
    Assert.assertEquals(outBuffer.get(), 1);
    Assert.assertEquals(outBuffer.getShort(), 2047);
    outBuffer.get();
    outBuffer.getShort();
    Assert.assertEquals(outBuffer.get(), 0x10);
    while (outBuffer.hasRemaining()) {
        Assert.assertEquals(outBuffer.get(), 0x11);
    }

    collector = HyperLogLogCollector.makeLatestCollector();
    collector.fold(smallerOffset);
    collector.fold(biggerOffset);

    outBuffer = collector.toByteBuffer();

    Assert.assertEquals(outBuffer.get(), collector.getVersion());
    Assert.assertEquals(outBuffer.get(), 1);
    Assert.assertEquals(outBuffer.getShort(), 2047);
    outBuffer.get();
    outBuffer.getShort();
    Assert.assertEquals(outBuffer.get(), 0x10);
    while (outBuffer.hasRemaining()) {
        Assert.assertEquals(outBuffer.get(), 0x11);
    }
}