-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Len = 0 and Insufficient Buffer behaviours for positioned reads #203
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import software.amazon.s3.analyticsaccelerator.common.Preconditions; | ||
|
||
/** | ||
* A SeekableInputStream is like a conventional InputStream but equipped with two additional | ||
|
@@ -57,4 +58,38 @@ public abstract class SeekableInputStream extends InputStream { | |
* @throws IOException if an error occurs while reading the file | ||
*/ | ||
public abstract int readTail(byte[] buf, int off, int n) throws IOException; | ||
|
||
/** | ||
* Validates the arguments for a read operation. This method is available to use in all subclasses | ||
* to ensure consistency. | ||
* | ||
* @param position the position to read from | ||
* @param buffer the buffer to read into | ||
* @param offset the offset in the buffer to start writing at | ||
* @param length the number of bytes to read | ||
* @throws IllegalArgumentException if the position, offset or length is negative | ||
* @throws NullPointerException if the buffer is null | ||
* @throws IndexOutOfBoundsException if the offset or length are invalid for the given buffer | ||
*/ | ||
protected void validatePositionedReadArgs(long position, byte[] buffer, int offset, int length) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe add a unit test for this method? should be quick There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well i didn't because it is a protected method and not sure if we want to expose it. Instead i added tests to S3SeekableInputStream class which is publicly exposed. Let me know if you think this is a good approach. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Happy with this approach! |
||
Preconditions.checkArgument(offset >= 0, "Offset is negative"); | ||
Preconditions.checkArgument(length >= 0, "Length is negative"); | ||
|
||
// TODO: S3A throws an EOFException here, S3FileIO does IllegalArgumentException | ||
// TODO: https://github.com/awslabs/analytics-accelerator-s3/issues/84 | ||
Preconditions.checkArgument(position >= 0, "Position is negative"); | ||
|
||
if (buffer == null) { | ||
throw new NullPointerException("Null destination buffer"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not just have both of these in precondition checks? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes perfect sense. Doing it now. |
||
} else if (length > buffer.length - offset) { | ||
throw new IndexOutOfBoundsException( | ||
"Too many bytes for destination buffer " | ||
+ ": request length=" | ||
+ length | ||
+ ", with offset =" | ||
+ offset | ||
+ "; buffer capacity =" | ||
+ (buffer.length - offset)); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shall we move this validation as well to the method where we did the previous validations, so that all the validations are in same place?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good recommendation. I did not move this validation because full content length might not be available to other streams those will implement this class. In S3 case we know the size thanks to object metadata. Let me know what you think.