Example usage for java.nio ByteBuffer putLong

List of usage examples for java.nio ByteBuffer putLong

Introduction

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

Prototype

public abstract ByteBuffer putLong(long value);

Source Link

Document

Writes the given long to the current position and increases the position by 8.

Usage

From source file:org.apache.cassandra.Util.java

public static ByteBuffer getBytes(long v) {
    byte[] bytes = new byte[8];
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    bb.putLong(v);
    bb.rewind();// w ww.j  a  va 2s. c o  m
    return bb;
}

From source file:burstcoin.jminer.core.round.Round.java

private static int calcScoopNumber(long blockNumber, byte[] generationSignature) {
    if (blockNumber > 0 && generationSignature != null) {
        ByteBuffer buf = ByteBuffer.allocate(32 + 8);
        buf.put(generationSignature);/*from  w w  w  . java2  s . com*/
        buf.putLong(blockNumber);

        // generate new scoop number
        Shabal256 md = new Shabal256();
        md.update(buf.array());

        BigInteger hashnum = new BigInteger(1, md.digest());
        return hashnum.mod(BigInteger.valueOf(MiningPlot.SCOOPS_PER_PLOT)).intValue();
    }
    return 0;
}

From source file:org.energy_home.jemma.javagal.layers.data.implementations.Utils.DataManipulation.java

/**
 * Converts a {@code long} to a {@code byte[]}. A long is composed of eight
 * bytes. Writes eight bytes containing the given long value, in the current
 * byte order. Numbering them from 0 (the most important) to 7 (the least
 * important), the resulting array will have all them placed in the same
 * position./*from w  ww . j av  a  2 s.co m*/
 * 
 * @return the converted list.
 * @param x
 *            the long to convert
 * @return the resulting array.
 */
public static byte[] longToBytes(long x) {
    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.order(ByteOrder.BIG_ENDIAN);
    buffer.putLong(x);
    return buffer.array();
}

From source file:org.midonet.cluster.rest_api.models.Route.java

public static UUID idOf(org.midonet.midolman.layer3.Route route) {
    ByteBuffer buffer = ByteBuffer.allocate(50);
    buffer.putInt(route.dstNetworkAddr);
    buffer.putInt(route.srcNetworkAddr);
    buffer.putInt(route.nextHopGateway);
    buffer.putInt(route.weight);//from ww w. j  a v  a 2  s .co m
    buffer.put((byte) (route.dstNetworkLength & 0xFF));
    buffer.put((byte) (route.srcNetworkLength & 0xFF));
    buffer.putLong(route.nextHopPort.getMostSignificantBits());
    buffer.putLong(route.nextHopPort.getLeastSignificantBits());
    buffer.putLong(route.routerId.getMostSignificantBits());
    buffer.putLong(route.routerId.getLeastSignificantBits());
    return UUID.nameUUIDFromBytes(buffer.array());
}

From source file:org.apache.cassandra.db.index.sasi.disk.TokenTreeTest.java

private static DecoratedKey dk(Long token) {
    ByteBuffer buf = ByteBuffer.allocate(8);
    buf.putLong(token);
    buf.flip();/*from   ww w  .jav  a  2s .com*/
    Long hashed = MurmurHash.hash2_64(buf, buf.position(), buf.remaining(), 0);
    return new DecoratedKey(new LongToken(hashed), buf);
}

From source file:org.texai.util.ByteUtils.java

/** Returns a 8-byte array built from a long.
 *
 * @param number the long number to convert
 * @return a byte array//from w  ww  . ja v  a 2s. c o  m
 */
public static byte[] toBytes(final long number) {
    final ByteBuffer byteBuffer = ByteBuffer.allocate(8);
    byteBuffer.putLong(number);
    return byteBuffer.array();
}

From source file:com.github.nlloyd.hornofmongo.util.BSONizer.java

