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();

Source Link

Document

Returns the long at the current position and increases the position by 8.

Usage

From source file:eu.europa.esig.dss.DSSUtils.java

public static long toLong(final byte[] bytes) {
    // Long.valueOf(new String(bytes)).longValue();
    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.put(bytes, 0, Long.SIZE / 8);
    // TODO: (Bob: 2014 Jan 22) To be checked if it is not platform dependent?
    buffer.flip();//need flip
    return buffer.getLong();
}

From source file:org.cloudata.core.commitlog.pipe.RunState.java

@Override
public boolean writeFileDone(Context ctx) throws IOException {
    ctx.status.scheduled = false;/*from   w  w  w. j av a2s.com*/
    ctx.status.writingDone = true;
    ByteBuffer ret = ByteBuffer.allocate(9);

    if (ctx.signalReceiver.read(ret) < 0) {
        throw new IOException("Internal pipe disconnection");
    }

    ret.flip();
    ctx.bulk.clear();

    byte result = ret.get();
    long time = ret.getLong();

    ctx.writeDoneTime = System.currentTimeMillis();

    CommitLogServer.metrics.setWrite(ctx.bulk.totalNumWritten, (ctx.writeDoneTime - time));

    if (ctx.writeDoneTime - time > 100) {
        LOG.info("TIME REPORT [too long time to be signaled] takes " + (ctx.writeDoneTime - time) + "ms");
    }

    if (result == AsyncFileWriter.NACK) {
        writeFileFail(ctx);
    }

    if (ctx.isLastPipe) {
        ctx.ack.clear();
        ctx.ack.put(AsyncFileWriter.ACK);
        ctx.ack.putLong(System.currentTimeMillis());
        ctx.ack.flip();
        writeToPrev(ctx);
    } else if (ctx.status.ackReceived) {
        writeToPrev(ctx);
    }

    return true;
}

From source file:org.apache.htrace.impl.PackedBufferManager.java

private void readAndValidateResponseFrame(SelectionKey sockKey, ByteBuffer buf, long expectedSeq,
        int expectedMethodId) throws IOException {
    buf.clear();//www.  j  ava  2s  .c  o  m
    buf.limit(PackedBuffer.HRPC_RESP_FRAME_LENGTH);
    doRecv(sockKey, buf);
    buf.flip();
    buf.order(ByteOrder.LITTLE_ENDIAN);
    long seq = buf.getLong();
    if (seq != expectedSeq) {
        throw new IOException("Expected sequence number " + expectedSeq + ", but got sequence number " + seq);
    }
    int methodId = buf.getInt();
    if (expectedMethodId != methodId) {
        throw new IOException("Expected method id " + expectedMethodId + ", but got " + methodId);
    }
    int errorLength = buf.getInt();
    buf.getInt();
    if ((errorLength < 0) || (errorLength > PackedBuffer.MAX_HRPC_ERROR_LENGTH)) {
        throw new IOException("Got server error with invalid length " + errorLength);
    } else if (errorLength > 0) {
        buf.clear();
        buf.limit(errorLength);
        doRecv(sockKey, buf);
        buf.flip();
        CharBuffer charBuf = StandardCharsets.UTF_8.decode(buf);
        String serverErrorStr = charBuf.toString();
        throw new IOException("Got server error " + serverErrorStr);
    }
}

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

public MapReply deserialize(ByteBuffer replyBuffer) {
    MapReplyBuilder builder = new MapReplyBuilder();
    byte typeAndFlags = replyBuffer.get();
    builder.setProbe(ByteUtil.extractBit(typeAndFlags, Flags.PROBE));
    builder.setEchoNonceEnabled(ByteUtil.extractBit(typeAndFlags, Flags.ECHO_NONCE_ENABLED));
    builder.setSecurityEnabled(ByteUtil.extractBit(typeAndFlags, Flags.SECURITY_ENABLED));
    replyBuffer.getShort();/*from w ww  .ja  v a  2 s  . c  o m*/
    int recordCount = ByteUtil.getUnsignedByte(replyBuffer);
    builder.setNonce(replyBuffer.getLong());
    builder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
    for (int i = 0; i < recordCount; i++) {
        builder.getEidToLocatorRecord().add(new EidToLocatorRecordBuilder(
                EidToLocatorRecordSerializer.getInstance().deserialize(replyBuffer)).build());
    }

    return builder.build();
}

From source file:org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer.java

