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

The following examples show how to use java.nio.ByteBuffer#slice() . 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: TableName.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * @param fullname of a table, possibly with a leading namespace and ':' as delimiter.
 * @throws IllegalArgumentException if fullName equals old root or old meta.
 */
public static TableName valueOf(ByteBuffer fullname) {
  fullname = fullname.duplicate();
  fullname.mark();
  boolean miss = true;
  while (fullname.hasRemaining() && miss) {
    miss = ((byte) NAMESPACE_DELIM) != fullname.get();
  }
  if (miss) {
    fullname.reset();
    return valueOf(null, fullname);
  } else {
    ByteBuffer qualifier = fullname.slice();
    int delimiterIndex = fullname.position() - 1;
    fullname.reset();
    // changing variable name for clarity
    ByteBuffer namespace = fullname.duplicate();
    namespace.limit(delimiterIndex);
    return valueOf(namespace, qualifier);
  }
}
 
Example 2
Source File: MappedFile.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
public SelectMappedBufferResult selectMappedBuffer(int pos, int size) {
    int readPosition = getReadPosition();
    if ((pos + size) <= readPosition) {

        if (this.hold()) {
            ByteBuffer byteBuffer = this.mappedByteBuffer.slice();
            byteBuffer.position(pos);
            ByteBuffer byteBufferNew = byteBuffer.slice();
            byteBufferNew.limit(size);
            return new SelectMappedBufferResult(this.fileFromOffset + pos, byteBufferNew, size, this);
        } else {
            log.warn("matched, but hold failed, request pos: " + pos + ", fileFromOffset: "
                + this.fileFromOffset);
        }
    } else {
        log.warn("selectMappedBuffer request pos invalid, request pos: " + pos + ", size: " + size
            + ", fileFromOffset: " + this.fileFromOffset);
    }

    return null;
}
 
Example 3
Source File: XtraBox.java    From mp4parser with Apache License 2.0 6 votes vote down vote up
@Override
public void _parseDetails(ByteBuffer content) {
    int boxSize = content.remaining();
    data = content.slice(); //Keep this in case we fail to parse
    successfulParse = false;
    try {
        tags.clear();
        while (content.remaining() > 0) {
            XtraTag tag = new XtraTag();
            tag.parse(content);
            tags.addElement(tag);
        }
        int calcSize = detailSize();
        if (boxSize != calcSize) {
            throw new RuntimeException("Improperly handled Xtra tag: Calculated sizes don't match ( " + boxSize + "/" + calcSize + ")");
        }
        successfulParse = true;
    } catch (Exception e) {
        successfulParse = false;
        LOG.error("Malformed Xtra Tag detected: {}", e.toString());
        ((Buffer)content).position(content.position() + content.remaining());
    } finally {
        content.order(ByteOrder.BIG_ENDIAN); //Just in case we bailed out mid-parse we don't want to leave the byte order in MS land
    }
}
 
Example 4
Source File: PipelinedSorter.java    From tez with Apache License 2.0 6 votes vote down vote up
public ByteBuffer end() {
  ByteBuffer remaining = kvbuffer.duplicate();
  remaining.position(kvbuffer.position());
  remaining = remaining.slice();
  kvbuffer.limit(kvbuffer.position());
  kvmeta.limit(kvmeta.position());
  int items = length();
  if(items == 0) {
    return null;
  }
  int perItem = kvbuffer.position()/items;
  LOG.info(outputContext.getDestinationVertexName() + ": " + String.format("Span%d.length = %d, perItem = %d", index, length(), perItem));
  if(remaining.remaining() < METASIZE+perItem) {
    //Check if we can get the next Buffer from the main buffer list
    ByteBuffer space = allocateSpace();
    if (space != null) {
      LOG.info(outputContext.getDestinationVertexName() + ": " + "Getting memory from next block in the list, recordsWritten=" +
          mapOutputRecordCounter.getValue());
      reinit = true;
      return space;
    }
    return null;
  }
  return remaining;
}
 
Example 5
Source File: GZIPUtilsTest.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonHeapBuffers() throws Exception
{

    byte[] data = new byte[1024];
    Arrays.fill(data, (byte)'a');
    ByteBuffer directBuffer = ByteBuffer.allocateDirect(1024);
    directBuffer.put(data);
    directBuffer.flip();

    byte[] compressed = GZIPUtils.compressBufferToArray(directBuffer);

    assertTrue("Compression didn't compress", compressed.length < data.length);

    directBuffer.clear();
    directBuffer.position(1);
    directBuffer = directBuffer.slice();
    directBuffer.put(compressed);
    directBuffer.flip();

    byte[] uncompressed = GZIPUtils.uncompressBufferToArray(directBuffer);

    assertTrue("Compression not reversible", Arrays.equals(data, uncompressed));
}
 
