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:com.jivesoftware.os.rcvs.api.keys.SymetricalHashableKeyTest.java

License:asdf

public long bytesLong(byte[] bytes) {
    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.put(bytes);//from   www .j a v a 2s .c om
    buffer.flip(); //need flip
    return buffer.getLong();
}

From source file:com.buaa.cfs.nfs3.FileHandle.java

private long bytesToLong(byte[] data) {
    ByteBuffer buffer = ByteBuffer.allocate(8);
    for (int i = 0; i < 8; i++) {
        buffer.put(data[i]);/*w w w  .j av  a  2 s.c  o m*/
    }
    buffer.flip();// need flip
    return buffer.getLong();
}

From source file:com.codeabovelab.dm.common.security.token.SignedTokenServiceBackend.java

@Override
public TokenData getToken(String tokenString) {
    final String key = unwrapToken(tokenString);
    if (key.isEmpty()) {
        return null;
    }/*from www  .  j a  v  a2s  .  c  o m*/
    byte[] decodedKey = base32.decode(key);
    byte[] currentSignature;
    final long creationTime;
    byte[] random;
    byte[] payload;
    {
        byte[] content;
        {
            ByteBuffer buffer = ByteBuffer.wrap(decodedKey);
            content = restoreArray(buffer);
            currentSignature = restoreArray(buffer);
        }

        ByteBuffer contentBuffer = ByteBuffer.wrap(content);
        creationTime = contentBuffer.getLong();
        random = restoreArray(contentBuffer);
        // we need to skip secret
        restoreArray(contentBuffer);
        payload = restoreArray(contentBuffer);
    }

    final byte[] serverSecret = computeServerSecretApplicableAt(creationTime);

    // Verification
    byte[] expectedSign = sign(contentPack(creationTime, random, serverSecret, payload));
    Assert.isTrue(Arrays.equals(expectedSign, currentSignature), "Key verification failure");

    String[] unpack = unpack(Utf8.decode(payload));
    return new TokenDataImpl(creationTime, TokenUtils.getKeyWithTypeAndToken(TYPE, key), unpack[0], unpack[1]);
}

From source file:org.opensafety.hishare.util.implementation.EncryptionImpl.java

