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 final static void giveBack(ByteBuffer bb) {
    if (bb != null) {
        final int capacity = bb.capacity();
        if (capacity == MAX_LINE_BYTES_SMALL) {
            if (!byteBuffersPoolSmall.contains(bb) && byteBuffersPoolSmall.size() < MAX_BUFFER_SIZE) {
                byteBuffersPoolSmall.add(bb);
            }/*from  ww w  .  j a v a  2s.  co  m*/
        } else if (capacity == MAX_LINE_BYTES_MEDIUM) {
            if (!byteBuffersPoolMedium.contains(bb) && byteBuffersPoolMedium.size() < MAX_BUFFER_SIZE) {
                byteBuffersPoolMedium.add(bb);
            }
        } else if (capacity == MAX_LINE_BYTES_NORMAL) {
            if (!byteBuffersPoolNormal.contains(bb) && byteBuffersPoolNormal.size() < MAX_BUFFER_SIZE) {
                byteBuffersPoolNormal.add(bb);
            }
        } else if (capacity == MAX_LINE_BYTES_LARGE) {
            if (!byteBuffersPoolLarge.contains(bb) && byteBuffersPoolLarge.size() < MAX_BUFFER_SIZE) {
                byteBuffersPoolLarge.add(bb);
            }
        } else if (capacity == MAX_LINE_BYTES_VERY_LARGE) {
            if (!byteBuffersPoolLarge.contains(bb) && byteBuffersPoolLarge.size() < 2) {
                byteBuffersPoolLarge.add(bb);
            }
        } else {
            throw new IllegalArgumentException("Non compatible byte buffer size: " + capacity);
        }
        bb.clear();
    }
}

From source file:Main.java

public static boolean wrapsFullArray(ByteBuffer byteBuffer) {
    return byteBuffer.hasArray() && byteBuffer.position() == 0 && byteBuffer.arrayOffset() == 0
            && byteBuffer.remaining() == byteBuffer.capacity();
}

From source file:com.jadarstudios.rankcapes.bukkit.CapePackValidator.java

/**
 * Utility method to detect if the bytes given are a zip file.
 *
 * @param bytes the bytes of the file// ww w .j  a v a  2 s.c o m
 *
 * @return if it is a zip file
 */
public static boolean isZipFile(byte[] bytes) {
    ByteArrayInputStream input = new ByteArrayInputStream(bytes);

    ByteBuffer buffer = ByteBuffer.allocate(4);
    input.read(buffer.array(), 0, buffer.capacity());

    short packIdentifier = buffer.getShort();

    return packIdentifier == ZIP_IDENTIFIER;
}

From source file:com.liveramp.commons.util.BytesUtils.java

public static ByteBuffer byteBufferDeepCopy(ByteBuffer src, ByteBuffer dst) {
    if (dst == null || dst.capacity() < src.remaining()) {
        dst = byteBufferDeepCopy(src);/*from  w w  w  .j  av a  2  s .  c  om*/
    } else {
        dst.rewind();
        dst.limit(src.remaining());
        dst.put(src.slice());
        dst.flip();
    }
    return dst;
}

From source file:experts.net.ip6.ULUA.java

/**
 * Convert ByteBuffer to Short List//w  w w.ja v a 2 s .  c om
 *
 * @param buf
 *            ByteBuffer
 * @return Short List
 */
private static List<Short> toList(ByteBuffer buf) {
    int cap = buf.capacity() / 2;
    List<Short> tmp = new ArrayList<>(cap);

    buf.rewind();
    for (int i = 0; i < cap; i++) {
        tmp.add(buf.getShort());
    } // for

    return tmp;
}

From source file:org.colombbus.tangara.io.ScriptHeaderHelper.java

public static boolean containsHeader(ByteBuffer content) {
    Validate.notNull(content, "content argument is null"); //$NON-NLS-1$
    if (content.capacity() < HEADER_PREFIX.length)
        return false;
    for (int i = 0; i < HEADER_PREFIX.length; i++) {
        if (HEADER_PREFIX[i] != content.get(i))
            return false;
    }//w w w .  j a  v  a 2s  .c om
    return true;
}

From source file:org.colombbus.tangara.io.ScriptReader.java

/**
  * Try to decode a byte buffer with a charset
  *//w w  w .j a  v a  2  s .c  om
  * @param content
  *            the bute buffer
  * @param cs
  *            the charset
  * @return <code>null</code> if the charset is not supported, or the decoded
  *         string
  */
 private static String tryDecodeBufferWithCharset(ByteBuffer content, Charset cs) {
     CharBuffer buffer = CharBuffer.allocate(content.capacity() * 2);
     CharsetDecoder decoder = createDecoder(cs);
     content.rewind();
     CoderResult coderRes = decoder.decode(content, buffer, true);
     if (coderRes.isError() == false) {
         buffer.rewind();
         return buffer.toString().trim();
     }
     return null;
 }

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

/**
 * //from  w  w  w .j av  a 2s . c  o  m
 * @param source
 * @param target
 * @return
 */
public static ByteBuffer merge(ByteBuffer source, ByteBuffer target) {
    if (target.position() != 0) {
        target.flip();
    }
    int rPos = source.position() + target.limit();
    if (rPos <= source.capacity()) {
        source.put(target);
        return source;
    } else {
        int newLength = calculateNewLength(source, target);
        if (newLength > 0) {
            ByteBuffer b = ByteBuffer.allocate(newLength);
            source.flip();
            b.put(source);
            b.put(target);
            return b;
        } else {
            throw new BufferOverflowException(
                    "Buffer can no longer be expended. Maximum allowed size is " + threshold + " bytes.");
        }
    }
}

From source file:com.oneguy.recognize.Util.java

public static ByteBuffer putData(ByteBuffer buffer, int data) {
    if (buffer == null) {
        return buffer;
    }//from  w  w  w.  j a  v  a  2 s .  c  o m
    // sizeof(int) == 4
    while (buffer.capacity() < buffer.position() + 4 - 1) {
        buffer = doubleSize(buffer);
    }
    buffer.putInt(data);
    return buffer;
}

From source file:com.oneguy.recognize.Util.java

public static ByteBuffer putData(ByteBuffer buffer, short data) {
    if (buffer == null) {
        return buffer;
    }/*from   w w w  . j av a2  s .c om*/
    // sizeof(int) == 2
    while (buffer.capacity() < buffer.position() + 2 - 1) {
        buffer = doubleSize(buffer);
    }
    buffer.putShort(data);
    return buffer;
}