Merge "Adds the lazy seek"

This commit is contained in:
Jenkins 2016-11-28 15:32:29 +00:00 committed by Gerrit Code Review
commit d95ffe7014
1 changed files with 22 additions and 0 deletions

View File

@ -89,6 +89,8 @@ class SwiftNativeInputStream extends FSInputStream {
*/
private long rangeOffset = 0;
private long nextReadPosition = 0;
public SwiftNativeInputStream(SwiftNativeFileSystemStore storeNative,
FileSystem.Statistics statistics, Path path, long bufferSize)
throws IOException {
@ -138,6 +140,7 @@ class SwiftNativeInputStream extends FSInputStream {
verifyOpen();
int result = -1;
try {
seekStream();
result = httpStream.read();
} catch (IOException e) {
String msg = "IOException while reading " + path
@ -297,6 +300,13 @@ class SwiftNativeInputStream extends FSInputStream {
*/
@Override
public synchronized void seek(long targetPos) throws IOException {
if (targetPos < 0) {
throw new IOException("Negative Seek offset not supported");
}
nextReadPosition = targetPos;
}
public synchronized void realSeek(long targetPos) throws IOException {
if (targetPos < 0) {
throw new IOException("Negative Seek offset not supported");
}
@ -344,6 +354,18 @@ class SwiftNativeInputStream extends FSInputStream {
fillBuffer(targetPos);
}
/**
* Lazy seek.
* @throws IOException
*/
private void seekStream() throws IOException {
if (httpStream != null && nextReadPosition == pos) {
// already at specified position
return;
}
realSeek(nextReadPosition);
}
/**
* Fill the buffer from the target position
* If the target position == current position, the