@Test public void testSimpleByteTest() { assumeFalse(Jvm.isArm()); try (final ChronicleQueue chronicle = builder(getTmpDir(), wireType) .rollCycle(TEST2_DAILY) .build()) { final ExcerptAppender appender = chronicle.acquireAppender(); Bytes steve = Bytes.allocateDirect("Steve".getBytes()); appender.writeBytes(steve); Bytes jobs = Bytes.allocateDirect("Jobs".getBytes()); appender.writeBytes(jobs); final ExcerptTailer tailer = chronicle.createTailer(); Bytes bytes = Bytes.elasticByteBuffer(); try { tailer.readBytes(bytes); Assert.assertEquals("Steve", bytes.toString()); bytes.clear(); tailer.readBytes(bytes); Assert.assertEquals("Jobs", bytes.toString()); } finally { steve.release(); jobs.release(); bytes.release(); } } }
@Test public void testReadingLessBytesThanWritten() { try (final ChronicleQueue queue = builder(getTmpDir(), wireType) .build()) { final ExcerptAppender appender = queue.acquireAppender(); final Bytes<byte[]> expected = Bytes.wrapForRead("some long message".getBytes(ISO_8859_1)); for (int i = 0; i < 10; i++) { appender.writeBytes(expected); } final ExcerptTailer tailer = queue.createTailer(); // Sequential read for (int i = 0; i < 10; i++) { Bytes b = Bytes.allocateDirect(8); tailer.readBytes(b); Assert.assertEquals(expected.readInt(0), b.readInt(0)); b.release(); } } }
/** * copies the contents of bytes into a direct byte buffer * * @param bytes the bytes to wrap * @return a direct byte buffer contain the {@code bytes} */ @NotNull static Bytes allocateDirect(@NotNull byte[] bytes) throws IllegalArgumentException { Bytes<Void> result = allocateDirect(bytes.length); try { result.write(bytes); } catch (BufferOverflowException e) { throw new AssertionError(e); } return result; }
private static void testBytes() { Bytes bytes = Bytes.allocateDirect(32); long start = System.currentTimeMillis(); int count = 500000; for (int i = 0; i < count; i++) { bytes.clear(); String s = "a,b,c,d,1,2,3,4"; bytes.append8bit(s); a = bytes.parseUtf8(StopCharTesters.COMMA_STOP); b = bytes.parseUtf8(StopCharTesters.COMMA_STOP); c = bytes.parseUtf8(StopCharTesters.COMMA_STOP); d = bytes.parseUtf8(StopCharTesters.COMMA_STOP); e = (int) bytes.parseLong(); f = (int) bytes.parseLong(); g = (int) bytes.parseLong(); h = (int) bytes.parseLong(); } long time = (System.currentTimeMillis() - start) * 100000 / count; System.out.println("Bytes: Average time " + time / 1e2 + "us."); } }