/** * Reads data from the front of the rolling buffer. * * @param absolutePosition The absolute position from which data should be read. * @param target The array into which data should be written. * @param length The number of bytes to read. */ private void readData(long absolutePosition, byte[] target, int length) { advanceReadTo(absolutePosition); int remaining = length; while (remaining > 0) { int toCopy = Math.min(remaining, (int) (readAllocationNode.endPosition - absolutePosition)); Allocation allocation = readAllocationNode.allocation; System.arraycopy(allocation.data, readAllocationNode.translateOffset(absolutePosition), target, length - remaining, toCopy); remaining -= toCopy; absolutePosition += toCopy; if (absolutePosition == readAllocationNode.endPosition) { readAllocationNode = readAllocationNode.next; } } }
/** * Clears allocation nodes starting from {@code fromNode}. * * @param fromNode The node from which to clear. */ private void clearAllocationNodes(AllocationNode fromNode) { if (!fromNode.wasInitialized) { return; } // Bulk release allocations for performance (it's significantly faster when using // DefaultAllocator because the allocator's lock only needs to be acquired and released once) // [Internal: See b/29542039]. int allocationCount = (writeAllocationNode.wasInitialized ? 1 : 0) + ((int) (writeAllocationNode.startPosition - fromNode.startPosition) / allocationLength); Allocation[] allocationsToRelease = new Allocation[allocationCount]; AllocationNode currentNode = fromNode; for (int i = 0; i < allocationsToRelease.length; i++) { allocationsToRelease[i] = currentNode.allocation; currentNode = currentNode.clear(); } allocator.release(allocationsToRelease); }
/** * Reads data from the front of the rolling buffer. * * @param absolutePosition The absolute position from which data should be read. * @param target The buffer into which data should be written. * @param length The number of bytes to read. */ private void readData(long absolutePosition, ByteBuffer target, int length) { int remaining = length; while (remaining > 0) { dropDownstreamTo(absolutePosition); int positionInAllocation = (int) (absolutePosition - totalBytesDropped); int toCopy = Math.min(remaining, allocationLength - positionInAllocation); Allocation allocation = dataQueue.peek(); target.put(allocation.data, allocation.translateOffset(positionInAllocation), toCopy); absolutePosition += toCopy; remaining -= toCopy; } }
/** * Reads data from the front of the rolling buffer. * * @param absolutePosition The absolute position from which data should be read. * @param target The array into which data should be written. * @param length The number of bytes to read. */ private void readData(long absolutePosition, byte[] target, int length) { int bytesRead = 0; while (bytesRead < length) { dropDownstreamTo(absolutePosition); int positionInAllocation = (int) (absolutePosition - totalBytesDropped); int toCopy = Math.min(length - bytesRead, allocationLength - positionInAllocation); Allocation allocation = dataQueue.peek(); System.arraycopy(allocation.data, allocation.translateOffset(positionInAllocation), target, bytesRead, toCopy); absolutePosition += toCopy; bytesRead += toCopy; } }
private void clearSampleData() { infoQueue.clearSampleData(); allocator.release(dataQueue.toArray(new Allocation[dataQueue.size()])); dataQueue.clear(); allocator.trim(); totalBytesDropped = 0; totalBytesWritten = 0; lastAllocation = null; lastAllocationOffset = allocationLength; }
private void clearSampleData() { infoQueue.clearSampleData(); allocator.release(dataQueue.toArray(new Allocation[dataQueue.size()])); dataQueue.clear(); allocator.trim(); totalBytesDropped = 0; totalBytesWritten = 0; lastAllocation = null; lastAllocationOffset = allocationLength; needKeyframe = true; }
/** * Reads data from the front of the rolling buffer. * * @param absolutePosition The absolute position from which data should be read. * @param target The buffer into which data should be written. * @param length The number of bytes to read. */ private void readData(long absolutePosition, ByteBuffer target, int length) { advanceReadTo(absolutePosition); int remaining = length; while (remaining > 0) { int toCopy = Math.min(remaining, (int) (readAllocationNode.endPosition - absolutePosition)); Allocation allocation = readAllocationNode.allocation; target.put(allocation.data, readAllocationNode.translateOffset(absolutePosition), toCopy); remaining -= toCopy; absolutePosition += toCopy; if (absolutePosition == readAllocationNode.endPosition) { readAllocationNode = readAllocationNode.next; } } }
/** * Initializes the node. * * @param allocation The node's {@link Allocation}. * @param next The next {@link AllocationNode}. */ public void initialize(Allocation allocation, AllocationNode next) { this.allocation = allocation; this.next = next; wasInitialized = true; }