Java Code Examples for net.openhft.chronicle.wire.Wires#lengthOf()

The following examples show how to use net.openhft.chronicle.wire.Wires#lengthOf() . 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: RollEOFTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
private void removeEOF(Path path) throws IOException {
    long blockSize = 64 << 10;
    long chunkSize = OS.pageAlign(blockSize);
    long overlapSize = OS.pageAlign(blockSize / 4);
    final MappedBytes mappedBytes = MappedBytes.mappedBytes(path.toFile(), chunkSize, overlapSize, false);
    mappedBytes.reserve(test);
    try {
        final Wire wire = WireType.BINARY_LIGHT.apply(mappedBytes);
        final Bytes<?> bytes = wire.bytes();
        bytes.readLimitToCapacity();
        bytes.readSkip(4);
        // move past header
        try (final SingleChronicleQueueStore qs = loadStore(wire)) {
            assertNotNull(qs);
            long l = qs.writePosition();
            long len = Wires.lengthOf(bytes.readVolatileInt(l));
            long eofOffset = l + len + 4L;
            bytes.writePosition(eofOffset);
            bytes.writeInt(0);
        }
    } finally {
        mappedBytes.release(test);
    }
}
 
Example 2
Source File: VerySimpleClient.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
private void readDocument(Wire inMetaDataWire) throws IOException {
    ByteBuffer inBuff = (ByteBuffer) inMetaDataWire.bytes().underlyingObject();

    // write the data to the socket
    long start = inMetaDataWire.bytes().writePosition();

    while (inBuff.position() < 4 + start)
        client.read(inBuff);

    int len = Wires.lengthOf(inMetaDataWire.bytes().readInt(start));
    inMetaDataWire.bytes().readLimit(start + 4 + len);
    while (inBuff.position() < 4 + len + start)
        client.read(inBuff);
}
 
Example 3
Source File: VerySimpleClientTest.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
private void readDocument(@NotNull Wire inMetaDataWire) throws IOException {
    @Nullable ByteBuffer inBuff = (ByteBuffer) inMetaDataWire.bytes().underlyingObject();

    // write the data to the socket
    long start = inMetaDataWire.bytes().readPosition();
    while (inBuff.position() + start < 4)
        client.read(inBuff);

    inMetaDataWire.bytes().writePosition(inBuff.position());
    int len = Wires.lengthOf(inMetaDataWire.bytes().readInt(start));
    while (inBuff.position() < 4 + len + start)
        client.read(inBuff);
}
 
Example 4
Source File: RawAccessJava.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@Test
public void Tailer() {
    if (!assert_from_cpp())
        return;

    String tmp = "/dev/shm/RawAccessCtoJ";
    System.out.println(tmp); // so C++ knows this ran rather than skipped

    try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build()) {

        ExcerptTailer tailer = cq.createTailer();

        for (int i = 0; i < COUNT; ++i) {
            try (DocumentContext dc = tailer.readingDocument()) {

                Bytes bytes = dc.wire().bytes();

                bytes.readSkip(-QUEUE_HEADER_SIZE);
                int header = bytes.readInt();

                // document length, inc 4-byte length
                int length = Wires.lengthOf(header);

                // actual length of data
                int data_length = bytes.readInt();

                assertEquals(bytes.readByte(), (byte) 0xab);
                assertEquals(bytes.readShort(), (short) 12);
                assertEquals(bytes.readInt(), 123);
                assertEquals(bytes.readLong(), 123456789L);
                assertEquals(bytes.readFloat(), 1.234f, 1.0e-7);
                assertEquals(bytes.readDouble(), 123.456, 1.0e-7);
                assertEquals(bytes.readChar(), 'a');

                StringBuilder sb = new StringBuilder();
                bytes.read8bit(sb);
                assertEquals(sb.toString(), "Hello World");
            }
        }
    }
}