Example usage for java.nio ByteBuffer position

List of usage examples for java.nio ByteBuffer position

Introduction

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

Prototype

public final int position() 

Source Link

Document

Returns the position of this buffer.

Usage

From source file:com.healthmarketscience.jackcess.impl.office.EncryptionHeader.java

public static EncryptionHeader read(ByteBuffer encProvBuf, Set<CryptoAlgorithm> validCryptoAlgos,
        Set<HashAlgorithm> validHashAlgos) {
    // read length of header
    int headerLen = encProvBuf.getInt();

    // read header (temporarily narrowing buf to header)
    int curLimit = encProvBuf.limit();
    int curPos = encProvBuf.position();
    encProvBuf.limit(curPos + headerLen);
    EncryptionHeader header = new EncryptionHeader(encProvBuf);

    // verify parameters
    if (!validCryptoAlgos.contains(header.getCryptoAlgorithm())) {
        throw new IllegalStateException(header + " crypto algorithm must be one of " + validCryptoAlgos);
    }//from w  w w  .  j a  v a2 s  . c o m

    if (!validHashAlgos.contains(header.getHashAlgorithm())) {
        throw new IllegalStateException(header + " hash algorithm must be one of " + validHashAlgos);
    }

    int keySize = header.getKeySize();
    if (!header.getCryptoAlgorithm().isValidKeySize(keySize)) {
        throw new IllegalStateException(header + " key size is outside allowable range");
    }
    if ((keySize % 8) != 0) {
        throw new IllegalStateException(header + " key size must be multiple of 8");
    }

    // move to after header
    encProvBuf.limit(curLimit);
    encProvBuf.position(curPos + headerLen);

    return header;
}

From source file:com.glaf.core.util.ByteBufferUtils.java

public static String bytesToHex(ByteBuffer bytes) {
    final int offset = bytes.position();
    final int size = bytes.remaining();
    final char[] c = new char[size * 2];
    for (int i = 0; i < size; i++) {
        final int bint = bytes.get(i + offset);
        c[i * 2] = Hex.byteToChar[(bint & 0xf0) >> 4];
        c[1 + i * 2] = Hex.byteToChar[bint & 0x0f];
    }//  w w w.j  a v  a 2s . c o  m
    return Hex.wrapCharArray(c);
}

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * flush?????limit,??position, position0 ,
 * ByteBuffer.flip()/* ww  w  . ja va 2 s  .c o m*/
 * 
 * @param buffer
 * @param position
 */
public static void flipToFlush(ByteBuffer buffer, int position) {
    buffer.limit(buffer.position());
    buffer.position(position);
}

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * fill?//from w  w  w  . j  av  a2 s  .  c o m
 * 
 * @param buffer
 * @return
 */
public static int flipToFill(ByteBuffer buffer) {
    int position = buffer.position();
    int limit = buffer.limit();
    // flush??fill?
    if (position == limit) {
        buffer.position(0);
        buffer.limit(buffer.capacity());
        return 0;
    }
    // ?limit equal capacity,?
    int capacity = buffer.capacity();
    if (limit == capacity) {
        buffer.compact();
        return 0;
    }
    // ??
    buffer.position(limit);
    buffer.limit(capacity);
    return position;
}

From source file:com.icloud.framework.core.util.FBUtilities.java

public static int byteBufferToInt(ByteBuffer bytes) {
    if (bytes.remaining() < 4) {
        throw new IllegalArgumentException("An integer must be 4 bytes in size.");
    }/*from   ww  w  .  j  a  v  a2 s.c o m*/
    int n = 0;
    for (int i = 0; i < 4; ++i) {
        n <<= 8;
        n |= bytes.array()[bytes.position() + bytes.arrayOffset() + i] & 0xFF;
    }
    return n;
}

From source file:com.icloud.framework.core.util.FBUtilities.java

