Skip to content

Instantly share code, notes, and snippets.

@stephenh
Created April 18, 2013 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stephenh/5415089 to your computer and use it in GitHub Desktop.
Save stephenh/5415089 to your computer and use it in GitHub Desktop.
retries
public synchronized int read() throws IOException {
int result = in.read();
if (result != -1) {
pos++;
}
return result;
}
public synchronized int read() throws IOException
{
int result = -1;
try {
result = this.pair.in.read();
} catch (IOException e) {
NativeS3FileSystem.LOG.info("Received IOException while reading '" + this.pair.key + "', attempting to reopen.");
seek(this.pos);
result = this.pair.in.read();
}
if (result == -1) {
int numRetries = 5;
if ((this.pair.contentLength != -1L) && (this.pos != this.pair.contentLength)) {
NativeS3FileSystem.LOG.info("Unexpected end of stream in read() while reading from stream pos=" + this.pos + ", contentLength=" + this.pair.contentLength);
NativeS3FileSystem.LOG.info("result=" + result + " and numRetries=" + numRetries);
while ((result <= 0) && (numRetries > 0)) {
NativeS3FileSystem.LOG.info("Retry reading from stream " + this.pos + ", contentLength=" + this.pair.contentLength);
seek(this.pos);
result = this.pair.in.read();
--numRetries;
}
if (result <= 0) {
NativeS3FileSystem.LOG.error("Unable to recover reading from stream");
throw new IOException("Unexpected end of stream pos=" + this.pos + " contentLength= " + this.pair.contentLength);
}
}
}
if (result > 0) {
advance(result);
}
return result;
}
@Override
public synchronized int read(byte[] b, int off, int len)
throws IOException {
int result = -1;
try {
result = in.read(b, off, len);
} catch (IOException e) {
LOG.info("Received IOException while reading '" + key + "', attempting to reopen.");
seek(pos);
result = in.read(b, off, len);
}
if (result > 0) {
pos += result;
}
if (statistics != null && result > 0) {
statistics.incrementBytesRead(result);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment