use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.
the class BufferBuilderTest method shouldAppendTwoBuffersAndResize.
@Test
public void shouldAppendTwoBuffersAndResize() {
final int bufferLength = 128;
final byte[] buffer = new byte[bufferLength];
final int firstLength = buffer.length / 4;
final int secondLength = buffer.length / 2;
Arrays.fill(buffer, 0, firstLength + secondLength, (byte) 7);
final UnsafeBuffer srcBuffer = new UnsafeBuffer(buffer);
final BufferBuilder bufferBuilder = new BufferBuilder(bufferLength / 2);
bufferBuilder.append(srcBuffer, 0, firstLength);
bufferBuilder.append(srcBuffer, firstLength, secondLength);
final byte[] temp = new byte[buffer.length];
bufferBuilder.buffer().getBytes(0, temp, 0, secondLength + firstLength);
assertThat(bufferBuilder.limit(), is(firstLength + secondLength));
assertThat(bufferBuilder.capacity(), greaterThanOrEqualTo(firstLength + secondLength));
assertArrayEquals(temp, buffer);
}
use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.
the class FragmentAssemblerTest method shouldAssembleFourPartMessage.
@Test
public void shouldAssembleFourPartMessage() {
when(header.flags()).thenReturn(FrameDescriptor.BEGIN_FRAG_FLAG).thenReturn((byte) 0).thenReturn((byte) 0).thenReturn(FrameDescriptor.END_FRAG_FLAG);
final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
final int offset = 0;
final int length = srcBuffer.capacity() / 4;
for (int i = 0; i < 4; i++) {
srcBuffer.setMemory(i * length, length, (byte) (65 + i));
}
adapter.onFragment(srcBuffer, offset, length, header);
adapter.onFragment(srcBuffer, offset + length, length, header);
adapter.onFragment(srcBuffer, offset + (length * 2), length, header);
adapter.onFragment(srcBuffer, offset + (length * 3), length, header);
final ArgumentCaptor<UnsafeBuffer> bufferArg = ArgumentCaptor.forClass(UnsafeBuffer.class);
final ArgumentCaptor<Header> headerArg = ArgumentCaptor.forClass(Header.class);
verify(delegateFragmentHandler, times(1)).onFragment(bufferArg.capture(), eq(offset), eq(length * 4), headerArg.capture());
final UnsafeBuffer capturedBuffer = bufferArg.getValue();
for (int i = 0; i < srcBuffer.capacity(); i++) {
assertThat("same at i=" + i, capturedBuffer.getByte(i), is(srcBuffer.getByte(i)));
}
final Header capturedHeader = headerArg.getValue();
assertThat(capturedHeader.sessionId(), is(SESSION_ID));
assertThat(capturedHeader.flags(), is(FrameDescriptor.END_FRAG_FLAG));
}
use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.
the class FragmentAssemblerTest method shouldAssembleTwoPartMessage.
@Test
public void shouldAssembleTwoPartMessage() {
when(header.flags()).thenReturn(FrameDescriptor.BEGIN_FRAG_FLAG).thenReturn(FrameDescriptor.END_FRAG_FLAG);
final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
final int offset = 0;
final int length = srcBuffer.capacity() / 2;
srcBuffer.setMemory(0, length, (byte) 65);
srcBuffer.setMemory(length, length, (byte) 66);
adapter.onFragment(srcBuffer, offset, length, header);
adapter.onFragment(srcBuffer, length, length, header);
final ArgumentCaptor<UnsafeBuffer> bufferArg = ArgumentCaptor.forClass(UnsafeBuffer.class);
final ArgumentCaptor<Header> headerArg = ArgumentCaptor.forClass(Header.class);
verify(delegateFragmentHandler, times(1)).onFragment(bufferArg.capture(), eq(offset), eq(length * 2), headerArg.capture());
final UnsafeBuffer capturedBuffer = bufferArg.getValue();
for (int i = 0; i < srcBuffer.capacity(); i++) {
assertThat("same at i=" + i, capturedBuffer.getByte(i), is(srcBuffer.getByte(i)));
}
final Header capturedHeader = headerArg.getValue();
assertThat(capturedHeader.sessionId(), is(SESSION_ID));
assertThat(capturedHeader.flags(), is(FrameDescriptor.END_FRAG_FLAG));
}
use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.
the class CommonContext method saveErrorLog.
/**
* Read the error log to a given {@link PrintStream}
*
* @param out to write the error log contents to.
* @return the number of observations from the error log
*/
public int saveErrorLog(final PrintStream out) {
final File dirFile = new File(aeronDirectoryName);
int distinctErrorCount = 0;
if (dirFile.exists() && dirFile.isDirectory()) {
final File cncFile = new File(aeronDirectoryName, CncFileDescriptor.CNC_FILE);
if (cncFile.exists()) {
MappedByteBuffer cncByteBuffer = null;
try {
cncByteBuffer = IoUtil.mapExistingFile(cncFile, CncFileDescriptor.CNC_FILE);
final UnsafeBuffer cncMetaDataBuffer = CncFileDescriptor.createMetaDataBuffer(cncByteBuffer);
final int cncVersion = cncMetaDataBuffer.getInt(CncFileDescriptor.cncVersionOffset(0));
if (CncFileDescriptor.CNC_VERSION != cncVersion) {
throw new IllegalStateException("aeron cnc file version not understood: version=" + cncVersion);
}
final AtomicBuffer buffer = CncFileDescriptor.createErrorLogBuffer(cncByteBuffer, cncMetaDataBuffer);
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
distinctErrorCount = ErrorLogReader.read(buffer, (observationCount, firstObservationTimestamp, lastObservationTimestamp, encodedException) -> out.format("***%n%d observations from %s to %s for:%n %s%n", observationCount, dateFormat.format(new Date(firstObservationTimestamp)), dateFormat.format(new Date(lastObservationTimestamp)), encodedException));
out.format("%n%d distinct errors observed.%n", distinctErrorCount);
} catch (final Exception ex) {
LangUtil.rethrowUnchecked(ex);
} finally {
IoUtil.unmap(cncByteBuffer);
}
}
}
return distinctErrorCount;
}
use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.
the class Image method blockPoll.
/**
* Poll for new messages in a stream. If new messages are found beyond the last consumed position then they
* will be delivered to the {@link BlockHandler} up to a limited number of bytes.
*
* @param blockHandler to which block is delivered.
* @param blockLengthLimit up to which a block may be in length.
* @return the number of bytes that have been consumed.
*/
public int blockPoll(final BlockHandler blockHandler, final int blockLengthLimit) {
if (isClosed) {
return 0;
}
final long position = subscriberPosition.get();
final int termOffset = (int) position & termLengthMask;
final UnsafeBuffer termBuffer = activeTermBuffer(position);
final int limit = Math.min(termOffset + blockLengthLimit, termBuffer.capacity());
final int resultingOffset = TermBlockScanner.scan(termBuffer, termOffset, limit);
final int bytesConsumed = resultingOffset - termOffset;
if (resultingOffset > termOffset) {
try {
final int termId = termBuffer.getInt(termOffset + TERM_ID_FIELD_OFFSET, LITTLE_ENDIAN);
blockHandler.onBlock(termBuffer, termOffset, bytesConsumed, sessionId, termId);
} catch (final Throwable t) {
errorHandler.onError(t);
}
subscriberPosition.setOrdered(position + bytesConsumed);
}
return bytesConsumed;
}
Aggregations