public MapReply deserialize(ByteBuffer replyBuffer) {
    MapReplyBuilder builder = new MapReplyBuilder();
    byte typeAndFlags = replyBuffer.get();
    builder.setProbe(ByteUtil.extractBit(typeAndFlags, Flags.PROBE));
    builder.setEchoNonceEnabled(ByteUtil.extractBit(typeAndFlags, Flags.ECHO_NONCE_ENABLED));
    builder.setSecurityEnabled(ByteUtil.extractBit(typeAndFlags, Flags.SECURITY_ENABLED));
    replyBuffer.getShort();//  w  w  w  . j  a va2  s  . co m
    int recordCount = ByteUtil.getUnsignedByte(replyBuffer);
    builder.setNonce(replyBuffer.getLong());
    builder.setMappingRecordItem(new ArrayList<MappingRecordItem>());
    for (int i = 0; i < recordCount; i++) {
        builder.getMappingRecordItem().add(new MappingRecordItemBuilder()
                .setMappingRecord(MappingRecordSerializer.getInstance().deserialize(replyBuffer)).build());
    }

    return builder.build();
}

From source file:org.cosmo.common.util.Util.java

public static long shortsToLong2(short[] shorts) {
    ByteBuffer b = ByteBuffer.allocate(8);
    b.putShort(shorts[0]);//from  w w w  .ja  v  a 2s  . co  m
    b.putShort(shorts[1]);
    b.putShort(shorts[2]);
    b.putShort(shorts[3]);
    b.rewind();
    return b.getLong();
}

From source file:com.srotya.monitoring.kafka.util.KafkaConsumerOffsetUtil.java

private long readOffsetMessageValue(ByteBuffer buffer) {
    buffer.getShort(); // read and ignore version
    long offset = buffer.getLong();
    return offset;
}

From source file:com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer.java

protected SchemaIdVersion retrieveSchemaIdVersion(InputStream payloadInputStream) throws SerDesException {
    // it can be enhanced to have respective protocol handlers for different versions
    // first byte is protocol version/id.
    // protocol format:
    // 1 byte  : protocol version
    // 8 bytes : schema metadata Id
    // 4 bytes : schema version
    ByteBuffer byteBuffer = ByteBuffer.allocate(13);
    try {/*  w  w w.  j  av a  2 s  .c o  m*/
        payloadInputStream.read(byteBuffer.array());
    } catch (IOException e) {
        throw new SerDesException(e);
    }

    byte protocolId = byteBuffer.get();
    if (protocolId != AvroSchemaProvider.CURRENT_PROTOCOL_VERSION) {
        throw new SerDesException(
                "Unknown protocol id [" + protocolId + "] received while deserializing the payload");
    }
    long schemaMetadataId = byteBuffer.getLong();
    int schemaVersion = byteBuffer.getInt();

    return new SchemaIdVersion(schemaMetadataId, schemaVersion);
}

From source file:com.sm.connector.server.ServerStore.java

protected Pair<byte[], byte[]> next(int current, ByteBuffer buf) throws IOException {
    buf.clear();// ww  w.ja va  2 s  .  c o m
    long pos = OFFSET + (long) current * RECORD_SIZE;
    indexChannel.read(buf, pos);
    buf.rewind();
    byte status = buf.get();
    if (isDeleted(status)) {
        return null;
    } else {
        long keyLen = buf.getLong();
        byte[] keys = readChannel(keyLen, keyChannel);
        long data = buf.getLong();
        long block2version = buf.getLong();
        CacheBlock block = new CacheBlock(current, data, block2version, status);
        if (block.getDataOffset() <= 0 || block.getDataLen() <= 0
                || block.getBlockSize() < block.getDataLen()) {
            throw new StoreException("data reading error");
        } else {
            //Key key = toKey(keys);
            byte[] datas = readChannel(block.getDataOffset2Len(), dataChannel);
            return new Pair(keys, datas);
        }
    }
}

From source file:edu.umass.cs.gigapaxos.paxospackets.AcceptReplyPacket.java

/**
 * @param bbuf/*from  ww w.  j  av a 2 s  . co m*/
 * @throws UnsupportedEncodingException
 * @throws UnknownHostException
 */
public AcceptReplyPacket(ByteBuffer bbuf) throws UnsupportedEncodingException, UnknownHostException {
    super(bbuf);
    int paxosIDLength = this.getPaxosID().getBytes(CHARSET).length;
    assert (bbuf.position() == SIZEOF_PAXOSPACKET_FIXED + paxosIDLength);
    this.acceptor = bbuf.getInt();
    this.ballot = new Ballot(bbuf.getInt(), bbuf.getInt());
    this.slotNumber = bbuf.getInt();
    this.maxCheckpointedSlot = bbuf.getInt();
    this.requestID = bbuf.getLong();
    if (bbuf.get() == (byte) 1)
        this.setDigestRequest();
    assert (bbuf.position() == SIZEOF_PAXOSPACKET_FIXED + paxosIDLength + SIZEOF_ACCEPTREPLY) : bbuf.position()
            + " != [" + SIZEOF_PAXOSPACKET_FIXED + " + " + paxosIDLength + " + " + SIZEOF_ACCEPTREPLY + "]";
}