Example usage for java.nio ByteBuffer remaining

List of usage examples for java.nio ByteBuffer remaining

Introduction

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

Prototype

public final int remaining() 

Source Link

Document

Returns the number of remaining elements in this buffer, that is limit - position .

Usage

From source file:Main.java

public static void readFully(ReadableByteChannel paramReadableByteChannel, ByteBuffer paramByteBuffer)
        throws IOException {
    readFully(paramReadableByteChannel, paramByteBuffer, paramByteBuffer.remaining());
}

From source file:Main.java

/**
 * Returns an MD-5 digest of the database encryption password.
 * <blockquote>This algorithm performs an MD-5 hash on a UTF-8 representation of the password.</blockquote>
 *
 * @param pass The plain encryption password
 *
 * @return The database encryption password digest
 *
 * @throws NoSuchAlgorithmException If the platform doesn't support MD-5
 *//*from  ww w .  java2s. com*/
public static byte[] passToDigest(char[] pass) throws NoSuchAlgorithmException {
    // FIXME: Enhance security for this method
    Charset cs = Charset.forName("UTF-8");
    MessageDigest md = MessageDigest.getInstance("MD5");
    ByteBuffer bbuf = cs.encode(CharBuffer.wrap(pass));
    byte[] bpass = Arrays.copyOf(bbuf.array(), bbuf.remaining());

    return md.digest(bpass);
}

From source file:Main.java

public static boolean isPredessorEqualsP(ByteBuffer bb1, ByteBuffer bb2) {
    return isPredessorEquals(bb1, bb1.position(), bb1.remaining(), bb2, bb2.position(), bb2.remaining());
}

From source file:Main.java

private static String formatBytes(byte[] data, boolean unsigned) {
    ByteBuffer bb = ByteBuffer.wrap(data);

    StringBuilder sb = new StringBuilder(bb.capacity() * 3);

    while (bb.remaining() > 0) {
        if (unsigned) {
            sb.append(bb.get() & 0xff);
        } else {/*from w w  w .  java  2 s  .  c o  m*/
            sb.append(bb.get());
        }

        sb.append(',');
        sb.append('\n');
    }

    return sb.toString();
}

From source file:Main.java

/**
 * Converts a char array into a byte array using the default character set.
 * /*from   ww w .  j  a v a 2 s.  co  m*/
 * @param chars
 *            The source characters.
 * @param charsetName
 *            The character set to use.
 * @return The result bytes.
 */
public static byte[] toByteArray(char[] chars, String charsetName) {
    java.nio.CharBuffer cb = java.nio.CharBuffer.wrap(chars);
    java.nio.ByteBuffer bb = java.nio.charset.Charset.forName(charsetName).encode(cb);
    byte[] r = new byte[bb.remaining()];
    bb.get(r);
    return r;
}

From source file:Main.java

public static final byte[] md5P(final ByteBuffer bb) {
    final byte[] array = bb.array();
    final int position = bb.position();
    final int len = bb.remaining();
    return md5(array, position, len);
}

From source file:Main.java

public static final boolean isSuccessorP(final ByteBuffer bb1, final ByteBuffer bb2) {
    return isSuccessor(bb1, bb1.position(), bb1.remaining(), bb2, bb2.position(), bb2.remaining());
}

From source file:Main.java

public static ByteBuffer copyBinary(final ByteBuffer orig) {
    if (orig == null) {
        return null;
    }/*from w ww. j  a v a2 s. com*/
    ByteBuffer copy = ByteBuffer.wrap(new byte[orig.remaining()]);
    if (orig.hasArray()) {
        System.arraycopy(orig.array(), orig.arrayOffset() + orig.position(), copy.array(), 0, orig.remaining());
    } else {
        orig.slice().get(copy.array());
    }

    return copy;
}

From source file:de.zalando.spring.cloud.config.aws.kms.KmsTextEncryptor.java

private static String extractString(final ByteBuffer bb) {
    if (bb.hasRemaining()) {
        final byte[] bytes = new byte[bb.remaining()];
        bb.get(bytes, bb.arrayOffset(), bb.remaining());
        return new String(bytes);
    } else {// www.  j  ava2 s  .  com
        return EMPTY_STRING;
    }
}

From source file:Main.java

public static boolean wrapsFullArray(ByteBuffer byteBuffer) {
    return byteBuffer.hasArray() && byteBuffer.position() == 0 && byteBuffer.arrayOffset() == 0
            && byteBuffer.remaining() == byteBuffer.capacity();
}