use Optional<MessageReader> as return type
This commit is contained in:
parent
ce32e367a3
commit
63048cb1bc
3 changed files with 24 additions and 21 deletions
|
@ -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<MessageReader> 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<MessageReader> 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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<MessageReader> 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<MessageReader> tryRead(BufferedInputStream input, ReaderOptions options) throws java.io.IOException {
|
||||
PackedInputStream packedInput = new PackedInputStream(input);
|
||||
return Serialize.tryRead(packedInput, options);
|
||||
}
|
||||
|
@ -60,17 +62,17 @@ 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<MessageReader> 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,
|
||||
public static Optional<MessageReader> tryReadFromUnbuffered(java.nio.channels.ReadableByteChannel input,
|
||||
ReaderOptions options) throws java.io.IOException {
|
||||
PackedInputStream packedInput = new PackedInputStream(new BufferedInputStreamWrapper(input));
|
||||
return Serialize.tryRead(packedInput, options);
|
||||
|
|
|
@ -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> 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> 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
|
||||
|
|
Loading…
Reference in a new issue