Example usage for java.nio ByteBuffer getLong

List of usage examples for java.nio ByteBuffer getLong

Introduction

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

Prototype

public abstract long getLong(int index);

Source Link

Document

Returns the long at the specified index.

Usage

From source file:com.palantir.atlasdb.keyvalue.cassandra.CassandraKeyValueServices.java

static String convertCassandraByteBufferUUIDtoString(ByteBuffer uuid) {
    return new UUID(uuid.getLong(uuid.position()), uuid.getLong(uuid.position() + 8)).toString();
}

From source file:burstcoin.observer.service.ATService.java

public static String getATLong(String hex) {
    ByteBuffer bf = ByteBuffer.allocate(8);
    bf.order(ByteOrder.LITTLE_ENDIAN);
    bf.put(parseHexString(hex));/*  ww  w.java2s .  co  m*/
    return toUnsignedLong(bf.getLong(0));
}

From source file:mtmo.test.mediadrm.Utils.java

public static String accountIdToMarlinFormat(final String accountId) {
    ByteBuffer accountIdBuf = ByteBuffer.allocate(BYTES_OF_ACCOUNT_ID);
    try {// ww  w  .  j av a2 s.co m
        accountIdBuf.putLong(Long.valueOf(accountId));
    } catch (Exception e) {
        return null;
    }
    accountIdBuf.order(ByteOrder.LITTLE_ENDIAN);
    return String.format(Locale.US, "%016x", accountIdBuf.getLong(0));
}

From source file:com.alibaba.rocketmq.common.MixAll.java

public static long createBrokerId(final String ip, final int port) {
    InetSocketAddress isa = new InetSocketAddress(ip, port);
    byte[] ipArray = isa.getAddress().getAddress();
    ByteBuffer bb = ByteBuffer.allocate(8);
    bb.put(ipArray);//w ww.  j  a v a 2 s  .c  o  m
    bb.putInt(port);
    long value = bb.getLong(0);
    return Math.abs(value);
}

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

public static long toLong(ByteBuffer bytes) {
    return bytes.getLong(bytes.position());
}

From source file:net.onrc.openvirtex.packet.OVXLLDP.java

/**
 * Extracts dpid and port from OVX-generated LLDP packet.
 *
 * @param packet/*w  w  w .j  ava  2  s . c o  m*/
 * @return Dpid and port
 */
public static DPIDandPort parseLLDP(final byte[] packet) {
    final ByteBuffer bb = ByteBuffer.wrap(packet);

    // Extra offset due to VLAN tag
    int offset = 0;
    if (bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_LLDP
            && bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_BSN) {
        offset = 4;
    }

    final short port = bb.getShort(PORT_OFFSET + offset);
    final long dpid = bb.getLong(DPID_OFFSET + offset);

    return new DPIDandPort(dpid, port);
}

From source file:org.onlab.packet.ONLabLddp.java

/**
 * Extracts dpid and port from OVX-generated LLDP packet.
 *
 * @param packet packet data/*from  ww w. j  a v  a  2 s  . c om*/
 * @return Dpid and port
 */
public static DPIDandPort parseLLDP(final byte[] packet) {
    final ByteBuffer bb = ByteBuffer.wrap(packet);

    // Extra offset due to VLAN tag
    int offset = 0;
    if (bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_LLDP
            && bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_BSN) {
        offset = 4;
    }

    final int port = bb.getInt(PORT_OFFSET + offset);
    final long dpid = bb.getLong(DPID_OFFSET + offset);

    return new DPIDandPort(dpid, port);
}

From source file:FindInt.java

public int seek() throws IOException {
    int num = -1;

    try (SeekableByteChannel fc = Files.newByteChannel(file)) {

        ByteBuffer buf = ByteBuffer.allocate(8);

        //Get the offset into the file.
        fc.read(buf);//w  w w . j a  v  a  2s.co m
        long offset = buf.getLong(0);

        //Seek to the offset location.
        fc.position(offset);
        buf.rewind();

        //Read the 'secret' value.
        fc.read(buf);
        num = buf.getInt(0);
    } catch (IOException x) {
        System.err.println(x);
    }
    return num;
}