public Long hash(String plainText) throws CryptographyException {
    try {/*  ww  w . java  2s.c o m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] cipher = md.digest(plainText.getBytes());
        ByteBuffer converter = ByteBuffer.wrap(cipher);
        return converter.getLong();
    } catch (Exception e) {
        throw new CryptographyException(e.getMessage());
    }
}

From source file:com.act.lcms.v2.fullindex.BuilderTest.java

@Test
public void testExtractTriples() throws Exception {
    // Verify all TMzI triples are stored correctly.
    Map<Long, TMzI> deserializedTriples = new HashMap<>();
    assertEquals("All triples should have entries in the DB", 9,
            fakeDB.getFakeDB().get(ColumnFamilies.ID_TO_TRIPLE).size());
    for (Map.Entry<List<Byte>, byte[]> entry : fakeDB.getFakeDB().get(ColumnFamilies.ID_TO_TRIPLE).entrySet()) {
        Long id = ByteBuffer.wrap(fakeDB.byteListToArray(entry.getKey())).getLong();
        TMzI triple = TMzI.readNextFromByteBuffer(ByteBuffer.wrap(entry.getValue()));
        Float expectedTime = Double.valueOf(TIMES[id.intValue() / 3]).floatValue();
        Double expectedMZ = MZS[id.intValue() / 3][id.intValue() % 3];
        Float expectedIntensity = Double.valueOf(INTENSITIES[id.intValue() / 3][id.intValue() % 3])
                .floatValue();//  w  ww  .  jav a  2s .c  om

        assertEquals("Time matches expected", expectedTime, triple.getTime(), FP_TOLERANCE); // No error expected
        assertEquals("M/z matches expected", expectedMZ, triple.getMz(), FP_TOLERANCE);
        assertEquals("Intensity matches expected", expectedIntensity, triple.getIntensity(), FP_TOLERANCE);

        deserializedTriples.put(id, triple);
    }

    for (Map.Entry<List<Byte>, byte[]> entry : fakeDB.getFakeDB().get(ColumnFamilies.WINDOW_ID_TO_TRIPLES)
            .entrySet()) {
        int windowId = ByteBuffer.wrap(fakeDB.byteListToArray(entry.getKey())).getInt();
        MZWindow window = windowIdsToWindows.get(windowId);

        List<Long> tmziIds = new ArrayList<>(entry.getValue().length / Long.BYTES);
        ByteBuffer valBuffer = ByteBuffer.wrap(entry.getValue());
        while (valBuffer.hasRemaining()) {
            tmziIds.add(valBuffer.getLong());
        }

        for (Long tripleId : tmziIds) {
            TMzI triple = deserializedTriples.get(tripleId);
            assertTrue("Triple m/z falls within range of containing window",
                    triple.getMz() >= window.getMin() && triple.getMz() <= window.getMax());
        }
    }

    for (Map.Entry<List<Byte>, byte[]> entry : fakeDB.getFakeDB().get(ColumnFamilies.TIMEPOINT_TO_TRIPLES)
            .entrySet()) {
        float time = ByteBuffer.wrap(fakeDB.byteListToArray(entry.getKey())).getFloat();

        List<Long> tmziIds = new ArrayList<>(entry.getValue().length / Long.BYTES);
        ByteBuffer valBuffer = ByteBuffer.wrap(entry.getValue());
        while (valBuffer.hasRemaining()) {
            tmziIds.add(valBuffer.getLong());
        }

        for (Long tripleId : tmziIds) {
            TMzI triple = deserializedTriples.get(tripleId);
            assertEquals("Triple time matches key time", time, triple.getTime(), FP_TOLERANCE);
        }
    }
}

From source file:org.apache.hadoop.hdfs.hoss.db.HotStore.java

/**
 * @param objId//from w w  w. ja va2s . co m
 * @return
 */
public float hot(long objId) {
    final ByteBuffer buf = fbs.get((int) objId);
    if (buf == null) {
        LOG.error("Error trying read object " + objId);
    }
    long current = System.currentTimeMillis();
    long createTime = buf.getLong();
    long lastTime = buf.getLong();
    long size = buf.getLong();

    put(objId, createTime, current, size);
    float hotness = ALPHA * sizeHot(size) + BETA * timeHot(current, createTime, lastTime);
    return hotness;
}

From source file:org.apache.hadoop.hdfs.hoss.db.HotStore.java

/**
 * /*from   w  w w .j  a va 2s  .c  o  m*/
 * @param objId
 * @return
 */
public float firstHot(long objId, long size) {
    final ByteBuffer buf = fbs.get((int) objId);
    if (buf == null) {
        LOG.error("Error trying read object " + objId);
    }
    long current = System.currentTimeMillis();
    long createTime = buf.getLong();
    long lastTime = buf.getLong();
    buf.getLong();
    long sizeMB = convertMB(size);
    // update the last access time
    put(objId, createTime, current, sizeMB);
    float hotness = ALPHA * sizeHot(size) + BETA * timeHot(current, createTime, lastTime);
    return hotness;
}

From source file:org.apache.hadoop.hdfs.hoss.db.PathStore.java

/**
 * get path and offset given object id /*from  w  ww.  j  av  a 2s  . c  o m*/
 * @param objId
 * @return
 */
public PathPosition get(long objId) {
    final ByteBuffer buf = fbs.get((int) objId);
    if (buf == null) {
        LOG.error("Error trying read object " + objId);
    }
    String path = StringSerializer.fromBufferToString(buf, PATHWIDTH);
    long offset = buf.getLong();
    PathPosition pp = new PathPosition(path, offset);
    return pp;
}

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

public UUID compose(ByteBuffer bytes) {

    bytes = bytes.slice();/*  w  ww  . ja  v  a 2s  . com*/
    if (bytes.remaining() < 16)
        return new UUID(0, 0);
    return new UUID(bytes.getLong(), bytes.getLong());
}

From source file:org.wso2.carbon.analytics.data.commons.utils.AnalyticsCommonUtils.java

public static Map<String, Object> decodeRecordValues(byte[] data, Set<String> columns)
        throws AnalyticsException {
    /* using LinkedHashMap to retain the column order */
    Map<String, Object> result = new LinkedHashMap<>();
    int type, size;
    String colName;/*from w  ww. j  a v a 2 s.  c o  m*/
    Object value;
    byte[] buff;
    byte boolVal;
    byte[] binData;
    try {
        ByteBuffer buffer = ByteBuffer.wrap(data);
        while (buffer.remaining() > 0) {
            size = buffer.getInt();
            if (size == 0) {
                break;
            }
            buff = new byte[size];
            buffer.get(buff, 0, size);
            colName = new String(buff, StandardCharsets.UTF_8);
            type = buffer.get();
            switch (type) {
            case DATA_TYPE_STRING:
                size = buffer.getInt();
                buff = new byte[size];
                buffer.get(buff, 0, size);
                value = new String(buff, StandardCharsets.UTF_8);
                break;
            case DATA_TYPE_LONG:
                value = buffer.getLong();
                break;
            case DATA_TYPE_DOUBLE:
                value = buffer.getDouble();
                break;
            case DATA_TYPE_BOOLEAN:
                boolVal = buffer.get();
                if (boolVal == BOOLEAN_TRUE) {
                    value = true;
                } else if (boolVal == BOOLEAN_FALSE) {
                    value = false;
                } else {
                    throw new AnalyticsException("Invalid encoded boolean value: " + boolVal);
                }
                break;
            case DATA_TYPE_INTEGER:
                value = buffer.getInt();
                break;
            case DATA_TYPE_FLOAT:
                value = buffer.getFloat();
                break;
            case DATA_TYPE_BINARY:
                size = buffer.getInt();
                binData = new byte[size];
                buffer.get(binData);
                value = binData;
                break;
            case DATA_TYPE_OBJECT:
                size = buffer.getInt();
                binData = new byte[size];
                buffer.get(binData);
                value = deserializeObject(binData);
                break;
            case DATA_TYPE_NULL:
                value = null;
                break;
            default:
                throw new AnalyticsException("Unknown encoded data source type : " + type);
            }
            if (columns == null || columns.contains(colName)) {
                result.put(colName, value);
            }
        }
    } catch (Exception e) {
        throw new AnalyticsException("Error in decoding record values: " + e.getMessage(), e);
    }
    return result;
}