diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/GapicUnbufferedReadableByteChannel.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/GapicUnbufferedReadableByteChannel.java index 2efc340c2..aa2bc9f60 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/GapicUnbufferedReadableByteChannel.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/GapicUnbufferedReadableByteChannel.java @@ -234,35 +234,42 @@ public void close() throws IOException { } private void drainQueue() throws IOException { - IOException ioException = null; - while (queue.nonEmpty()) { - try { - java.lang.Object queueValue = queue.poll(); - if (queueValue instanceof ReadObjectResponse) { - ReadObjectResponse resp = (ReadObjectResponse) queueValue; - ResponseContentLifecycleHandle handle = rclm.get(resp); - handle.close(); - } else if (queueValue == EOF_MARKER || queueValue instanceof Throwable) { - break; - } - } catch (IOException e) { - if (ioException == null) { - ioException = e; - } else if (ioException != e) { - ioException.addSuppressed(e); + boolean shouldInterupt = false; + try { + IOException ioException = null; + while (queue.nonEmpty()) { + try { + java.lang.Object queueValue = queue.poll(); + if (queueValue instanceof ReadObjectResponse) { + ReadObjectResponse resp = (ReadObjectResponse) queueValue; + ResponseContentLifecycleHandle handle = rclm.get(resp); + handle.close(); + } else if (queueValue == EOF_MARKER || queueValue instanceof Throwable) { + break; + } + } catch (IOException e) { + if (ioException == null) { + ioException = e; + } else if (ioException != e) { + ioException.addSuppressed(e); + } + } catch (InterruptedException e) { + shouldInterupt = true; + if (ioException == null) { + ioException = new InterruptedIOException(); + } else { + ioException.addSuppressed(e); + } } - } catch (InterruptedException e) { + } + if (ioException != null) { + throw ioException; + } + } finally { + if (shouldInterupt) { Thread.currentThread().interrupt(); - if (ioException == null) { - ioException = new InterruptedIOException(); - } else { - ioException.addSuppressed(e); - } } } - if (ioException != null) { - throw ioException; - } } ApiFuture getResult() {