public static void writeShortByteArray(ByteBuffer name, DataOutput out) {
    int length = name.remaining();
    assert 0 <= length && length <= MAX_UNSIGNED_SHORT;
    try {/*from w  w  w .  j a  va2s . c  o  m*/
        out.writeByte((length >> 8) & 0xFF);
        out.writeByte(length & 0xFF);
        out.write(name.array(), name.position() + name.arrayOffset(), name.remaining());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.easemob.dataexport.utils.JsonUtils.java

public static Object fromByteBuffer(ByteBuffer byteBuffer, Class<?> clazz) {
    if ((byteBuffer == null) || !byteBuffer.hasRemaining()) {
        return null;
    }//w w  w  .j ava2  s. co m
    if (clazz == null) {
        clazz = Object.class;
    }

    Object obj = null;
    try {
        obj = smileMapper.readValue(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(),
                byteBuffer.remaining(), clazz);
    } catch (Exception e) {
        LOG.error("Error parsing SMILE bytes", e);
    }
    return obj;
}

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * ByteBuffer adaptation of org.apache.commons.lang3.ArrayUtils.lastIndexOf
 * method//from  ww w  . j  a  va2 s . c  o m
 * 
 * @param buffer
 *            the array to traverse for looking for the object, may be
 *            <code>null</code>
 * @param valueToFind
 *            the value to find
 * @param startIndex
 *            the start index (i.e. BB position) to travers backwards from
 * @return the last index (i.e. BB position) of the value within the array
 *         [between buffer.position() and buffer.limit()]; <code>-1</code>
 *         if not found.
 */
public static int lastIndexOf(ByteBuffer buffer, byte valueToFind, int startIndex) {
    assert buffer != null;

    if (startIndex < buffer.position()) {
        return -1;
    } else if (startIndex >= buffer.limit()) {
        startIndex = buffer.limit() - 1;
    }

    for (int i = startIndex; i >= buffer.position(); i--) {
        if (valueToFind == buffer.get(i))
            return i;
    }

    return -1;
}

From source file:Main.java

/**
 * Compute the HMAC with SHA-256 of data, as defined in
 * http://tools.ietf.org/html/rfc2104#section-2 .
 * @param key The key byte array./*from w  w  w .j a v  a2  s .co  m*/
 * @param data The input byte buffer. This does not change the position.
 * @return The HMAC result.
 */
public static byte[] computeHmacWithSha256(byte[] key, ByteBuffer data) {
    final String algorithm = "HmacSHA256";
    Mac mac;
    try {
        mac = Mac.getInstance(algorithm);
    } catch (NoSuchAlgorithmException ex) {
        // Don't expect this to happen.
        throw new Error("computeHmac: " + algorithm + " is not supported: " + ex.getMessage());
    }

    try {
        mac.init(new SecretKeySpec(key, algorithm));
    } catch (InvalidKeyException ex) {
        // Don't expect this to happen.
        throw new Error("computeHmac: Can't init " + algorithm + " with key: " + ex.getMessage());
    }
    int savePosition = data.position();
    mac.update(data);
    data.position(savePosition);
    return mac.doFinal();
}

From source file:com.google.flatbuffers.Table.java

/**
 * Check if a {@link ByteBuffer} contains a file identifier.
 *
 * @param bb A {@code ByteBuffer} to check if it contains the identifier
 * `ident`./*from  www  .j a  v a 2  s .c o  m*/
 * @param ident A `String` identifier of the FlatBuffer file.
 * @return True if the buffer contains the file identifier
 */
protected static boolean __has_identifier(ByteBuffer bb, String ident) {
    if (ident.length() != FILE_IDENTIFIER_LENGTH)
        throw new AssertionError("FlatBuffers: file identifier must be length " + FILE_IDENTIFIER_LENGTH);
    for (int i = 0; i < FILE_IDENTIFIER_LENGTH; i++) {
        if (ident.charAt(i) != (char) bb.get(bb.position() + SIZEOF_INT + i))
            return false;
    }
    return true;
}