Example usage for java.nio ByteBuffer array

List of usage examples for java.nio ByteBuffer array

Introduction

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

Prototype

public final byte[] array() 

Source Link

Document

Returns the byte array which this buffer is based on, if there is one.

Usage

From source file:com.networknt.light.util.HashUtil.java

public static String generateUUID() {
    UUID id = UUID.randomUUID();
    ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(id.getMostSignificantBits());
    bb.putLong(id.getLeastSignificantBits());
    Base64 base64 = new Base64();
    return base64.encodeBase64URLSafeString(bb.array());
}

From source file:com.silverpeas.ical.StringUtils.java

static byte[] encodeArray(char[] chars, Charset encoding) throws CharacterCodingException {
    if (CharEncoding.US_ASCII.equals(encoding.name())) {
        byte[] array = new byte[chars.length];
        for (int i = 0; i < array.length; i++) {
            array[i] = (byte) chars[i];
        }/* ww  w  .  j a v a 2s . c  o  m*/
        return array;
    }
    ByteBuffer buffer = encoding.newEncoder().encode(CharBuffer.wrap(chars));
    byte[] array = new byte[buffer.limit()];
    System.arraycopy(buffer.array(), 0, array, 0, array.length);
    return array;
}

From source file:com.astamuse.asta4d.web.util.SecureIdGenerator.java

public static String createEncryptedURLSafeId() {
    try {//from www. j a va 2 s.c o m
        byte[] idBytes = IdGenerator.createIdBytes();

        ByteBuffer bb = ByteBuffer.allocate(idBytes.length + 4);
        bb.put(idBytes);

        // add random salt
        bb.putInt(sr.nextInt());

        MessageDigest crypt = MessageDigest.getInstance("SHA-1");
        return Base64.encodeBase64URLSafeString(crypt.digest(bb.array()));

    } catch (NoSuchAlgorithmException e) {
        // impossible
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Compares two bitmaps and gives the percentage of similarity
 *
 * @param bitmap1 input bitmap 1/*from   w w  w .  j  av  a2s .co  m*/
 * @param bitmap2 input bitmap 2
 * @return a value between 0.0 to 1.0 . Note the method will return 0.0 if either of bitmaps are null nor of same size.
 *
 */
public static float compareEquivalance(Bitmap bitmap1, Bitmap bitmap2) {

    if (bitmap1 == null || bitmap2 == null || bitmap1.getWidth() != bitmap2.getWidth()
            || bitmap1.getHeight() != bitmap2.getHeight()) {
        return 0f;
    }

    ByteBuffer buffer1 = ByteBuffer.allocate(bitmap1.getHeight() * bitmap1.getRowBytes());
    bitmap1.copyPixelsToBuffer(buffer1);

    ByteBuffer buffer2 = ByteBuffer.allocate(bitmap2.getHeight() * bitmap2.getRowBytes());
    bitmap2.copyPixelsToBuffer(buffer2);

    byte[] array1 = buffer1.array();
    byte[] array2 = buffer2.array();

    int len = array1.length; // array1 and array2 will be of some length.
    int count = 0;

    for (int i = 0; i < len; i++) {
        if (array1[i] == array2[i]) {
            count++;
        }
    }

    return ((float) (count)) / len;
}

From source file:org.brekka.phalanx.core.services.impl.AbstractCryptoService.java

private static byte[] encodePrivateKey(InternalPrivateKeyToken pkt) {
    int profileId = pkt.getKeyPair().getPrivateKey().getProfile();
    byte[] data = pkt.getPrivateKey().getEncoded();
    ByteBuffer buffer = ByteBuffer.allocate(PK_MAGIC_MARKER.length + 4 + data.length).put(PK_MAGIC_MARKER)
            .putInt(profileId).put(data);
    return buffer.array();
}

From source file:com.networknt.utility.Util.java

/**
 * Generate UUID across the entire app and it is used for correlationId.
 *
 * @return String correlationId/*from www .  jav  a 2s .co  m*/
 */
public static String getUUID() {
    UUID id = UUID.randomUUID();
    ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(id.getMostSignificantBits());
    bb.putLong(id.getLeastSignificantBits());
    return Base64.encodeBase64URLSafeString(bb.array());
}

From source file:ezbake.deployer.utilities.VersionHelper.java

public static String getVersionFromArtifact(ByteBuffer artifact) throws IOException {
    String versionNumber = null;//w  ww  .  j  a  v a2  s . c o  m
    try (CompressorInputStream uncompressedInput = new GzipCompressorInputStream(
            new ByteArrayInputStream(artifact.array()))) {
        ArchiveInputStream input = new TarArchiveInputStream(uncompressedInput);
        TarArchiveEntry entry;
        while ((entry = (TarArchiveEntry) input.getNextEntry()) != null) {
            if (!entry.isDirectory() && entry.getName().endsWith(VERSION_FILE)) {
                versionNumber = IOUtils.toString(input, Charsets.UTF_8).trim();
                break;
            }
        }
    }
    return versionNumber;
}

From source file:com.tesora.dve.db.mysql.portal.protocol.MSPAuthenticateV10MessageMessage.java

public static byte[] computeSecurePassword(String password, String salt) {
    byte[] sha1password = DigestUtils.sha1(password);
    byte[] seedbytes = salt.getBytes();
    ByteBuffer bb = ByteBuffer.allocate(sha1password.length + seedbytes.length);
    bb.put(seedbytes);// ww w. ja  v  a2  s.  co m
    bb.put(DigestUtils.sha1(sha1password));
    byte[] sha1parttwo = DigestUtils.sha1(bb.array());
    byte[] securePassword = new byte[sha1password.length];
    for (int i = 0; i < securePassword.length; ++i)
        securePassword[i] = (byte) (sha1password[i] ^ sha1parttwo[i]);
    return securePassword;
}

From source file:com.mcxiaoke.next.http.entity.mime.AbstractMultipartForm.java

private static ByteArrayBuffer encode(final Charset charset, final String string) {
    final ByteBuffer encoded = charset.encode(CharBuffer.wrap(string));
    final ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining());
    bab.append(encoded.array(), encoded.position(), encoded.remaining());
    return bab;//from  w w  w.ja v  a2s . c  o m
}

From source file:com.sonymobile.android.media.internal.Util.java

public static MediaCrypto createMediaCrypto(MediaFormat format) throws MediaCryptoException {
    if (format.containsKey(MetaData.KEY_PLAYREADY_SESSIONID)) {
        ByteBuffer buffer = format.getByteBuffer(MetaData.KEY_PLAYREADY_SESSIONID);
        if (buffer != null) {
            return new MediaCrypto(DrmUUID.PLAY_READY, buffer.array());
        }/*from   ww w .j  a v  a 2s.  c o m*/
    } else if (format.containsKey(MetaData.KEY_MARLIN_JSON)) {
        byte[] marlinJson;
        try {
            marlinJson = format.getString(MetaData.KEY_MARLIN_JSON).getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            if (LOGS_ENABLED)
                Log.e(TAG, "Unsupported encoding", e);
            return null;
        }

        return new MediaCrypto(DrmUUID.MARLIN, marlinJson);
    }

    return null;
}