Example 6
Source File: ByteSlice.java    From zetasketch with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a ByteBuffer representation of this slice. The buffer's position and limit will
 * correspond to the slice's position and limit. If the slice is marked as {@link #copyOnWrite}
 * the buffer will be read-only.
 */
public ByteBuffer byteBuffer() {
  // Create a ByteBuffer that starts at the array offset.
  ByteBuffer buffer = ByteBuffer.wrap(array);
  buffer.position(arrayOffset);
  buffer = buffer.slice();

  // Update the position and limit to match.
  buffer.position(position);
  buffer.limit(limit);

  if (isCopyOnWrite()) {
    return buffer.asReadOnlyBuffer();
  }
  return buffer;
}
 
Example 7
Source File: ApkSigningBlockUtils.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void consume(ByteBuffer buffer) {
    buffer = buffer.slice();
    for (MessageDigest md : mMds) {
        buffer.position(0);
        md.update(buffer);
    }
}
 
Example 8
Source File: MemorySegmentTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testSlicedByteBufferPut(boolean directBuffer) {
	byte[] bytes = new byte[pageSize + 49];
	random.nextBytes(bytes);

	ByteBuffer source = directBuffer ?
			ByteBuffer.allocateDirect(pageSize + 49) :
			ByteBuffer.allocate(pageSize + 49);

	source.put(bytes);
	source.position(19).limit(19 + pageSize);
	ByteBuffer slicedSource = source.slice();

	MemorySegment seg = createSegment(3 * pageSize);

	final int offset = 2 * pageSize;

	// transfer the segment in chunks into the byte buffer
	int pos = 0;
	while (pos < pageSize) {
		int len = random.nextInt(pageSize / 10);
		len = Math.min(len, pageSize - pos);
		seg.put(offset + pos, slicedSource, len);
		pos += len;
	}

	// verify that we read the same bytes
	byte[] result = new byte[pageSize];
	seg.get(offset, result);

	byte[] expected = Arrays.copyOfRange(bytes, 19, 19 + pageSize);
	assertArrayEquals(expected, result);
}
 
Example 9
Source File: BasicImageReader.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private static ByteBuffer slice(ByteBuffer buffer, int position, int capacity) {
    // Note that this is the only limit and position manipulation of
    // BasicImageReader private ByteBuffers.  The synchronize could be avoided
    // by cloning the buffer to make a local copy, but at the cost of creating
    // a new object.
    synchronized(buffer) {
        buffer.limit(position + capacity);
        buffer.position(position);
        return buffer.slice();
    }
}
 
Example 10
Source File: Type1Font.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public synchronized ByteBuffer readBlock(int offset, int length) {
    ByteBuffer mappedBuf = null;
    try {
        mappedBuf = getBuffer();
        if (offset > fileSize) {
            offset = fileSize;
        }
        mappedBuf.position(offset);
        return mappedBuf.slice();
    } catch (FontFormatException e) {
        return null;
    }
}
 
Example 11
Source File: ConsumeQueueExt.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
/**
 * Only read first 2 byte to get unit size.
 * <p>
 * if size > 0, then skip buffer position with size.
 * </p>
 * <p>
 * if size <= 0, nothing to do.
 * </p>
 */
private void readBySkip(final ByteBuffer buffer) {
    ByteBuffer temp = buffer.slice();

    short tempSize = temp.getShort();
    this.size = tempSize;

    if (tempSize > 0) {
        buffer.position(buffer.position() + this.size);
    }
}
 
Example 12
Source File: AbstractContentOutputStreamTestCase.java    From vespa with Apache License 2.0 5 votes vote down vote up
@Override
protected void doFlush(ByteBuffer buf) {
    writes.add(buf);
    buf = buf.slice();
    while (buf.hasRemaining()) {
        out.write(buf.get());
    }
}
 