@SuppressWarnings("deprecation")
public static Object convertBSONtoJS(MongoScope mongoScope, Object bsonObject) {
    Object jsObject = null;/*from w  w  w . j ava2 s  .c  o m*/
    if (bsonObject instanceof List<?>) {
        List<?> bsonList = (List<?>) bsonObject;
        Scriptable jsArray = (Scriptable) MongoRuntime.call(new NewInstanceAction(mongoScope, bsonList.size()));

        int index = 0;
        for (Object bsonEntry : bsonList) {
            ScriptableObject.putProperty(jsArray, index, convertBSONtoJS(mongoScope, bsonEntry));
            index++;
        }

        jsObject = jsArray;
    } else if (bsonObject instanceof BSONObject) {
        Scriptable jsObj = (Scriptable) MongoRuntime.call(new NewInstanceAction(mongoScope));
        BSONObject bsonObj = (BSONObject) bsonObject;

        for (String key : bsonObj.keySet()) {
            Object value = convertBSONtoJS(mongoScope, bsonObj.get(key));
            MongoRuntime.call(new JSPopulatePropertyAction(jsObj, key, value));
        }
        jsObject = jsObj;
    } else if (bsonObject instanceof Symbol) {
        jsObject = ((Symbol) bsonObject).getSymbol();
    } else if (bsonObject instanceof Date) {
        jsObject = MongoRuntime.call(
                new NewInstanceAction(mongoScope, "Date", new Object[] { ((Date) bsonObject).getTime() }));
    } else if (bsonObject instanceof Pattern) {
        Pattern regex = (Pattern) bsonObject;
        String source = regex.pattern();
        String options = Bytes.regexFlags(regex.flags());
        jsObject = MongoRuntime
                .call(new NewInstanceAction(mongoScope, "RegExp", new Object[] { source, options }));
    } else if (bsonObject instanceof org.bson.types.ObjectId) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "ObjectId"));
        ((ObjectId) jsObject).setRealObjectId((org.bson.types.ObjectId) bsonObject);
    } else if (bsonObject instanceof org.bson.types.MinKey) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "MinKey"));
    } else if (bsonObject instanceof org.bson.types.MaxKey) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "MaxKey"));
    } else if (bsonObject instanceof com.mongodb.DBRef) {
        com.mongodb.DBRef dbRef = (com.mongodb.DBRef) bsonObject;
        Object id = convertBSONtoJS(mongoScope, dbRef.getId());
        jsObject = MongoRuntime.call(
                new NewInstanceAction(mongoScope, "DBRef", new Object[] { dbRef.getCollectionName(), id }));
    } else if (bsonObject instanceof BSONTimestamp) {
        BSONTimestamp bsonTstamp = (BSONTimestamp) bsonObject;
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "Timestamp",
                new Object[] { bsonTstamp.getTime(), bsonTstamp.getInc() }));
    } else if (bsonObject instanceof Long) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "NumberLong"));
        ((NumberLong) jsObject).setRealLong((Long) bsonObject);
    } else if (bsonObject instanceof Integer) {
        jsObject = Double.valueOf((Integer) bsonObject);
    } else if (bsonObject instanceof Code) {
        jsObject = ((Code) bsonObject).getCode();
    } else if (bsonObject instanceof byte[]) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "BinData"));
        ((BinData) jsObject).setValues(0, (byte[]) bsonObject);
    } else if (bsonObject instanceof Binary) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "BinData"));
        ((BinData) jsObject).setValues(((Binary) bsonObject).getType(), ((Binary) bsonObject).getData());
    } else if (bsonObject instanceof UUID) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "BinData"));
        UUID uuid = (UUID) bsonObject;
        ByteBuffer dataBuffer = ByteBuffer.allocate(16);
        // mongodb wire protocol is little endian
        dataBuffer.order(ByteOrder.LITTLE_ENDIAN);
        dataBuffer.putLong(uuid.getMostSignificantBits());
        dataBuffer.putLong(uuid.getLeastSignificantBits());
        ((BinData) jsObject).setValues(BSON.B_UUID, dataBuffer.array());
    } else {
        jsObject = bsonObject;
    }

    return jsObject;
}

From source file:org.apache.cassandra.index.sasi.disk.TokenTreeTest.java

private static DecoratedKey dk(Long token) {
    ByteBuffer buf = ByteBuffer.allocate(8);
    buf.putLong(token);
    buf.flip();/*from  w  w  w .  ja v  a 2 s  .  co  m*/
    Long hashed = MurmurHash.hash2_64(buf, buf.position(), buf.remaining(), 0);
    return new BufferDecoratedKey(new Murmur3Partitioner.LongToken(hashed), buf);
}

From source file:Main.java

private static byte[] encodeUrnUuid(String urn, int position, ByteBuffer bb) {
    String uuidString = urn.substring(position, urn.length());
    UUID uuid;/*  w  ww .j av a  2s.  c om*/
    try {
        uuid = UUID.fromString(uuidString);
    } catch (IllegalArgumentException e) {
        //Log.w(TAG, "encodeUrnUuid invalid urn:uuid format - " + urn);
        return null;
    }
    // UUIDs are ordered as byte array, which means most significant first
    bb.order(ByteOrder.BIG_ENDIAN);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());
    return byteBufferToArray(bb);
}

From source file:net.servicestack.client.Utils.java

public static byte[] toGuidBytes(UUID theUuid) {
    ByteBuffer first8 = ByteBuffer.allocate(8);
    first8.putLong(theUuid.getMostSignificantBits());
    first8.rewind();// ww w .j  av  a 2  s.  co  m

    byte[] first4 = new byte[4];
    first8.get(first4);
    reverse(first4);

    byte[] second2 = new byte[2];
    first8.get(second2);
    reverse(second2);

    byte[] third2 = new byte[2];
    first8.get(third2);
    reverse(third2);

    ByteBuffer converted16 = ByteBuffer.allocate(16).put(first4).put(second2).put(third2);

    ByteBuffer last8 = ByteBuffer.allocate(8);
    last8.putLong(theUuid.getLeastSignificantBits());
    last8.rewind();

    converted16.put(last8);

    return converted16.array();
}