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.sm.store.Utils.java

public static void addBlockLong(Value value, Value block) {
    ByteBuffer buf = ByteBuffer.wrap((byte[]) value.getData());
    //block value is integer
    long k = buf.getLong() + ByteBuffer.wrap((byte[]) block.getData()).getInt();
    byte[] data = putLong(k);
    value.setData(data);//from w w  w  .  ja v a 2 s.c o  m
    value.setVersion(value.getVersion() + 1);
}

From source file:org.linguafranca.pwdb.kdbx.dom.DomHelper.java

static UUID uuidFromBase64(String base64) {
    // round the houses for Android
    byte[] buffer = Base64.decodeBase64(base64.getBytes());
    ByteBuffer b = ByteBuffer.wrap(buffer);
    return new UUID(b.getLong(), b.getLong(8));
}

From source file:com.scf.utils.UUIDUtilies.java

public static String decodeBase64Uuid(String compressedUuid) {

    byte[] byUuid = Base64.decodeBase64(compressedUuid);

    ByteBuffer bb = ByteBuffer.wrap(byUuid);
    UUID uuid = new UUID(bb.getLong(), bb.getLong());
    return uuid.toString();
}

From source file:com.sm.message.Header.java

public static Header toHeader(byte[] data) {
    ByteBuffer buf = ByteBuffer.wrap(data);
    byte release = buf.get();
    long version = buf.getLong();
    long node = buf.getLong();
    byte[] n = new byte[data.length - FIRST];
    buf.get(n);/*from ww  w . ja v a  2s.  com*/
    try {
        String name = new String(n, "UTF-8");
        return new Header(name, version, release, node);
    } catch (UnsupportedEncodingException ex) {
        throw new RuntimeException(ex.getMessage());
    }

}

From source file:org.grouplens.grapht.util.ClassProxy.java

/**
 * Compute a checksum for a class. These checksums are used to see if a class has changed
 * its definition since being serialized.
 * <p>/*from   www .jav a  2s  . c  o  m*/
 * The checksum used here is not cryptographically strong. It is intended only as a sanity
 * check to detect incompatible serialization, not to robustly prevent tampering. The
 * checksum algorithm currently is to compute an MD5 checksum over class member signatures
 * and XOR the lower and upper halves of the checksum.
 * </p>
 *
 * @param type The class to checksum.
 * @return The
 */
private static long checksumClass(Class<?> type) {
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("JVM does not support MD5", e);
    }
    checksumClass(type, digest);

    ByteBuffer buf = ByteBuffer.wrap(digest.digest());
    long l1 = buf.getLong();
    long l2 = buf.getLong();
    return l1 ^ l2;
}

From source file:ConversionUtil.java

public static long convertToLong(byte[] array) {
    ByteBuffer buffer = ByteBuffer.wrap(array);
    return buffer.getLong();
    /*/* ww w.  j  av  a  2 s  .c om*/
    long value = 0;
    for (int i =0;i<array.length; ++i) {
    int offset = (array.length -i-1) *8;
    value += (array[i] << offset);
    // bytes[i] = (byte)((value & (0xff << offset)) >>> offset);
    }
    return value;
     */
}

From source file:org.hobbit.core.rabbit.RabbitMQUtils.java

/**
 * Reads a long value from the given byte array.
 *
 * @param data//ww w  .  j ava 2 s  . c  om
 *            a serialized long value
 * @param offset
 *            position at which the parsing will start
 * @param length
 *            number of bytes that should be parsed (should equal
 *            {@link Long#BYTES})
 * @return the value read from the array
 */
public static long readLong(byte[] data, int offset, int length) {
    if (length < Long.BYTES) {
        LOGGER.error("Cant read a long value from {} bytes. Returning 0.", length);
        return 0;
    }
    ByteBuffer buffer = ByteBuffer.wrap(data, offset, length);
    return buffer.getLong();
}

From source file:com.openteach.diamond.network.waverider.command.Command.java

/**
 * ByteBuffer??//from  www  .ja  v  a  2  s .  c  o m
 * @param buffer
 * @return
 */
public static Command unmarshall(ByteBuffer buffer) {
    if (buffer.remaining() < getHeaderSize()) {
        throw new RuntimeException("Wrong command.");
    }
    Command command = new Command();
    command.setType(buffer.getLong());
    command.setLength(buffer.getInt());
    // (payLoad)array()
    command.setPayLoad(buffer.slice());
    return command;
}

From source file:com.codelanx.codelanxlib.util.auth.UUIDFetcher.java

/**
 * Returns a {@link UUID} from a byte array
 * //from   w w w .  ja  v a  2  s . c o  m
 * @since 0.0.1
 * @version 0.1.0
 * 
 * @param array The byte array to convert
 * @return The new {@link UUID} object
 * @throws IllegalArgumentException if the array length is not 16
 */
public static UUID fromBytes(byte[] array) {
    Validate.isTrue(array.length == 16, "Illegal byte array length: " + array.length);
    ByteBuffer byteBuffer = ByteBuffer.wrap(array);
    long mostSignificant = byteBuffer.getLong();
    long leastSignificant = byteBuffer.getLong();
    return new UUID(mostSignificant, leastSignificant);
}

From source file:ca.psiphon.ploggy.Robohash.java

public static Bitmap getRobohash(Context context, boolean cacheCandidate, byte[] data)
        throws Utils.ApplicationError {
    try {//ww  w .  jav a  2  s. com
        MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
        byte[] digest = sha1.digest(data);

        String key = Utils.formatFingerprint(digest);
        Bitmap cachedBitmap = mCache.get(key);
        if (cachedBitmap != null) {
            return cachedBitmap;
        }

        ByteBuffer byteBuffer = ByteBuffer.wrap(digest);
        byteBuffer.order(ByteOrder.BIG_ENDIAN);
        // TODO: SecureRandom SHA1PRNG (but not LinuxSecureRandom)
        Random random = new Random(byteBuffer.getLong());

        AssetManager assetManager = context.getAssets();

        if (mConfig == null) {
            mConfig = new JSONObject(loadAssetToString(assetManager, CONFIG_FILENAME));
        }

        int width = mConfig.getInt("width");
        int height = mConfig.getInt("height");

        JSONArray colors = mConfig.getJSONArray("colors");
        JSONArray parts = colors.getJSONArray(random.nextInt(colors.length()));

        Bitmap robotBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas robotCanvas = new Canvas(robotBitmap);

        for (int i = 0; i < parts.length(); i++) {
            JSONArray partChoices = parts.getJSONArray(i);
            String selection = partChoices.getString(random.nextInt(partChoices.length()));
            Bitmap partBitmap = loadAssetToBitmap(assetManager, selection);
            Rect rect = new Rect(0, 0, width, height);
            Paint paint = new Paint();
            paint.setAlpha(255);
            robotCanvas.drawBitmap(partBitmap, rect, rect, paint);
            partBitmap.recycle();
        }

        if (cacheCandidate) {
            mCache.set(key, robotBitmap);
        }

        return robotBitmap;

    } catch (IOException e) {
        throw new Utils.ApplicationError(LOG_TAG, e);
    } catch (JSONException e) {
        throw new Utils.ApplicationError(LOG_TAG, e);
    } catch (NoSuchAlgorithmException e) {
        throw new Utils.ApplicationError(LOG_TAG, e);
    }
}