public class HelloWorld { private static final byte[] hwBytes = "Hello, World!".getBytes(); public static void main(String[] args) { NativeBytes<Void> nativeBytes = Bytes.allocateElasticDirect(); nativeBytes.write(hwBytes, 0, hwBytes.length); long writePosition = nativeBytes.writePosition();
// is it normal to have to set the readLimit? Bytes<Void> bytesForRead1 = nativeBytes.bytesForRead().readLimit(writePosition); nativeBytes.write("Farewell, good-bye!");
// is it normal to have to set the readPosition? Bytes<Void> bytesForRead2 = nativeBytes.bytesForRead().readPosition(writePosition); System.out.println("nativeBytes: " + nativeBytes.toDebugString()); System.out.println("bytesForRead1: " + bytesForRead1.toString() + " " + bytesForRead1.toDebugString()); System.out.println("bytesForRead2: " + bytesForRead2.toString() + " " + bytesForRead2.toDebugString()); }}
import net.openhft.chronicle.queue.ExcerptAppender;
import net.openhft.chronicle.queue.ExcerptTailer;
import net.openhft.chronicle.queue.impl.RollingChronicleQueue;
import net.openhft.chronicle.wire.DocumentContext;
import net.openhft.chronicle.wire.Wires;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.concurrent.TimeoutException;
/**
* @author Rob Austin.
*/
public class Example {
public static void main(String[] args) throws TimeoutException, IOException {
try (final RollingChronicleQueue queue = SingleChronicleQueueBuilder.binary(getTmpDir()).build()) {
final ExcerptAppender appender = queue.createAppender();
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().write("my-bytes").bytes("Farewell, good-bye!".getBytes());
}
final ExcerptTailer tailer = queue.createTailer();
try (DocumentContext dc = tailer.readingDocument()) {
// you can remove this line in you want <----- ********
System.out.println(Wires.fromSizePrefixedBlobs(dc));
byte[] bytes = dc.wire().read(() -> "my-bytes").bytes();
System.out.println("tailer=" + new String(bytes));
}
// also take a look at
System.out.println("this is whats in the queue file=" + queue.dump());
}
}
// *************************************************************************
//
// *************************************************************************
public static File getTmpDir() throws IOException {
return Files.createTempDirectory("chronicle" + "-").toFile();
}
}
--
You received this message because you are subscribed to the Google Groups "Chronicle" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-chronicl...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
import net.openhft.chronicle.queue.ExcerptAppender;
import net.openhft.chronicle.queue.ExcerptTailer;
import net.openhft.chronicle.queue.impl.RollingChronicleQueue;
import net.openhft.chronicle.wire.DocumentContext;
import net.openhft.chronicle.wire.Wires;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;
/**
* @author Rob Austin.
*/
public class Example {
public static void main(String[] args) throws TimeoutException, IOException {
Map anExampleObject = new HashMap<>();
anExampleObject.put("some", "data");
try (final RollingChronicleQueue queue = SingleChronicleQueueBuilder.binary(getTmpDir()).build()) {
final ExcerptAppender appender = queue.createAppender();
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().write("my-map").object(anExampleObject);
}
final ExcerptTailer tailer = queue.createTailer();
try (DocumentContext dc = tailer.readingDocument()) {
// you can remove this line in you want <----- ********
System.out.println(Wires.fromSizePrefixedBlobs(dc));
Map result = dc.wire().read(() -> "my-map").object(Map.class);
System.out.println("tailer result=" + result.get("some"));
}
// also take a look at
System.out.println("this is whats in the queue file=" + queue.dump());
}
}
// *************************************************************************
//
// *************************************************************************
public static File getTmpDir() throws IOException {
return Files.createTempDirectory("chronicle" + "-").toFile();
}
}
On 17 Jun 2016, at 16:35, Andrew Oswald <andrew...@gmail.com> wrote:
Is this the sort of thing that you want to do
or extend AbstractMarshallable
Also you may find this BLOG useful
Do I need to explicitly assign the readPosition and readLimit to the Bytes that are given back to me through use of bytesForRead
I would have minimally thought its read position would have been automatically set...
The thought for assigning the respective byte[] was to utilise bytesForRead which would provide me with a BytesStore for use in serialization.
@Test
public void testBytesForRead() {
byte[] byteArr = new byte[128];
for (int i = 0; i < byteArr.length; i++)
byteArr[i] = (byte) i;
Bytes bytes = Bytes.wrapForRead(byteArr);
bytes.readSkip(8);
Bytes bytes2 = bytes.bytesForRead();
assertEquals(128 - 8, bytes2.readRemaining());
assertEquals(8, bytes2.readPosition());
assertEquals(8, bytes2.readByte(bytes2.start()));
assertEquals(9, bytes2.readByte(bytes2.start() + 1));
assertEquals(9, bytes.readByte(9));
bytes2.writeByte(bytes2.start() + 1, 99);
assertEquals(99, bytes.readByte(99));
}
--
@NotNull @Override public Bytes<Underlying> bytesForRead() throws IllegalStateException { return isClear() ? new VanillaBytes<>(bytesStore, writePosition(), bytesStore.writeLimit()) : new SubBytes<>(bytesStore, readPosition(), readLimit()); }
/** * @return is the readPosition at the start and the writeLimit at the end. */ default boolean isClear() { return start() == readPosition() && writeLimit() == capacity(); }
public class HelloWorld { private static final byte[] hwBytes = "Hello, World!".getBytes();
private static final byte[] ihBytes = "I'm here!".getBytes(); private static final byte[] gbBytes = "Farewell, good-bye!".getBytes();
public static void main(String[] args) { NativeBytes<Void> nativeBytes = Bytes.allocateElasticDirect();
// following two lines seem odd, but so be it?... nativeBytes.writeByte((byte)0); nativeBytes.readSkip(1); nativeBytes.write(hwBytes, 0, hwBytes.length); Bytes<Void> bytesForRead1 = nativeBytes.bytesForRead().readLimit(nativeBytes.writePosition()); nativeBytes.readSkip(hwBytes.length); nativeBytes.write(ihBytes); Bytes<Void> bytesForRead2 = nativeBytes.bytesForRead().readLimit(nativeBytes.writePosition()); nativeBytes.readSkip(ihBytes.length); nativeBytes.write(gbBytes); Bytes<Void> bytesForRead3 = nativeBytes.bytesForRead().readLimit(nativeBytes.writePosition()); System.out.println("nativeBytes: " + nativeBytes.toDebugString()); System.out.println("bytesForRead1: " + bytesForRead1.toString() + " " + bytesForRead1.toDebugString() + " " + bytesForRead1.readRemaining()); System.out.println("bytesForRead2: " + bytesForRead2.toString() + " " + bytesForRead2.toDebugString() + " " + bytesForRead2.readRemaining());; System.out.println("bytesForRead3: " + bytesForRead3.toString() + " " + bytesForRead3.toDebugString() + " " + bytesForRead3.readRemaining()); }}
nativeBytes: [pos: 23, rlim: 42, wlim: 8EiB, cap: 8EiB ] ٠Hello, World!I'm here!‖Farewell, good-bye!‡٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠bytesForRead1: Hello, World! [pos: 1, rlim: 14, wlim: 14, cap: 14 ] ‖Hello, World!‡ 13bytesForRead2: I'm here! [pos: 14, rlim: 23, wlim: 23, cap: 23 ] ‖I'm here!‡ 9bytesForRead3: Farewell, good-bye! [pos: 23, rlim: 42, wlim: 42, cap: 42 ] ‖Farewell, good-bye!‡ 19
ReadSkip moves the read position to where you want the start to be. You could just use readPosition(x) the readRemaining is the same after you do this.