rdblue commented on code in PR #4608:
URL: https://github.com/apache/iceberg/pull/4608#discussion_r856671582
##########
aws/src/main/java/org/apache/iceberg/aws/s3/S3InputStream.java:
##########
@@ -111,6 +114,19 @@ public int read(byte[] b, int off, int len) throws
IOException {
return bytesRead;
}
+ @Override
+ public void readFully(long position, byte[] buffer, int offset, int length) {
+ GetObjectRequest.Builder requestBuilder = GetObjectRequest.builder()
+ .bucket(location.bucket())
+ .key(location.key())
+ .range(String.format("bytes=%s-%s", position, position + length));
+
+ S3RequestUtil.configureEncryption(awsProperties, requestBuilder);
+
+ ResponseBytes<GetObjectResponse> response =
s3.getObject(requestBuilder.build(), ResponseTransformer.toBytes());
Review Comment:
Looks like the `toBytes` transformer is really expensive:
```java
/**
* Reads and returns the rest of the given input stream as a byte array.
* Caller is responsible for closing the given input stream.
*/
public static byte[] toByteArray(InputStream is) throws IOException {
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
byte[] b = new byte[BUFFER_SIZE];
int n = 0;
while ((n = is.read(b)) != -1) {
output.write(b, 0, n);
}
return output.toByteArray();
}
}
```
While that implements the `readFully` logic for us, it doesn't seem worth
creating a byte array output stream (and allocating arrays within it),
allocating the temporary buffer per call, and copying the data twice, only to
copy it again here.
I think we should take the `readFully` implementation from `IcebergDecoder`
and update it to use offset/length:
```java
/**
* Reads a buffer from a stream, making multiple read calls if necessary.
*
* @param stream an InputStream to read from
* @param bytes a buffer
* @param offset starting offset in the buffer for the data
* @param length length of bytes to copy from the input stream to the
buffer
* @return true if the buffer is complete, false otherwise (stream ended)
* @throws IOException if there is an error while reading
*/
@SuppressWarnings("checkstyle:InnerAssignment")
private boolean readFully(InputStream stream, byte[] bytes, int offset,
int length)
throws IOException {
int pos = offset;
int bytesRead;
while ((length - pos) > 0 &&
(bytesRead = stream.read(bytes, pos, length - pos)) > 0) {
pos += bytesRead;
}
return pos == length;
}
```
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]