Java Code Examples for java.nio.ByteBuffer#limit()

The following examples show how to use java.nio.ByteBuffer#limit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BrotliCompressorTest.java    From jbrotli with Apache License 2.0 6 votes vote down vote up
@Test
public void compress_with_byte_array_wrapped_ByteBuffer_using_position_and_limit_on_output_buffer() throws Exception {
  // setup
  ByteBuffer inBuffer = wrap(A_BYTES);
  ByteBuffer outBuffer = allocate(100);

  // given
  int testPosition = 23;
  outBuffer.position(testPosition);
  outBuffer.limit(testPosition + A_BYTES_COMPRESSED.length);

  // when
  int outLength = compressor.compress(Brotli.DEFAULT_PARAMETER, inBuffer, outBuffer);

  // then
  assertThat(outLength).isEqualTo(A_BYTES_COMPRESSED.length);
  assertThat(outBuffer.position()).isEqualTo(testPosition);
  assertThat(outBuffer.limit()).isEqualTo(testPosition + outLength);
  assertThat(inBuffer.position()).isEqualTo(A_BYTES.length);
  // then
  assertThat(getByteArray(outBuffer)).isEqualTo(A_BYTES_COMPRESSED);
}
 
Example 2
Source File: PcapNgFile.java    From tracecompass with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Get an option value for a given option code from an options list
 *
 * @param options
 *            options list byte buffer
 * @param code
 *            option code
 * @return a byte buffer containing the option value, or null if not found
 */
private static @Nullable ByteBuffer getOptionValue(ByteBuffer options, int code) {
    options.rewind();
    while (options.remaining() > 0) {
        short optionCode = options.getShort();
        if (optionCode == PcapNgFileValues.ENDOFOPT_CODE) {
            break;
        }
        int length = ConversionHelper.unsignedShortToInt(options.getShort());
        if (optionCode == code) {
            ByteBuffer value = options.slice();
            value.limit(length);
            value.order(options.order());
            return value;
        }
        if (length % 4 != 0) {
            // pad to 32-bit boundary
            length += (4 - length % 4);
        }
        options.position(options.position() + length);
    }
    return null;
}
 
Example 3
Source File: RemotingCommand.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
public static RemotingCommand decode(final ByteBuffer byteBuffer) {
    int length = byteBuffer.limit();
    int oriHeaderLen = byteBuffer.getInt();
    int headerLength = getHeaderLength(oriHeaderLen);

    byte[] headerData = new byte[headerLength];
    byteBuffer.get(headerData);

    RemotingCommand cmd = headerDecode(headerData, getProtocolType(oriHeaderLen));

    int bodyLength = length - 4 - headerLength;
    byte[] bodyData = null;
    if (bodyLength > 0) {
        bodyData = new byte[bodyLength];
        byteBuffer.get(bodyData);
    }
    cmd.body = bodyData;

    return cmd;
}
 
Example 4
Source File: TestDelegatingSeekableInputStream.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
@Test
public void testDirectReadFullySmallTempBufferWithPositionAndLimit() throws Exception {
  byte[] temp = new byte[2]; // this will cause readFully to loop

  final ByteBuffer readBuffer = ByteBuffer.allocateDirect(10);
  readBuffer.position(3);
  readBuffer.limit(7);
  readBuffer.mark();

  MockInputStream stream = new MockInputStream(2, 3, 3);

  DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, temp);
  Assert.assertEquals(7, readBuffer.position());
  Assert.assertEquals(7, readBuffer.limit());

  DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, temp);
  Assert.assertEquals(7, readBuffer.position());
  Assert.assertEquals(7, readBuffer.limit());

  readBuffer.reset();
  Assert.assertEquals("Buffer contents should match",
      ByteBuffer.wrap(TEST_ARRAY, 0, 4), readBuffer);

  readBuffer.position(7);
  readBuffer.limit(10);
  DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, temp);
  Assert.assertEquals(10, readBuffer.position());
  Assert.assertEquals(10, readBuffer.limit());

  readBuffer.reset();
  Assert.assertEquals("Buffer contents should match",
      ByteBuffer.wrap(TEST_ARRAY, 0, 7), readBuffer);
}
 
