From 63048cb1bc1327cc0c90fa444c1263a436081484 Mon Sep 17 00:00:00 2001 From: Joan-Roch Sala Date: Tue, 3 Jan 2023 11:18:22 +0000 Subject: [PATCH] use Optional as return type --- .../main/java/org/capnproto/Serialize.java | 10 +++---- .../java/org/capnproto/SerializePacked.java | 26 ++++++++++--------- .../java/org/capnproto/SerializeTest.java | 9 ++++--- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/runtime/src/main/java/org/capnproto/Serialize.java b/runtime/src/main/java/org/capnproto/Serialize.java index f7ee2f3..53511a0 100644 --- a/runtime/src/main/java/org/capnproto/Serialize.java +++ b/runtime/src/main/java/org/capnproto/Serialize.java @@ -27,8 +27,8 @@ import java.nio.ByteOrder; import java.nio.channels.*; import java.util.ArrayList; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; import java.util.function.Consumer; +import java.util.Optional; /** * Serialization using the standard (unpacked) stream encoding: @@ -87,7 +87,7 @@ public final class Serialize { * Attempts to read a message from the provided BufferedInputStream with default options. Returns null if the input * stream reached end-of-stream on first read. */ - public static MessageReader tryRead(ReadableByteChannel bc) throws IOException { + public static Optional tryRead(ReadableByteChannel bc) throws IOException { return tryRead(bc, ReaderOptions.DEFAULT_READER_OPTIONS); } @@ -95,21 +95,21 @@ public final class Serialize { * Attempts to read a message from the provided BufferedInputStream with the provided options. Returns null if the * input stream reached end-of-stream on first read. */ - public static MessageReader tryRead(ReadableByteChannel bc, ReaderOptions options) throws IOException { + public static Optional tryRead(ReadableByteChannel bc, ReaderOptions options) throws IOException { ByteBuffer firstWord = makeByteBufferForWords(1); int nBytes = tryFillBuffer(firstWord, bc); if (firstWord.hasRemaining()) { // We failed to read a whole word if (0 == nBytes) { // We were unable to read anything at all: the byte channel has reached end-of-stream - return null; + return Optional.empty(); } else { // We read fewer than 1 word's worth of bytes throw new IOException("premature EOF"); } } else { // We filled the buffer - return doRead(bc, options, firstWord); + return Optional.of(doRead(bc, options, firstWord)); } } diff --git a/runtime/src/main/java/org/capnproto/SerializePacked.java b/runtime/src/main/java/org/capnproto/SerializePacked.java index d6f565d..5f84df8 100644 --- a/runtime/src/main/java/org/capnproto/SerializePacked.java +++ b/runtime/src/main/java/org/capnproto/SerializePacked.java @@ -21,24 +21,26 @@ package org.capnproto; +import java.util.Optional; + /** * Serialization using the packed encoding: https://capnproto.org/encoding.html#packing */ public final class SerializePacked { /** - * Attempts to read a message from the provided BufferedInputStream with default options. Returns null if the input - * stream reached end-of-stream on first read. + * Attempts to read a message from the provided BufferedInputStream with default options. Returns an empty optional + * if the input stream reached end-of-stream on first read. */ - public static MessageReader tryRead(BufferedInputStream input) throws java.io.IOException { + public static Optional tryRead(BufferedInputStream input) throws java.io.IOException { return tryRead(input, ReaderOptions.DEFAULT_READER_OPTIONS); } /** - * Attempts to read a message from the provided BufferedInputStream with the provided options. Returns null if the - * input stream reached end-of-stream on first read. + * Attempts to read a message from the provided BufferedInputStream with the provided options. Returns an empty + * optional if the input stream reached end-of-stream on first read. */ - public static MessageReader tryRead(BufferedInputStream input, ReaderOptions options) throws java.io.IOException { + public static Optional tryRead(BufferedInputStream input, ReaderOptions options) throws java.io.IOException { PackedInputStream packedInput = new PackedInputStream(input); return Serialize.tryRead(packedInput, options); } @@ -60,18 +62,18 @@ public final class SerializePacked { /** * Wraps the provided ReadableByteChannel in a BufferedInputStream and attempts to read a message from it with - * default options. Returns null if the channel reached end-of-stream on first read. + * default options. Returns an empty optional if the channel reached end-of-stream on first read. */ - public static MessageReader tryReadFromUnbuffered(java.nio.channels.ReadableByteChannel input) throws java.io.IOException { + public static Optional tryReadFromUnbuffered(java.nio.channels.ReadableByteChannel input) throws java.io.IOException { return tryReadFromUnbuffered(input, ReaderOptions.DEFAULT_READER_OPTIONS); } /** * Wraps the provided ReadableByteChannel in a BufferedInputStream and attempts to read a message from it with - * the provided options. Returns null if the channel reached end-of-stream on first read. + * the provided options. Returns an empty optional if the channel reached end-of-stream on first read. */ - public static MessageReader tryReadFromUnbuffered(java.nio.channels.ReadableByteChannel input, - ReaderOptions options) throws java.io.IOException { + public static Optional tryReadFromUnbuffered(java.nio.channels.ReadableByteChannel input, + ReaderOptions options) throws java.io.IOException { PackedInputStream packedInput = new PackedInputStream(new BufferedInputStreamWrapper(input)); return Serialize.tryRead(packedInput, options); } @@ -115,7 +117,7 @@ public final class SerializePacked { * Serializes a MessageBuilder to an unbuffered output stream. */ public static void writeToUnbuffered(java.nio.channels.WritableByteChannel output, - MessageBuilder message) throws java.io.IOException { + MessageBuilder message) throws java.io.IOException { BufferedOutputStreamWrapper buffered = new BufferedOutputStreamWrapper(output); write(buffered, message); buffered.flush(); diff --git a/runtime/src/test/java/org/capnproto/SerializeTest.java b/runtime/src/test/java/org/capnproto/SerializeTest.java index 20f5803..b27303f 100644 --- a/runtime/src/test/java/org/capnproto/SerializeTest.java +++ b/runtime/src/test/java/org/capnproto/SerializeTest.java @@ -33,6 +33,7 @@ import org.junit.Ignore; import org.junit.Test; import java.util.Arrays; +import java.util.Optional; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -192,14 +193,14 @@ public class SerializeTest { // No padding // Segment 0 (empty) }; - MessageReader messageReader = Serialize.tryRead(new ArrayInputStream(ByteBuffer.wrap(input))); - Assert.assertNotNull(messageReader); + Optional messageReader = Serialize.tryRead(new ArrayInputStream(ByteBuffer.wrap(input))); + Assert.assertTrue(messageReader.isPresent()); } // `tryRead` returns null when given no input { - MessageReader messageReader = Serialize.tryRead(new ArrayInputStream(ByteBuffer.wrap(new byte[]{}))); - Assert.assertNull(messageReader); + Optional messageReader = Serialize.tryRead(new ArrayInputStream(ByteBuffer.wrap(new byte[]{}))); + Assert.assertFalse(messageReader.isPresent()); } // `tryRead` throws when given too few bytes to form the first word