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.nio.channels.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Serialization using the standard (unpacked) stream encoding:
|
* 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
|
* 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.
|
* 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);
|
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
|
* 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.
|
* 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);
|
ByteBuffer firstWord = makeByteBufferForWords(1);
|
||||||
int nBytes = tryFillBuffer(firstWord, bc);
|
int nBytes = tryFillBuffer(firstWord, bc);
|
||||||
if (firstWord.hasRemaining()) {
|
if (firstWord.hasRemaining()) {
|
||||||
// We failed to read a whole word
|
// We failed to read a whole word
|
||||||
if (0 == nBytes) {
|
if (0 == nBytes) {
|
||||||
// We were unable to read anything at all: the byte channel has reached end-of-stream
|
// We were unable to read anything at all: the byte channel has reached end-of-stream
|
||||||
return null;
|
return Optional.empty();
|
||||||
} else {
|
} else {
|
||||||
// We read fewer than 1 word's worth of bytes
|
// We read fewer than 1 word's worth of bytes
|
||||||
throw new IOException("premature EOF");
|
throw new IOException("premature EOF");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// We filled the buffer
|
// We filled the buffer
|
||||||
return doRead(bc, options, firstWord);
|
return Optional.of(doRead(bc, options, firstWord));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,24 +21,26 @@
|
||||||
|
|
||||||
package org.capnproto;
|
package org.capnproto;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Serialization using the packed encoding: https://capnproto.org/encoding.html#packing
|
* Serialization using the packed encoding: https://capnproto.org/encoding.html#packing
|
||||||
*/
|
*/
|
||||||
public final class SerializePacked {
|
public final class SerializePacked {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to read a message from the provided BufferedInputStream with default options. Returns null if the input
|
* Attempts to read a message from the provided BufferedInputStream with default options. Returns an empty optional
|
||||||
* stream reached end-of-stream on first read.
|
* 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);
|
return tryRead(input, ReaderOptions.DEFAULT_READER_OPTIONS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to read a message from the provided BufferedInputStream with the provided options. Returns null if the
|
* Attempts to read a message from the provided BufferedInputStream with the provided options. Returns an empty
|
||||||
* input stream reached end-of-stream on first read.
|
* 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);
|
PackedInputStream packedInput = new PackedInputStream(input);
|
||||||
return Serialize.tryRead(packedInput, options);
|
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
|
* 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);
|
return tryReadFromUnbuffered(input, ReaderOptions.DEFAULT_READER_OPTIONS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wraps the provided ReadableByteChannel in a BufferedInputStream and attempts to read a message from it with
|
* 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 {
|
ReaderOptions options) throws java.io.IOException {
|
||||||
PackedInputStream packedInput = new PackedInputStream(new BufferedInputStreamWrapper(input));
|
PackedInputStream packedInput = new PackedInputStream(new BufferedInputStreamWrapper(input));
|
||||||
return Serialize.tryRead(packedInput, options);
|
return Serialize.tryRead(packedInput, options);
|
||||||
}
|
}
|
||||||
|
@ -115,7 +117,7 @@ public final class SerializePacked {
|
||||||
* Serializes a MessageBuilder to an unbuffered output stream.
|
* Serializes a MessageBuilder to an unbuffered output stream.
|
||||||
*/
|
*/
|
||||||
public static void writeToUnbuffered(java.nio.channels.WritableByteChannel output,
|
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);
|
BufferedOutputStreamWrapper buffered = new BufferedOutputStreamWrapper(output);
|
||||||
write(buffered, message);
|
write(buffered, message);
|
||||||
buffered.flush();
|
buffered.flush();
|
||||||
|
|
|
@ -33,6 +33,7 @@ import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
@ -192,14 +193,14 @@ public class SerializeTest {
|
||||||
// No padding
|
// No padding
|
||||||
// Segment 0 (empty)
|
// Segment 0 (empty)
|
||||||
};
|
};
|
||||||
MessageReader messageReader = Serialize.tryRead(new ArrayInputStream(ByteBuffer.wrap(input)));
|
Optional<MessageReader> messageReader = Serialize.tryRead(new ArrayInputStream(ByteBuffer.wrap(input)));
|
||||||
Assert.assertNotNull(messageReader);
|
Assert.assertTrue(messageReader.isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
// `tryRead` returns null when given no input
|
// `tryRead` returns null when given no input
|
||||||
{
|
{
|
||||||
MessageReader messageReader = Serialize.tryRead(new ArrayInputStream(ByteBuffer.wrap(new byte[]{})));
|
Optional<MessageReader> messageReader = Serialize.tryRead(new ArrayInputStream(ByteBuffer.wrap(new byte[]{})));
|
||||||
Assert.assertNull(messageReader);
|
Assert.assertFalse(messageReader.isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
// `tryRead` throws when given too few bytes to form the first word
|
// `tryRead` throws when given too few bytes to form the first word
|
||||||
|
|
Loading…
Reference in a new issue