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.glaf.core.util.ByteBufferUtils.java

/**
 * You should almost never use this. Instead, use the write* methods to
 * avoid copies./*  www .  j  a v a 2s.  c o  m*/
 */
public static byte[] getArray(ByteBuffer buffer) {
    int length = buffer.remaining();

    if (buffer.hasArray()) {
        int boff = buffer.arrayOffset() + buffer.position();
        if (boff == 0 && length == buffer.array().length)
            return buffer.array();
        else
            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 write(ByteBuffer buffer, DataOutput out) throws IOException {
    if (buffer.hasArray()) {
        out.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
    } else {/*from www.ja va 2s  . co m*/
        for (int i = buffer.position(); i < buffer.limit(); i++) {
            out.writeByte(buffer.get(i));
        }
    }
}

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   www .  jav a2s . 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;
}

From source file:com.gamesalutes.utils.ByteUtils.java

/**
 * Clones an object using serialization.
 * //from  w w w . j av a 2s.  c  o m
 * @param obj the input object
 * @param bufData the <code>ByteBuffer</code> in index 0 to use for temporary storage or <code>null</code> to create a new storage buffer.
 *            On output it will contain the final buffer used for serializing the object
 * @return the cloned object
 * @throws Exception 
 */
public static <T> T serializationClone(T obj, ByteBuffer[] bufData) throws Exception {
    if (obj == null)
        return null;
    ByteBuffer buf = null;
    int startPos = 0;
    if (!MiscUtils.isEmpty(bufData)) {
        buf = bufData[0];
        if (buf != null)
            startPos = buf.position();
    }
    buf = ByteUtils.getObjectBytes(obj, buf);
    T clone = ByteUtils.<T>readObject(buf);
    // copy new buffer over
    buf.position(startPos);
    bufData[0] = buf;

    return clone;

}

From source file:com.gamesalutes.utils.ByteUtils.java

/**
 * Returns a <code>ByteBuffer</code> containing the byte representation
 * of the serializable object <code>obj</code>.
 * /*from ww w  . j a va2s .  co m*/
 * @param obj the <code>Object</code> to convert to its byte representation
 * @param buf an existing buffer to use for storage or <code>null</code> to create new buffer.
 *        If <code>buf</code> is not large enough it will be expanded using {@link #growBuffer(ByteBuffer, int)}
 * @return <code>ByteBuffer</code> containing the byte representation
 * of <code>obj</code>.
 * 
 * @throws IOException if <code>obj</code> is not serializable or error occurs while writing the bytes
 */
public static ByteBuffer getObjectBytes(Object obj, ByteBuffer buf) throws IOException {
    if (buf == null)
        buf = ByteBuffer.allocate(READ_BUFFER_SIZE);

    // note the input position
    int startPos = buf.position();
    ByteBufferChannel channel = new ByteBufferChannel(buf);
    ObjectOutputStream out = null;
    try {
        out = new ObjectOutputStream(Channels.newOutputStream(channel));
        out.writeObject(obj);
        out.flush();
    } finally {
        MiscUtils.closeStream(out);
    }

    ByteBuffer returnBuf = channel.getByteBuffer();
    returnBuf.flip();
    // reset starting position to be that of input buffer
    returnBuf.position(startPos);
    return returnBuf;
}

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

/**
 * @param bb/*from  w w w  . j av a 2s.c o m*/
 * @param bytes
 * @param len
 * @return
 */
public static ByteBuffer appendToByteBuffer(ByteBuffer bb, byte[] bytes, int len) {
    if (len > bytes.length) {
        int pos = bb.position();
        bb.put(bytes);
        bb.position(pos + len);
    } else {
        bb.put(bytes, 0, len);
    }
    return bb;
}

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

/**
 * buffer?/* w  ww.j av a 2 s  .c  om*/
 * 
 * @param buffer
 * @return
 */
public static byte[] toArray(ByteBuffer buffer) {
    // ?heap buffer
    if (buffer.hasArray()) {
        byte[] array = buffer.array();
        int from = buffer.arrayOffset() + buffer.position();
        return Arrays.copyOfRange(array, from, from + buffer.remaining());
    }
    //  direct buffer
    else {
        byte[] to = new byte[buffer.remaining()];
        buffer.slice().get(to);
        return to;
    }
}

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

private static String readZeroTermStr(ByteBuffer bb) {
    int off = bb.position();
    while (bb.hasRemaining()) {
        byte b = bb.get();
        if (b == 0) {
            break;
        }/*from w  w w . j a  v  a  2 s.co m*/
    }
    int len = bb.position() - off;
    return readStr(bb, off, len);
}

From source file:com.gamesalutes.utils.ByteUtils.java

/**
 * Reads all the bytes from the given input stream and stores them in the specified buffer.
 * If the input buffer is <code>null</code> or does not have the capacity to store all the input, a 
 * new buffer is created and returned.  The input stream is closed regardless of whether an
 * <code>IOException</code> is thrown.
 * /*from  ww  w  . j  a va 2 s.  c om*/
 * 
 * @param in the <code>InputStream</code> to read
 * @param buf a <code>ByteBuffer</code> to use for storage or <code>null</code> to just allocate a new one
 *        If <code>buf</code> is not large enough it will be expanded using {@link #growBuffer(ByteBuffer, int)}
 * @return the buffer containing the read data
 * @throws IOException
 */
public static ByteBuffer readBytes(InputStream in, ByteBuffer buf) throws IOException {
    try {
        if (buf == null)
            buf = ByteBuffer.allocate(READ_BUFFER_SIZE);

        // note the input position
        int startPos = buf.position();

        byte[] tmp = new byte[NETWORK_BYTE_SIZE];
        int read;
        // read until end of file
        while ((read = in.read(tmp)) > 0) {
            if (buf.remaining() < read) {
                buf = ByteUtils.growBuffer(buf, buf.limit() + (read - buf.remaining()));
            }
            buf.put(tmp, 0, read);
        }

        buf.flip();
        // reset starting position to be that of input buffer
        buf.position(startPos);
        return buf;
    } finally {
        MiscUtils.closeStream(in);
    }

}

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

public static String string(ByteBuffer bytes) {
    if (bytes == null) {
        return null;
    }/*from w w  w.ja  v  a  2s.  c om*/
    return string(bytes.array(), bytes.arrayOffset() + bytes.position(), bytes.remaining(), UTF8_ENCODING);
}