Example 13
Source File: SSLCipher.java    From openjsse with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Plaintext decrypt(byte contentType, ByteBuffer bb,
        byte[] sequence) throws GeneralSecurityException {
    MAC signer = (MAC)authenticator;
    if (signer.macAlg().size != 0) {
        checkStreamMac(signer, bb, contentType, sequence);
    } else {
        authenticator.increaseSequenceNumber();
    }

    return new Plaintext(contentType,
            ProtocolVersion.NONE.major, ProtocolVersion.NONE.minor,
            -1, -1L, bb.slice());
}
 
Example 14
Source File: Type1Font.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public synchronized ByteBuffer readBlock(int offset, int length) {
    ByteBuffer mappedBuf = null;
    try {
        mappedBuf = getBuffer();
        if (offset > fileSize) {
            offset = fileSize;
        }
        mappedBuf.position(offset);
        return mappedBuf.slice();
    } catch (FontFormatException e) {
        return null;
    }
}
 
Example 15
Source File: ImmutableRoaringArray.java    From RoaringBitmap with Apache License 2.0 5 votes vote down vote up
/**
 * Create an array based on a previously serialized ByteBuffer. The input ByteBuffer is
 * effectively copied (with the slice operation) so you should expect the provided ByteBuffer
 * position/mark/limit/order to remain unchanged.
 *
 * @param bbf The source ByteBuffer
 */
protected ImmutableRoaringArray(ByteBuffer bbf) {
  buffer = bbf.slice();
  buffer.order(ByteOrder.LITTLE_ENDIAN);
  final int cookie = buffer.getInt(0);
  if ((cookie & 0xFFFF) != SERIAL_COOKIE && cookie != SERIAL_COOKIE_NO_RUNCONTAINER) {
    throw new InvalidRoaringFormat("I failed to find one of the right cookies. " + cookie);
  }
  boolean hasRunContainers = (cookie & 0xFFFF) == SERIAL_COOKIE;
  this.size = hasRunContainers ? (cookie >>> 16) + 1 : buffer.getInt(4);
  int theLimit = size > 0 ? computeSerializedSizeInBytes() : headerSize(hasRunContainers);
  buffer.limit(theLimit);
}
 
Example 16
Source File: BufferAlignmentAgentTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnsafeBufferFromSlicedHeapByteBuffer()
{
    final ByteBuffer nioBuffer = ByteBuffer.allocateDirect(256);
    nioBuffer.position(1);
    final UnsafeBuffer buffer = new UnsafeBuffer(nioBuffer.slice());
    assertTrue(buffer.addressOffset() % 4 != 0);
    testUnsafeBuffer(buffer, 7 + HEAP_BUFFER_ALIGNMENT_OFFSET);
}
 
Example 17
Source File: TestTLS12.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static void printTlsNetworkPacket(String prefix, ByteBuffer bb) {
    ByteBuffer slice = bb.slice();
    byte[] buffer = new byte[slice.remaining()];
    slice.get(buffer);
    for (int i = 0; i < buffer.length; i++) {
        System.out.printf("%02X, ", (byte)(buffer[i] & (byte)0xFF));
        if (i % 8 == 0 && i % 16 != 0) {
            System.out.print(" ");
        }
        if (i % 16 == 0) {
            System.out.println("");
        }
    }
    System.out.flush();
}
 
Example 18
Source File: CMap.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
CMapFormat12(ByteBuffer buffer, int offset, char[] xlat) {
    if (xlat != null) {
        throw new RuntimeException("xlat array for cmap fmt=12");
    }

    numGroups = buffer.getInt(offset+12);
    startCharCode = new long[numGroups];
    endCharCode = new long[numGroups];
    startGlyphID = new int[numGroups];
    buffer.position(offset+16);
    buffer = buffer.slice();
    IntBuffer ibuffer = buffer.asIntBuffer();
    for (int i=0; i<numGroups; i++) {
        startCharCode[i] = ibuffer.get() & INTMASK;
        endCharCode[i] = ibuffer.get() & INTMASK;
        startGlyphID[i] = ibuffer.get() & INTMASK;
    }

    /* Finds the high bit by binary searching through the bits */
    int value = numGroups;

    if (value >= 1 << 16) {
        value >>= 16;
        highBit += 16;
    }

    if (value >= 1 << 8) {
        value >>= 8;
        highBit += 8;
    }

    if (value >= 1 << 4) {
        value >>= 4;
        highBit += 4;
    }

    if (value >= 1 << 2) {
        value >>= 2;
        highBit += 2;
    }

    if (value >= 1 << 1) {
        value >>= 1;
        highBit += 1;
    }

    power = 1 << highBit;
    extra = numGroups - power;
}
 
