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:Main.java

public static byte[] computeSHA1(ByteBuffer convertme, int offset, int len) {
    int oldp = convertme.position();
    int oldl = convertme.limit();
    try {//from  w w w.  j a  v a  2 s  .com
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        convertme.position(offset);
        convertme.limit(len);
        md.update(convertme);
        return md.digest();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        convertme.limit(oldl);
        convertme.position(oldp);
    }
    return new byte[20];
}

From source file:Main.java

/**
 * Discards data from the buffer up to the first SPS, where {@code data.position()} is interpreted
 * as the length of the buffer./*  www .j  a v  a  2  s. c  o m*/
 * <p>
 * When the method returns, {@code data.position()} will contain the new length of the buffer. If
 * the buffer is not empty it is guaranteed to start with an SPS.
 *
 * @param data Buffer containing start code delimited NAL units.
 */
public static void discardToSps(ByteBuffer data) {
    int length = data.position();
    int consecutiveZeros = 0;
    int offset = 0;
    while (offset + 1 < length) {
        int value = data.get(offset) & 0xFF;
        if (consecutiveZeros == 3) {
            if (value == 1 && (data.get(offset + 1) & 0x1F) == H264_NAL_UNIT_TYPE_SPS) {
                // Copy from this NAL unit onwards to the start of the buffer.
                ByteBuffer offsetData = data.duplicate();
                offsetData.position(offset - 3);
                offsetData.limit(length);
                data.position(0);
                data.put(offsetData);
                return;
            }
        } else if (value == 0) {
            consecutiveZeros++;
        }
        if (value != 0) {
            consecutiveZeros = 0;
        }
        offset++;
    }
    // Empty the buffer if the SPS NAL unit was not found.
    data.clear();
}

From source file:Main.java

/**
 * Discards data from the buffer up to the first SPS, where {@code data.position()} is interpreted
 * as the length of the buffer.//from  ww  w. j av a  2  s.c o  m
 * <p>
 * When the method returns, {@code data.position()} will contain the new length of the buffer. If
 * the buffer is not empty it is guaranteed to start with an SPS.
 *
 * @param data Buffer containing start code delimited NAL units.
 */
public static void discardToSps(ByteBuffer data) {
    int length = data.position();
    int consecutiveZeros = 0;
    int offset = 0;
    while (offset + 1 < length) {
        int value = data.get(offset) & 0xFF;
        if (consecutiveZeros == 3) {
            if (value == 1 && (data.get(offset + 1) & 0x1F) == NAL_UNIT_TYPE_SPS) {
                // Copy from this NAL unit onwards to the start of the buffer.
                ByteBuffer offsetData = data.duplicate();
                offsetData.position(offset - 3);
                offsetData.limit(length);
                data.position(0);
                data.put(offsetData);
                return;
            }
        } else if (value == 0) {
            consecutiveZeros++;
        }
        if (value != 0) {
            consecutiveZeros = 0;
        }
        offset++;
    }
    // Empty the buffer if the SPS NAL unit was not found.
    data.clear();
}

From source file:Main.java

public static final int compareToP(ByteBuffer bb1, ByteBuffer bb2) {
    final int offset1 = bb1.position();
    final int offset2 = bb2.position();
    final byte[] array1 = bb1.array();
    final byte[] array2 = bb2.array();
    final int len1 = bb1.remaining();
    final int len2 = bb2.remaining();
    return compareTo(array1, offset1, len1, array2, offset2, len2);
}

From source file:Main.java

public final static void writeP(final OutputStream out, final ByteBuffer bb) throws IOException {
    out.write(bb.array(), bb.position(), bb.remaining());
}

From source file:Main.java

public static final ByteBuffer wrap(final ByteBuffer bb) {
    return (ByteBuffer) ByteBuffer.wrap(bb.array()).limit(bb.limit()).position(bb.position());
}

From source file:Main.java

/**
 * Like {@link #parseDtsAudioSampleCount(byte[])} but reads from a byte buffer. The buffer
 * position is not modified.//  w  w  w .  j  a v  a 2s.  c  om
 */
public static int parseDtsAudioSampleCount(ByteBuffer data) {
    // See ETSI TS 102 114 subsection 5.4.1.
    int position = data.position();
    int nblks = ((data.get(position + 4) & 0x01) << 6) | ((data.get(position + 5) & 0xFC) >> 2);
    return (nblks + 1) * 32;
}

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:org.hawkular.metrics.core.api.AvailabilityType.java

public static AvailabilityType fromBytes(ByteBuffer bytes) {
    switch (bytes.array()[bytes.position()]) {
    case 0://www . j ava  2  s  .co m
        return UP;
    case 1:
        return DOWN;
    case 2:
        return UNKNOWN;
    default:
        throw new IllegalArgumentException(bytes.array()[0] + " is not a recognized availability type");
    }
}

From source file:Main.java

/**
 * Like {@link #parseDtsAudioSampleCount(byte[])} but reads from a {@link ByteBuffer}. The
 * buffer's position is not modified./*  ww w.ja  v  a2s  .c  o  m*/
 *
 * @param buffer The {@link ByteBuffer} from which to read.
 * @return The number of audio samples represented by the syncframe.
 */
public static int parseDtsAudioSampleCount(ByteBuffer buffer) {
    // See ETSI TS 102 114 subsection 5.4.1.
    int position = buffer.position();
    int nblks = ((buffer.get(position + 4) & 0x01) << 6) | ((buffer.get(position + 5) & 0xFC) >> 2);
    return (nblks + 1) * 32;
}