/** * The current capacity of the buffer. * * @return the current capacity of the buffer. */ public int capacity() { return buffer.capacity(); }
/** * The length of the claimed range in the buffer. * * @return length of the range in the buffer. */ public final int length() { return buffer.capacity() - HEADER_LENGTH; }
/** * Commit the message to the log buffer so that is it available to subscribers. */ public final void commit() { int frameLength = buffer.capacity(); if (ByteOrder.nativeOrder() != LITTLE_ENDIAN) { frameLength = Integer.reverseBytes(frameLength); } buffer.putIntOrdered(FRAME_LENGTH_FIELD_OFFSET, frameLength); }
public int termLength() { return termBuffers[0].capacity(); }
private void cleanBufferTo(final long newCleanPosition) { final long cleanPosition = this.cleanPosition; final int bytesForCleaning = (int)(newCleanPosition - cleanPosition); final UnsafeBuffer dirtyTerm = termBuffers[indexByPosition(cleanPosition, positionBitsToShift)]; final int termOffset = (int)cleanPosition & termLengthMask; final int length = Math.min(bytesForCleaning, dirtyTerm.capacity() - termOffset); if (length > 0) { dirtyTerm.setMemory(termOffset, length, (byte)0); this.cleanPosition = cleanPosition + length; } }
private void ensureCapacity(final int additionalCapacity) { final long requiredCapacity = (long)limit + additionalCapacity; if (requiredCapacity > BufferBuilderUtil.MAX_CAPACITY) { throw new IllegalStateException( "max capacity exceeded: limit=" + limit + " required=" + requiredCapacity); } final int capacity = buffer.capacity(); if (requiredCapacity > capacity) { resize(BufferBuilderUtil.findSuitableCapacity(capacity, (int)requiredCapacity)); } }
private static void streamChunks(final Publication publication, final long correlationId, final UnsafeBuffer buffer) { final BufferClaim bufferClaim = new BufferClaim(); final int fileLength = buffer.capacity(); final int maxChunkLength = publication.maxPayloadLength() - CHUNK_PAYLOAD_OFFSET; int chunkOffset = 0; while (chunkOffset < fileLength) { final int chunkLength = Math.min(maxChunkLength, fileLength - chunkOffset); sendChunk(publication, bufferClaim, correlationId, buffer, chunkOffset, chunkLength); chunkOffset += chunkLength; } }
@Before public void setUp() { when(termBuffer.capacity()).thenReturn(TERM_BUFFER_CAPACITY); }
@Before public void setUp() { when(termBuffer.capacity()).thenReturn(TERM_BUFFER_CAPACITY); }
private int offsetOfMessage(final int offset) { return (offset - 1) * align(HEADER.capacity() + PAYLOAD.length, FRAME_ALIGNMENT); } }
@Before public void setUp() { when(mockTermBuffer.capacity()).thenReturn(TERM_BUFFER_CAPACITY); }
@Before public void setUp() { when(termBuffer.capacity()).thenReturn(LOG_BUFFER_CAPACITY); }
@Before public void setUp() { when(valueOf(termBuffer.capacity())).thenReturn(valueOf(TERM_BUFFER_CAPACITY)); }
@Before public void before() { when(termBuffer.capacity()).thenReturn(LogBufferDescriptor.TERM_MIN_LENGTH); }
@Test public void shouldDoNotingIfEndArrivesWithoutBegin() { when(header.flags()).thenReturn(FrameDescriptor.END_FRAG_FLAG); final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]); final int offset = 0; final int length = srcBuffer.capacity() / 2; adapter.onFragment(srcBuffer, offset, length, header); verify(delegateFragmentHandler, never()).onFragment(any(), anyInt(), anyInt(), any()); }
@Test public void shouldScanEmptyBuffer() { final int offset = 0; final int limit = termBuffer.capacity(); final int newOffset = TermBlockScanner.scan(termBuffer, offset, limit); assertThat(newOffset, is(offset)); }
@Test public void shouldReadFirstMessage() { final int offset = 0; final int limit = termBuffer.capacity(); final int messageLength = 50; final int alignedMessageLength = BitUtil.align(messageLength, FRAME_ALIGNMENT); when(termBuffer.getIntVolatile(lengthOffset(offset))).thenReturn(messageLength); when(termBuffer.getShort(typeOffset(offset))).thenReturn((short)HDR_TYPE_DATA); final int newOffset = TermBlockScanner.scan(termBuffer, offset, limit); assertThat(newOffset, is(alignedMessageLength)); }
@Test public void shouldAppendThenReset() { final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[MIN_ALLOCATED_CAPACITY]); bufferBuilder.append(srcBuffer, 0, srcBuffer.capacity()); assertThat(bufferBuilder.limit(), is(srcBuffer.capacity())); bufferBuilder.reset(); assertThat(bufferBuilder.limit(), is(0)); }
@Test public void shouldGrowToMultipleOfInitialCapacity() { final int srcCapacity = MIN_ALLOCATED_CAPACITY * 5; final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[srcCapacity]); bufferBuilder.append(srcBuffer, 0, srcBuffer.capacity()); assertThat(bufferBuilder.limit(), is(srcCapacity)); assertThat(bufferBuilder.capacity(), greaterThanOrEqualTo(srcCapacity)); }