Example 19
Source File: BrotliEncoderChannelTest.java    From ShizuruNotes with Apache License 2.0 4 votes vote down vote up
private static void run(String entryName, TestMode mode) throws Throwable {
  InputStream bundle = getBundle();
  byte[] original;
  try {
    original = BundleHelper.readEntry(bundle, entryName);
  } finally {
    bundle.close();
  }
  if (original == null) {
    throw new RuntimeException("Can't read bundle entry: " + entryName);
  }

  if ((mode == TestMode.WRITE_CHUNKS) && (original.length <= CHUNK_SIZE)) {
    return;
  }

  ByteArrayOutputStream dst = new ByteArrayOutputStream();
  WritableByteChannel encoder = new BrotliEncoderChannel(Channels.newChannel(dst));
  ByteBuffer src = ByteBuffer.wrap(original);
  try {
    switch (mode) {
      case WRITE_ALL:
        encoder.write(src);
        break;

      case WRITE_CHUNKS:
        while (src.hasRemaining()) {
          int limit = Math.min(CHUNK_SIZE, src.remaining());
          ByteBuffer slice = src.slice();
          ((Buffer) slice).limit(limit);
          ((Buffer) src).position(src.position() + limit);
          encoder.write(slice);
        }
        break;
    }
  } finally {
    encoder.close();
  }

  InputStream decoder = new BrotliInputStream(new ByteArrayInputStream(dst.toByteArray()));
  try {
    long originalCrc = BundleHelper.fingerprintStream(new ByteArrayInputStream(original));
    long crc = BundleHelper.fingerprintStream(decoder);
    assertEquals(originalCrc, crc);
  } finally {
    decoder.close();
  }
}
 
Example 20
Source File: HardwareVideoEncoder.java    From webrtc_android with MIT License 4 votes vote down vote up
protected void deliverEncodedImage() {
  outputThreadChecker.checkIsOnValidThread();
  try {
    MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
    int index = codec.dequeueOutputBuffer(info, DEQUEUE_OUTPUT_BUFFER_TIMEOUT_US);
    if (index < 0) {
      return;
    }

    ByteBuffer codecOutputBuffer = codec.getOutputBuffers()[index];
    codecOutputBuffer.position(info.offset);
    codecOutputBuffer.limit(info.offset + info.size);

    if ((info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
      Logging.d(TAG, "Config frame generated. Offset: " + info.offset + ". Size: " + info.size);
      configBuffer = ByteBuffer.allocateDirect(info.size);
      configBuffer.put(codecOutputBuffer);
    } else {
      bitrateAdjuster.reportEncodedFrame(info.size);
      if (adjustedBitrate != bitrateAdjuster.getAdjustedBitrateBps()) {
        updateBitrate();
      }

      final boolean isKeyFrame = (info.flags & MediaCodec.BUFFER_FLAG_SYNC_FRAME) != 0;
      if (isKeyFrame) {
        Logging.d(TAG, "Sync frame generated");
      }

      final ByteBuffer frameBuffer;
      if (isKeyFrame && codecType == VideoCodecType.H264) {
        Logging.d(TAG,
            "Prepending config frame of size " + configBuffer.capacity()
                + " to output buffer with offset " + info.offset + ", size " + info.size);
        // For H.264 key frame prepend SPS and PPS NALs at the start.
        frameBuffer = ByteBuffer.allocateDirect(info.size + configBuffer.capacity());
        configBuffer.rewind();
        frameBuffer.put(configBuffer);
        frameBuffer.put(codecOutputBuffer);
        frameBuffer.rewind();
      } else {
        frameBuffer = codecOutputBuffer.slice();
      }

      final EncodedImage.FrameType frameType = isKeyFrame
          ? EncodedImage.FrameType.VideoFrameKey
          : EncodedImage.FrameType.VideoFrameDelta;

      EncodedImage.Builder builder = outputBuilders.poll();
      builder.setBuffer(frameBuffer).setFrameType(frameType);
      // TODO(mellem):  Set codec-specific info.
      callback.onEncodedFrame(builder.createEncodedImage(), new CodecSpecificInfo());
    }
    codec.releaseOutputBuffer(index, false);
  } catch (IllegalStateException e) {
    Logging.e(TAG, "deliverOutput failed", e);
  }
}