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:io.mycat.util.ByteBufferUtil.java

public static boolean isPrefix(ByteBuffer prefix, ByteBuffer value) {
    if (prefix.remaining() > value.remaining()) {
        return false;
    }//from w w w .  j  av  a  2  s  . c om

    int diff = value.remaining() - prefix.remaining();
    return prefix.equals(value.duplicate().limit(value.remaining() - diff));
}

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

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

    return new InputStream() {
        public int read() throws IOException {
            if (!copy.hasRemaining())
                return -1;

            return copy.get() & 0xFF;
        }/*from ww w. ja  va  2 s  .  c o m*/

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

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

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

From source file:com.palantir.atlasdb.keyvalue.cassandra.CassandraKeyValueServices.java

public static byte[] getBytesFromByteBuffer(ByteBuffer buffer) {
    // Be careful *NOT* to perform anything that will modify the buffer's position or limit
    byte[] bytes = new byte[buffer.limit() - buffer.position()];
    if (buffer.hasArray()) {
        System.arraycopy(buffer.array(), buffer.position(), bytes, 0, bytes.length);
    } else {//from w  w  w.  ja v a2s .  co  m
        buffer.duplicate().get(bytes, buffer.position(), bytes.length);
    }
    return bytes;
}

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

/**
 * Decode a String representation.//w w  w  . jav a2s.c  o m
 * 
 * @param buffer
 *            a byte buffer holding the string representation
 * @param charset
 *            the String encoding charset
 * @return the decoded string
 */
public static String string(ByteBuffer buffer, Charset charset) throws CharacterCodingException {
    return charset.newDecoder().decode(buffer.duplicate()).toString();
}

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

public static ByteBuffer bytebuffer(ByteBuffer bytes) {
    return bytes.duplicate();
}

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

/**
 * Decode a String representation.//from  w  ww .  j a v  a 2s .  c o m
 * 
 * @param buffer
 *            a byte buffer holding the string representation
 * @param position
 *            the starting position in {@code buffer} to start decoding from
 * @param length
 *            the number of bytes from {@code buffer} to use
 * @param charset
 *            the String encoding charset
 * @return the decoded string
 */
public static String string(ByteBuffer buffer, int position, int length, Charset charset)
        throws CharacterCodingException {
    ByteBuffer copy = buffer.duplicate();
    copy.position(position);
    copy.limit(copy.position() + length);
    return string(copy, charset);
}

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

public static byte[] bytes(ByteBuffer bb) {
    byte[] b = new byte[bb.remaining()];
    bb.duplicate().get(b);
    return b;//from  w ww  .  j a va  2 s.c om
}

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

/**
 * You should almost never use this.  Instead, use the write* methods to avoid copies.
 *//*from ww w .  j av a2s  .co  m*/
public static byte[] getArray(ByteBuffer buffer) {
    int length = buffer.remaining();

    if (buffer.hasArray()) {
        int boff = buffer.arrayOffset() + buffer.position();
        return Arrays.copyOfRange(buffer.array(), boff, boff + length);
    }
    // else, DirectByteBuffer.get() is the fastest route
    byte[] bytes = new byte[length];
    buffer.duplicate().get(bytes);

    return bytes;
}

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

public static void arrayCopy(ByteBuffer buffer, int position, byte[] bytes, int offset, int length) {
    if (buffer.hasArray())
        System.arraycopy(buffer.array(), buffer.arrayOffset() + position, bytes, offset, length);
    else/*from  w w w  .ja v a 2  s.c  om*/
        ((ByteBuffer) buffer.duplicate().position(position)).get(bytes, offset, length);
}

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

/**
 * @return a new copy of the data in @param buffer
 * USUALLY YOU SHOULD USE ByteBuffer.duplicate() INSTEAD, which creates a new Buffer
 * (so you can mutate its position without affecting the original) without copying the underlying array.
 *//*from  w w w .  ja v a2 s .  c  o m*/
public static ByteBuffer clone(ByteBuffer buffer) {
    assert buffer != null;

    if (buffer.remaining() == 0) {
        return EMPTY_BYTE_BUFFER;
    }

    ByteBuffer clone = ByteBuffer.allocate(buffer.remaining());

    if (buffer.hasArray()) {
        System.arraycopy(buffer.array(), buffer.arrayOffset() + buffer.position(), clone.array(), 0,
                buffer.remaining());
    } else {
        clone.put(buffer.duplicate());
        clone.flip();
    }

    return clone;
}