Example usage for java.nio ByteBuffer capacity

List of usage examples for java.nio ByteBuffer capacity

Introduction

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

Prototype

public final int capacity() 

Source Link

Document

Returns the capacity of this buffer.

Usage

From source file:Main.java

public static int getTypeNew(ByteBuffer buf) {
    int type = -1;
    if (buf != null && buf.capacity() > 11) {
        type = buf.get(11);// w  w w .  ja va 2 s.com
    }
    return type;
}

From source file:Main.java

public static ByteBuffer clone(ByteBuffer original) {
    ByteBuffer clone = ByteBuffer.allocate(original.capacity());
    original.rewind();//copy from the beginning
    clone.put(original);//from w w  w  . j  a v  a  2 s.  com
    original.rewind();
    clone.flip();
    return clone;
}

From source file:Main.java

/**
 * continue writing to bb starting from its current limit.
 * //from ww w .j a v a 2 s  .co  m
 * @param bb
 */
public static void extend(ByteBuffer bb) {
    bb.position(bb.limit()).limit(bb.capacity());
}

From source file:Main.java

public static ByteBuffer setupByteBuffer(ByteBuffer preBuffer, byte[] array) {

    if (preBuffer == null || preBuffer.capacity() < array.length) {
        preBuffer = createByteBuffer(array.length * 2);
    } else {/*  w w w . j a  v  a  2s .  c  o  m*/
        preBuffer.clear();
    }
    preBuffer.put(array);
    preBuffer.position(0);
    return preBuffer;
}

From source file:Main.java

/**
 * position is 0 and limit = capacity.//from w ww  .  j  av  a  2 s .com
 * 
 * @param bb
 * @return
 */
public final static boolean isEmpty(final ByteBuffer bb) {
    return (bb.position() == 0 && bb.limit() == bb.capacity()) || bb.limit() == 0;
}

From source file:Main.java

private static String formatBytes(byte[] data, boolean unsigned) {
    ByteBuffer bb = ByteBuffer.wrap(data);

    StringBuilder sb = new StringBuilder(bb.capacity() * 3);

    while (bb.remaining() > 0) {
        if (unsigned) {
            sb.append(bb.get() & 0xff);
        } else {/*from  w  ww  .j  a v a 2  s. c  om*/
            sb.append(bb.get());
        }

        sb.append(',');
        sb.append('\n');
    }

    return sb.toString();
}

From source file:oz.hadoop.yarn.api.utils.ByteBufferUtils.java

/**
 * /*from  ww  w.  j av  a  2s. c  o m*/
 */
private static int calculateNewLength(ByteBuffer source, ByteBuffer target) {
    long newLength = source.capacity() + (target.limit() - source.remaining());
    if (newLength <= threshold) {
        return (int) newLength;
    }
    return -1;
}

From source file:Main.java

private static ByteBuffer allocateMore(ByteBuffer output) {
    if (output.capacity() == 0) {
        return ByteBuffer.allocate(1);
    }//  ww  w . j  a va 2s . c om
    ByteBuffer result = ByteBuffer.allocate(output.capacity() * 2);
    output.flip();
    result.put(output);
    return result;
}

From source file:neembuu.uploader.external.HttpUtil.java

private static ReadableByteChannel wrap(final InputStream is, final HttpEntity he, final Content c,
        final double contentLength) {
    return new ReadableByteChannel() {
        double total = 0;

        @Override/*from www  .  j  a v a  2  s .  c o  m*/
        public int read(ByteBuffer dst) throws IOException {
            byte[] b = new byte[dst.capacity()];
            int r = is.read(b);
            //this sleep is just to slow down update to see, if the UI is working or not !
            // NU's update is very very very fast
            //try{Thread.sleep(1000);}catch(Exception a){}
            dst.put(b, 0, r);
            total += r;
            c.setProgress(total / contentLength);
            return r;
        }

        @Override
        public boolean isOpen() {
            return he.isStreaming();
        }

        @Override
        public void close() throws IOException {
            is.close();
        }
    };
}

From source file:Main.java

public static ByteBuffer cloneByteBuffer(final ByteBuffer original) {
    // Create clone with same capacity as original.
    final ByteBuffer clone = (original.isDirect()) ? ByteBuffer.allocateDirect(original.capacity())
            : ByteBuffer.allocate(original.capacity());

    // Create a read-only copy of the original.
    // This allows reading from the original without modifying it.
    final ByteBuffer readOnlyCopy = original.asReadOnlyBuffer();

    // Flip and read from the original.
    readOnlyCopy.flip();//from   w ww .jav  a  2  s  .  c om
    clone.put(readOnlyCopy);
    clone.position(original.position());
    clone.limit(original.limit());
    clone.order(original.order());
    return clone;
}