lukecwik commented on code in PR #17802:
URL: https://github.com/apache/beam/pull/17802#discussion_r900280397
##########
sdks/java/core/src/main/java/org/apache/beam/sdk/util/CoderUtils.java:
##########
@@ -107,6 +107,25 @@ public static <T> T decodeFromByteArray(
}
}
+ /** Decodes a value from the given stream, validating that no bytes are
remaining once decoded. */
+ public static <T> T decodeFromStream(Coder<T> coder, InputStream stream)
throws IOException {
+ return decodeFromStream(coder, stream, Coder.Context.OUTER);
+ }
+
+ /**
+ * Decodes a value from the given stream using a given context, validating
that no bytes are
+ * remaining once decoded.
+ */
+ public static <T> T decodeFromStream(Coder<T> coder, InputStream stream,
Coder.Context context)
+ throws IOException {
+ T result = coder.decode(stream, context);
+ if (stream.available() != 0) {
+ throw new CoderException(
+ stream.available() + " unexpected extra bytes after decoding " +
result);
+ }
+ return result;
+ }
Review Comment:
This doesn't work for all streams since streams can choose to always return
0 or will return 0 if there is a blocking read coming.
https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available()
```
Returns an estimate of the number of bytes that can be read (or skipped
over) from this input stream without blocking by the next invocation of a
method for this input stream. The next invocation might be the same thread or
another thread. A single read or skip of this many bytes will not block, but
may read or skip fewer bytes.
Note that while some implementations of InputStream will return the total
number of bytes in the stream, many will not. It is never correct to use the
return value of this method to allocate a buffer intended to hold all data in
this stream.
A subclass' implementation of this method may choose to throw an
[IOException](https://docs.oracle.com/javase/7/docs/api/java/io/IOException.html)
if this input stream has been closed by invoking the
[close()](https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#close())
method.
The available method for class InputStream always returns 0.
This method should be overridden by subclasses.
```
Unfortunately the only way to ensure this is to limit the input type as a
ByteString which explicitly declares that `available()` will always return
`size - current_pos`.
Here is what I was thinking:
```suggestion
/** Decodes a value from the given bytes, validating that no bytes are
remaining once decoded. */
public static <T> T decodeFromByteString(Coder<T> coder, ByteString
encodedValue) throws IOException {
return decodeFromByteString(coder, encodedValue, Coder.Context.OUTER);
}
/**
* Decodes a value from the given bytes using a given context, validating
that no bytes
* are remaining once decoded.
*/
public static <T> T decodeFromByteString(Coder<T> coder, ByteString
encodedValue, Coder.Context context)
throws IOException {
InputStream stream = encodedValue.newInput();
T result = coder.decode(stream, context);
if (stream.available() != 0) {
throw new CoderException(
stream.available() + " unexpected extra bytes after decoding " +
result);
}
return result;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]