Example 5
Source File: ISO_8859_1.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private CoderResult decodeArrayLoop(ByteBuffer src,
                                    CharBuffer dst)
{
    byte[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();
    assert (sp <= sl);
    sp = (sp <= sl ? sp : sl);
    char[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();
    assert (dp <= dl);
    dp = (dp <= dl ? dp : dl);

    try {
        while (sp < sl) {
            byte b = sa[sp];
            if (dp >= dl)
                return CoderResult.OVERFLOW;
            da[dp++] = (char)(b & 0xff);
            sp++;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 6
Source File: RowValueDecoderTest.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecode() throws Exception {
    CubeDesc cubeDesc = CubeManager.getInstance(getTestConfig()).getCube("test_kylin_cube_with_slr_ready").getDescriptor();
    HBaseColumnDesc hbaseCol = cubeDesc.getHbaseMapping().getColumnFamily()[0].getColumns()[0];

    BufferedMeasureCodec codec = new BufferedMeasureCodec(hbaseCol.getMeasures());
    BigDecimal sum = new BigDecimal("333.1234567");
    BigDecimal min = new BigDecimal("333.1111111");
    BigDecimal max = new BigDecimal("333.1999999");
    Long count = new Long(2);
    Long item_count = new Long(100);
    ByteBuffer buf = codec.encode(new Object[] { sum, min, max, count, item_count });

    buf.flip();
    byte[] valueBytes = new byte[buf.limit()];
    System.arraycopy(buf.array(), 0, valueBytes, 0, buf.limit());

    RowValueDecoder rowValueDecoder = new RowValueDecoder(hbaseCol);
    for (MeasureDesc measure : cubeDesc.getMeasures()) {
        FunctionDesc aggrFunc = measure.getFunction();
        int index = hbaseCol.findMeasure(aggrFunc);
        rowValueDecoder.setProjectIndex(index);
    }

    rowValueDecoder.decodeAndConvertJavaObj(valueBytes);
    Object[] measureValues = rowValueDecoder.getValues();
    //BigDecimal.ROUND_HALF_EVEN in BigDecimalSerializer
    assertEquals("[333.1235, 333.1111, 333.2000, 2, 100]", Arrays.toString(measureValues));
}
 
Example 7
Source File: DoubleByte.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
    byte[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();

    char[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();

    try {
        while (sp < sl && dp < dl) {
            // inline the decodeSingle/Double() for better performance
            int inSize = 1;
            int b1 = sa[sp] & 0xff;
            char c = b2cSB[b1];
            if (c == UNMAPPABLE_DECODING) {
                if (sl - sp < 2)
                    return crMalformedOrUnderFlow(b1);
                int b2 = sa[sp + 1] & 0xff;
                if (b2 < b2Min || b2 > b2Max ||
                    (c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING) {
                    return crMalformedOrUnmappable(b1, b2);
                }
                inSize++;
            }
            da[dp++] = c;
            sp += inSize;
        }
        return (sp >= sl) ? CoderResult.UNDERFLOW
                          : CoderResult.OVERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 8
Source File: TestBytes.java    From hbase with Apache License 2.0 5 votes vote down vote up
public void testGetBytesForByteBuffer() {
  byte[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  ByteBuffer target = ByteBuffer.wrap(array);
  target.position(2);
  target.limit(7);

  byte[] actual = Bytes.getBytes(target);
  byte[] expected = { 2, 3, 4, 5, 6 };
  assertArrayEquals(expected, actual);
  assertEquals(2, target.position());
  assertEquals(7, target.limit());
}
 
Example 9
Source File: PBKDF2KeyImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static byte[] getPasswordBytes(char[] passwd) {
    Charset utf8 = Charset.forName("UTF-8");
    CharBuffer cb = CharBuffer.wrap(passwd);
    ByteBuffer bb = utf8.encode(cb);

    int len = bb.limit();
    byte[] passwdBytes = new byte[len];
    bb.get(passwdBytes, 0, len);

    return passwdBytes;
}
 
Example 10
Source File: UnknownEntry.java    From mp4parser with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    ByteBuffer bb = content.duplicate();
    ((Buffer)bb).rewind();
    byte[] b = new byte[bb.limit()];
    bb.get(b);
    return "UnknownEntry{" +
            "content=" + Hex.encodeHex(b) +
            '}';
}
 
Example 11
Source File: BinaryDocValuesField.java    From liresolr with GNU General Public License v2.0 5 votes vote down vote up
@Override
    public IndexableField createField(SchemaField field, Object val /*, float boost*/) {
        if (val == null) return null;
        if (!field.stored()) {
            return null;
        }
        byte[] buf = null;
        int offset = 0, len = 0;
        if (val instanceof byte[]) {
            buf = (byte[]) val;
            len = buf.length;
        } else if (val instanceof ByteBuffer && ((ByteBuffer)val).hasArray()) {
            ByteBuffer byteBuf = (ByteBuffer) val;
            buf = byteBuf.array();
            offset = byteBuf.position();
            len = byteBuf.limit() - byteBuf.position();
        } else {
            String strVal = val.toString();
            //the string has to be a base64 encoded string
            buf = Base64.base64ToByteArray(strVal);
            offset = 0;
            len = buf.length;
        }

        Field f = new org.apache.lucene.document.BinaryDocValuesField(field.getName(), new BytesRef(buf, offset, len));
//        Field f = new org.apache.lucene.document.StoredField(field.getName(), buf, offset, len);
        //f.setBoost(boost);
        return f;
    }
 
Example 12
Source File: DataDecoder.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void parse(ByteBuffer b, Data data)
{

    int size = b.getInt();
    ByteBuffer buf = b.slice();
    buf.limit(size);
    b.position(b.position()+size);
    int count = buf.getInt();
    parseArray(data, buf, count);
}
 
Example 13
Source File: Buffers.java    From jlibs with Apache License 2.0 5 votes vote down vote up
public void write(byte b){
    ByteBuffer buffer = this.length==0 ? null : array[this.offset+this.length-1];
    if(buffer==null || buffer.limit()==buffer.capacity())
        append(buffer=BufferAllocator.current().allocate());
    else
        NIOUtil.compact(buffer);
    buffer.put(b);
    buffer.flip();
}
 
Example 14
Source File: DatagramChannelImpl.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private int send(FileDescriptor fd, ByteBuffer src, InetSocketAddress target)
    throws IOException
{
    if (src instanceof DirectBuffer)
        return sendFromNativeBuffer(fd, src, target);

    // Substitute a native buffer
    int pos = src.position();
    int lim = src.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    ByteBuffer bb = Util.getTemporaryDirectBuffer(rem);
    try {
        bb.put(src);
        bb.flip();
        // Do not update src until we see how many bytes were written
        src.position(pos);

        int n = sendFromNativeBuffer(fd, bb, target);
        if (n > 0) {
            // now update src
            src.position(pos + n);
        }
        return n;
    } finally {
        Util.releaseTemporaryDirectBuffer(bb);
    }
}
 
Example 15
Source File: TypeCodeOutputStream.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public void writeRawBuffer(org.omg.CORBA.portable.OutputStream s, int firstLong) {
    // Writes this streams buffer to the given OutputStream
    // without byte order flag and length as is the case for encapsulations.

    // Make sure to align s to 4 byte boundaries.
    // Unfortunately we can't do just this:
    // s.alignAndReserve(4, 4);
    // So we have to take the first four bytes given in firstLong and write them
    // with a call to write_long which will trigger the alignment.
    // Then write the rest of the byte array.

    //if (TypeCodeImpl.debug) {
        //System.out.println(this + ".writeRawBuffer(" + s + ", " + firstLong + ")");
        //if (s instanceof CDROutputStream) {
            //System.out.println("Parent position before writing kind = " + ((CDROutputStream)s).getIndex());
        //}
    //}
    s.write_long(firstLong);
    //if (TypeCodeImpl.debug) {
        //if (s instanceof CDROutputStream) {
            //System.out.println("Parent position after writing kind = " + ((CDROutputStream)s).getIndex());
        //}
    //}
    ByteBuffer byteBuffer = getByteBuffer();
    if (byteBuffer.hasArray())
    {
         s.write_octet_array(byteBuffer.array(), 4, getIndex() - 4);
    }
    else
    {
         // get bytes from DirectByteBuffer
         // NOTE: Microbenchmarks are showing it is faster to do
         //       a loop of ByteBuffer.get(int) than it is to do
         //       a bulk ByteBuffer.get(byte[], offset, length)
         byte[] buf = new byte[byteBuffer.limit()];
         for (int i = 0; i < buf.length; i++)
              buf[i] = byteBuffer.get(i);
         s.write_octet_array(buf, 4, getIndex() - 4);
    }
    //if (TypeCodeImpl.debug) {
        //if (s instanceof CDROutputStream) {
            //System.out.println("Parent position after writing all " + getIndex() + " bytes = " + ((CDROutputStream)s).getIndex());
        //}
    //}
}
 
Example 16
Source File: FileData.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
FileData(DataFile file, ByteBuffer buf) {
    this(file, file.writeTo(buf.array(), 0, buf.limit()), buf.limit());
}
 
Example 17
Source File: SimpleEUCEncoder.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
    char[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();
    assert (sp <= sl);
    sp = (sp <= sl ? sp : sl);
    byte[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();
    assert (dp <= dl);
    dp = (dp <= dl ? dp : dl);

    int     index;
    int     spaceNeeded;
    int     i;

    try {
        while (sp < sl) {
            boolean allZeroes = true;
            char inputChar = sa[sp];
            if (Character.isSurrogate(inputChar)) {
                if (sgp.parse(inputChar, sa, sp, sl) < 0)
                    return sgp.error();
                return sgp.unmappableResult();
            }

            if (inputChar >= '\uFFFE')
                return CoderResult.unmappableForLength(1);

            String theChars;
            char   aChar;

             // We have a valid character, get the bytes for it
            index = index1[((inputChar & mask1) >> shift)] + (inputChar & mask2);

            if (index < 7500)
                theChars = index2;
            else if (index < 15000) {
                 index = index - 7500;
                 theChars = index2a;
            } else if (index < 22500){
                index = index - 15000;
                theChars = index2b;
            }
            else {
                index = index - 22500;
                theChars = index2c;
            }

            aChar = theChars.charAt(2*index);
            outputByte[0] = (byte)((aChar & 0xff00)>>8);
            outputByte[1] = (byte)(aChar & 0x00ff);
            aChar = theChars.charAt(2*index + 1);
            outputByte[2] = (byte)((aChar & 0xff00)>>8);
            outputByte[3] = (byte)(aChar & 0x00ff);

        for (i = 0; i < outputByte.length; i++) {
            if (outputByte[i] != 0x00) {
            allZeroes = false;
            break;
            }
        }

        if (allZeroes && inputChar != '\u0000') {
            return CoderResult.unmappableForLength(1);
        }

        int oindex = 0;

        for (spaceNeeded = outputByte.length;
             spaceNeeded > 1; spaceNeeded--){
            if (outputByte[oindex++] != 0x00 )
                break;
        }

        if (dp + spaceNeeded > dl)
            return CoderResult.OVERFLOW;

        for (i = outputByte.length - spaceNeeded;
             i < outputByte.length; i++) {
                da[dp++] = outputByte[i];
        }
        sp++;
    }
    return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 18
Source File: AllocateDirectInit.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void printByteBuffer(ByteBuffer bb) {
    System.out.print("byte [");
    for (bb.position(0); bb.position() < bb.limit(); )
        System.out.print(" " + Integer.toHexString(bb.get() & 0xff));
    System.out.println(" ]");
}
 
Example 19
Source File: FadvisedFileRegion.java    From tez with Apache License 2.0 4 votes vote down vote up
/**
 * This method transfers data using local buffer. It transfers data from
 * a disk to a local buffer in memory, and then it transfers data from the
 * buffer to the target. This is used only if transferTo is disallowed in
 * the configuration file. super.TransferTo does not perform well on Windows
 * due to a small IO request generated. customShuffleTransfer can control
 * the size of the IO requests by changing the size of the intermediate
 * buffer.
 */
@VisibleForTesting
long customShuffleTransfer(WritableByteChannel target, long position)
    throws IOException {
  long actualCount = this.count - position;
  if (actualCount < 0 || position < 0) {
    throw new IllegalArgumentException(
        "position out of range: " + position +
            " (expected: 0 - " + (this.count - 1) + ')');
  }
  if (actualCount == 0) {
    return 0L;
  }

  long trans = actualCount;
  int readSize;
  ByteBuffer byteBuffer = ByteBuffer.allocate(this.shuffleBufferSize);

  while(trans > 0L &&
      (readSize = fileChannel.read(byteBuffer, this.position+position)) > 0) {
    //adjust counters and buffer limit
    if(readSize < trans) {
      trans -= readSize;
      position += readSize;
      byteBuffer.flip();
    } else {
      //We can read more than we need if the actualCount is not multiple
      //of the byteBuffer size and file is big enough. In that case we cannot
      //use flip method but we need to set buffer limit manually to trans.
      byteBuffer.limit((int)trans);
      byteBuffer.position(0);
      position += trans;
      trans = 0;
    }

    //write data to the target
    while(byteBuffer.hasRemaining()) {
      target.write(byteBuffer);
    }

    byteBuffer.clear();
  }

  return actualCount - trans;
}
 
Example 20
Source File: AllocateDirectInit.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void printByteBuffer(ByteBuffer bb) {
    System.out.print("byte [");
    for (bb.position(0); bb.position() < bb.limit(); )
        System.out.print(" " + Integer.toHexString(bb.get() & 0xff));
    System.out.println(" ]");
}