Example usage for java.nio ByteBuffer limit

List of usage examples for java.nio ByteBuffer limit

Introduction

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

Prototype

public final int limit() 

Source Link

Document

Returns the limit of this buffer.

Usage

From source file:com.icloud.framework.core.nio.ByteBufferUtil.java

public static int compare(ByteBuffer o1, byte[] o2) {
    return compareUnsigned(o1.array(), o2, o1.arrayOffset() + o1.position(), 0, o1.limit() + o1.arrayOffset(),
            o2.length);/*from w  ww .  j  a v  a 2s . c  o  m*/
}

From source file:com.linkedin.databus.core.util.StringUtils.java

/**
 * Dumps as a hex string the contents of a buffer around a position
 * @param buf     the ByteBuffer to dump
 * @param bufOfs  starting offset in the buffer
 * @param length  the number of bytes to print
 * @return the hexstring/*from ww  w  .  j  av  a2 s.  co m*/
 */
public static String hexdumpByteBufferContents(ByteBuffer buf, int bufOfs, int length) {
    if (length < 0) {
        return "";
    }

    final int endOfs = Math.min(buf.limit(), bufOfs + length + 1);
    final byte[] bytes = new byte[endOfs - bufOfs];

    buf = buf.duplicate();
    buf.position(bufOfs);
    buf.get(bytes);
    return new String(Hex.encodeHex(bytes));
}

From source file:Main.java

public static void toString(ByteBuffer bb, StringBuilder sb) {
    byte[] buf = bb.array();

    int arrayOffset = bb.arrayOffset();
    int offset = arrayOffset + bb.position();
    int origLimit = arrayOffset + bb.limit();
    int limit = (origLimit - offset > 128) ? offset + 128 : origLimit;

    for (int i = offset; i < limit; i++) {
        if (i > offset) {
            sb.append(" ");
        }// w  w  w .  ja  va  2  s  .c  om
        sb.append(paddedByteString(buf[i]));
    }
    if (origLimit != limit) {
        sb.append("...");
    }
}

From source file:Main.java

public static List<ByteBuffer> mergeAdjacentBuffers(List<ByteBuffer> paramList) {
    ArrayList localArrayList = new ArrayList(paramList.size());
    Iterator localIterator = paramList.iterator();
    while (localIterator.hasNext()) {
        ByteBuffer localByteBuffer1 = (ByteBuffer) localIterator.next();
        int i = -1 + localArrayList.size();
        if ((i >= 0) && (localByteBuffer1.hasArray()) && (((ByteBuffer) localArrayList.get(i)).hasArray())
                && (localByteBuffer1.array() == ((ByteBuffer) localArrayList.get(i)).array())
                && (((ByteBuffer) localArrayList.get(i)).arrayOffset()
                        + ((ByteBuffer) localArrayList.get(i)).limit() == localByteBuffer1.arrayOffset())) {
            ByteBuffer localByteBuffer3 = (ByteBuffer) localArrayList.remove(i);
            localArrayList.add(ByteBuffer.wrap(localByteBuffer1.array(), localByteBuffer3.arrayOffset(),
                    localByteBuffer3.limit() + localByteBuffer1.limit()).slice());
        } else if ((i >= 0) && ((localByteBuffer1 instanceof MappedByteBuffer))
                && ((localArrayList.get(i) instanceof MappedByteBuffer))
                && (((ByteBuffer) localArrayList.get(i))
                        .limit() == ((ByteBuffer) localArrayList.get(i)).capacity()
                                - localByteBuffer1.capacity())) {
            ByteBuffer localByteBuffer2 = (ByteBuffer) localArrayList.get(i);
            localByteBuffer2.limit(localByteBuffer1.limit() + localByteBuffer2.limit());
        } else {//from ww w. ja va2 s . c o  m
            localByteBuffer1.reset();
            localArrayList.add(localByteBuffer1);
        }
    }
    return localArrayList;
}

From source file:com.icloud.framework.core.nio.ByteBufferUtil.java

public static int compare(byte[] o1, ByteBuffer o2) {
    return compareUnsigned(o1, o2.array(), 0, o2.arrayOffset() + o2.position(), o1.length,
            o2.limit() + o2.arrayOffset());
}

From source file:Main.java

/**
 * Relative <em>get</em> method for reading {@code size} number of bytes from the current
 * position of this buffer.//from w w  w .  j ava 2 s .  c om
 * <p>
 * <p>This method reads the next {@code size} bytes at this buffer's current position,
 * returning them as a {@code ByteBuffer} with start set to 0, limit and capacity set to
 * {@code size}, byte order set to this buffer's byte order; and then increments the position by
 * {@code size}.
 */
private static ByteBuffer getByteBuffer(final ByteBuffer source, final int size)
        throws BufferUnderflowException {
    if (size < 0) {
        throw new IllegalArgumentException("size: " + size);
    }
    final int originalLimit = source.limit();
    final int position = source.position();
    final int limit = position + size;
    if ((limit < position) || (limit > originalLimit)) {
        throw new BufferUnderflowException();
    }
    source.limit(limit);
    try {
        final ByteBuffer result = source.slice();
        result.order(source.order());
        source.position(limit);
        return result;
    } finally {
        source.limit(originalLimit);
    }
}

From source file:com.bconomy.autobit.Encryption.java

private static byte[] charsToBytes(char[] chars) {
    CharBuffer charBuffer = CharBuffer.wrap(chars);
    ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer);
    byte[] bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit());
    Arrays.fill(charBuffer.array(), '\u0000'); // clear sensitive data
    Arrays.fill(byteBuffer.array(), (byte) 0); // clear sensitive data
    Arrays.fill(chars, '\u0000'); // clear sensitive data
    return bytes;
}

From source file:Main.java

public static final int indexOfP(final ByteBuffer lineBB, final byte[][] textsLower,
        final byte[][] textsUpper) {
    return indexOf(lineBB.array(), lineBB.position(), lineBB.limit(), textsLower, textsUpper);
}

From source file:org.apache.hadoop.hbase.ipc.TestIPCUtil.java

static void doBuildCellBlockUndoCellBlock(final IPCUtil util, final Codec codec,
        final CompressionCodec compressor, final int count, final int size, final boolean sized)
        throws IOException {
    Cell[] cells = getCells(count, size);
    CellScanner cellScanner = sized ? getSizedCellScanner(cells)
            : CellUtil.createCellScanner(Arrays.asList(cells).iterator());
    ByteBuffer bb = util.buildCellBlock(codec, compressor, cellScanner);
    cellScanner = util.createCellScanner(codec, compressor, bb.array(), 0, bb.limit());
    int i = 0;/*from   ww w  .j a va  2  s. com*/
    while (cellScanner.advance()) {
        i++;
    }
    assertEquals(count, i);
}

From source file:com.cloudera.branchreduce.impl.thrift.Writables.java

public static <T extends Writable> T fromByteBuffer(ByteBuffer bb, Class<T> clazz) {
    T instance = ReflectionUtils.newInstance(clazz, DUMMY);
    try {/* www  .  ja v  a2 s. c om*/
        instance.readFields(
                new DataInputStream(new ByteArrayInputStream(bb.array(), bb.arrayOffset(), bb.limit())));
    } catch (IOException e) {
        LOG.error("Deserialization error for class: " + clazz, e);
    }
    return instance;
}