From source file:com.mbientlab.metawear.app.popup.DataPlotFragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    final Collection<DataPoint> convertedX = new ArrayList<>(), convertedY = new ArrayList<>(),
            convertedZ = new ArrayList<>();

    GraphView graph = (GraphView) view.findViewById(R.id.data_plot);
    graph.getViewport().setScrollable(true);
    graph.getViewport().setScalable(true);

    graph.getViewport().setYAxisBoundsManual(true);
    graph.getViewport().setMaxY(8.0);/* w w w .j  a va 2s . c o  m*/
    graph.getViewport().setMinY(-8.0);
    graph.getLegendRenderer().setVisible(true);
    graph.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.MIDDLE);

    for (byte[] dataBytes : accelConfig.polledBytes()) {
        ByteBuffer buffer = ByteBuffer.wrap(dataBytes);
        double tickInS = (double) (buffer.getLong(6) / 1000.0);
        float xAccel, yAccel, zAccel;

        if (accelConfig.firmwarePos() == 0) {
            xAccel = buffer.getShort(0) / 1000.0f;
            yAccel = buffer.getShort(2) / 1000.0f;
            zAccel = buffer.getShort(4) / 1000.0f;
        } else {
            xAccel = BytesInterpreter.bytesToGs(accelConfig.getSamplingConfig(), buffer.getShort(0));
            yAccel = BytesInterpreter.bytesToGs(accelConfig.getSamplingConfig(), buffer.getShort(2));
            zAccel = BytesInterpreter.bytesToGs(accelConfig.getSamplingConfig(), buffer.getShort(4));
        }
        convertedX.add(new DataPoint(tickInS, xAccel));
        convertedY.add(new DataPoint(tickInS, yAccel));
        convertedZ.add(new DataPoint(tickInS, zAccel));

    }

    addDataSeries("X-Axis", convertedX);
    addDataSeries("Y-Axis", convertedY);
    addDataSeries("Z-Axis", convertedZ);

    int colorIndex = 0;
    for (Entry<String, LineGraphSeries<DataPoint>> data : dataSeries.entrySet()) {
        data.getValue().setColor(chartColors[colorIndex]);
        data.getValue().setTitle(data.getKey());
        graph.addSeries(data.getValue());
        colorIndex++;
    }
}

From source file:com.linkedin.pinot.core.indexsegment.utils.MmapMemoryManagerTest.java

@Test
public void testLargeBlocks() throws Exception {
    final String segmentName = "someSegment";
    PinotDataBufferMemoryManager memoryManager = new MmapMemoryManager(_tmpDir, segmentName);
    final long s1 = 2 * MmapMemoryManager.getDefaultFileLength();
    final long s2 = 1000;
    final String col1 = "col1";
    final String col2 = "col2";
    final byte value = 34;
    PinotDataBuffer buf1 = memoryManager.allocate(s1, col1);

    // Verify that we can write to and read from the buffer
    ByteBuffer b1 = buf1.toDirectByteBuffer(0, (int) s1);
    b1.putLong(0, s1);/*from w  w  w. ja  v  a  2  s .  co m*/
    Assert.assertEquals(b1.getLong(0), s1);
    b1.put((int) s1 - 1, value);
    Assert.assertEquals(b1.get((int) s1 - 1), value);

    PinotDataBuffer buf2 = memoryManager.allocate(s2, col2);
    ByteBuffer b2 = buf2.toDirectByteBuffer(0, (int) s2);
    b2.putLong(0, s2);
    Assert.assertEquals(b2.getLong(0), s2);

    File dir = new File(_tmpDir);
    File[] files = dir.listFiles();
    Assert.assertEquals(files.length, 2);

    Arrays.sort(files, new Comparator<File>() {
        @Override
        public int compare(File o1, File o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });

    String fileName = files[0].getName();
    Assert.assertTrue(fileName.contains(segmentName));

    fileName = files[1].getName();
    Assert.assertTrue(fileName.contains(segmentName));

    buf1.close();
    buf2.close();

    memoryManager.close();

    List<Pair<MmapUtils.AllocationContext, Integer>> allocationContexts = MmapUtils.getAllocationsAndSizes();
    Assert.assertEquals(allocationContexts.size(), 0);
    Assert.assertEquals(new File(_tmpDir).listFiles().length, 0);
}