Example usage for java.nio ByteBuffer duplicate

List of usage examples for java.nio ByteBuffer duplicate

Introduction

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

Prototype

public abstract ByteBuffer duplicate();

Source Link

Document

Returns a duplicated buffer that shares its content with this buffer.

Usage

From source file:com.netflix.astyanax.thrift.ThriftUtils.java

private static String base64Encode(ByteBuffer bb) {
    if (bb == null) {
        return "";
    }/*w w  w.  ja  va 2s .  c om*/
    byte[] nbb = new byte[bb.remaining()];
    bb.duplicate().get(nbb, 0, bb.remaining());
    return Base64.encodeBase64String(nbb);
}

From source file:io.mycat.util.ByteBufferUtil.java

public static ByteBuffer readBytes(ByteBuffer bb, int length) {
    ByteBuffer copy = bb.duplicate();
    copy.limit(copy.position() + length);
    bb.position(bb.position() + length);
    return copy;/*from   w  w  w  .j  a v  a 2 s  .  c  o  m*/
}

From source file:com.healthmarketscience.jackcess.impl.PageChannel.java

/**
 * @return a duplicate of the current buffer narrowed to the given position
 *         and limit.  mark will be set at the current position.
 *//*from  ww  w. ja va2 s.  c  o  m*/
public static ByteBuffer narrowBuffer(ByteBuffer buffer, int position, int limit) {
    return (ByteBuffer) buffer.duplicate().order(buffer.order()).clear().limit(limit).position(position).mark();
}

From source file:io.mycat.util.ByteBufferUtil.java

public static InputStream inputStream(ByteBuffer bytes) {
    final ByteBuffer copy = bytes.duplicate();

    return new InputStream() {
        public int read() {
            if (!copy.hasRemaining()) {
                return -1;
            }//w  w  w  .  j  av a 2  s. c o m

            return copy.get() & 0xFF;
        }

        @Override
        public int read(byte[] bytes, int off, int len) {
            if (!copy.hasRemaining()) {
                return -1;
            }

            len = Math.min(len, copy.remaining());
            copy.get(bytes, off, len);
            return len;
        }

        @Override
        public int available() {
            return copy.remaining();
        }
    };
}

From source file:net.darkmist.alib.io.BufferUtil.java

public static boolean isAll(ByteBuffer buf, byte b) {
    buf = buf.duplicate();

    while (buf.hasRemaining())
        if (buf.get() != b)
            return false;
    return true;/*  ww  w.  j a va 2 s  . c o  m*/
}

From source file:net.darkmist.alib.io.BufferUtil.java

public static byte[] asBytes(ByteBuffer buf) {
    buf = buf.duplicate();
    /* To use buf.array() the buffer must:
     *    be writable as the array will be writable
     *    have arrayOffset() == 0 or the array will not start at the right location
     *    the returned array must be the same length as the buffer's limit or it will be the wrong size.
     *//*from   ww  w  . j ava2 s.c o  m*/
    if (!buf.isReadOnly() && buf.hasArray() && buf.arrayOffset() == 0) {
        logger.debug("!read-only, hasArray && offset is 0");
        byte[] ret = buf.array();

        if (ret.length == buf.limit())
            return ret;
        logger.debug("length of array !=limit, doing copy...");
    }

    byte[] bytes = new byte[buf.limit()];
    buf.get(bytes, 0, buf.limit());
    return bytes;
}

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   w  w w  .  ja  v a  2 s.c  om
 * <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

/**
 * Discards data from the buffer up to the first SPS, where {@code data.position()} is interpreted
 * as the length of the buffer.//  w  ww .  j  a v  a  2s  . 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:net.darkmist.alib.io.BufferUtil.java

/**
 * Sane ByteBuffer slice//  w  ww. jav a2 s .  com
 * @param buf the buffer to slice something out of
 * @param off The offset into the buffer
 * @param len the length of the part to slice out
 */
public static ByteBuffer slice(ByteBuffer buf, int off, int len) {
    ByteBuffer localBuf = buf.duplicate(); // so we don't mess up the position,etc
    logger.debug("off={} len={}", off, len);
    localBuf.position(off);
    localBuf.limit(off + len);
    logger.debug("pre-slice: localBuf.position()={} localBuf.limit()={}", localBuf.position(),
            localBuf.limit());
    localBuf = localBuf.slice();
    logger.debug("post-slice: localBuf.position()={} localBuf.limit()={}", localBuf.position(),
            localBuf.limit());
    return localBuf;
}

From source file:de.csdev.ebus.utils.EBusUtils.java

/**
 * Convert a ByteBuffer to a byte array/* ww w .j  a v a2  s .c o  m*/
 *
 * @param buffer
 * @return
 */
public static byte[] toByteArray(ByteBuffer buffer) {

    int size = 0;
    if (buffer.position() == 0) {
        size = buffer.limit();
    } else {
        size = buffer.position();
    }

    byte[] data = new byte[size];
    ((ByteBuffer) buffer.duplicate().clear()).get(data);
    return data;
}