From 28f4a18ae41148f82ffe11bbb311f92f50da180f Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 25 Feb 2016 17:05:55 +0100 Subject: [PATCH] validateOpen throws ClosedChannelException in BaseWriteChannel and BlobReadChannel --- .../src/main/java/com/google/gcloud/BaseWriteChannel.java | 5 +++-- .../test/java/com/google/gcloud/BaseWriteChannelTest.java | 4 ++-- .../java/com/google/gcloud/storage/BlobReadChannel.java | 7 ++++--- .../com/google/gcloud/storage/BlobReadChannelTest.java | 7 ++++--- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/BaseWriteChannel.java b/gcloud-java-core/src/main/java/com/google/gcloud/BaseWriteChannel.java index e05383a65826..1d18a5a27e81 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/BaseWriteChannel.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/BaseWriteChannel.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.Serializable; import java.nio.ByteBuffer; +import java.nio.channels.ClosedChannelException; import java.util.Arrays; import java.util.Objects; @@ -114,9 +115,9 @@ private void flush() { } } - private void validateOpen() throws IOException { + private void validateOpen() throws ClosedChannelException { if (!isOpen) { - throw new IOException("stream is closed"); + throw new ClosedChannelException(); } } diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/BaseWriteChannelTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/BaseWriteChannelTest.java index e49a17b019e0..6d5306a3bc7f 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/BaseWriteChannelTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/BaseWriteChannelTest.java @@ -32,6 +32,7 @@ import java.io.IOException; import java.io.Serializable; import java.nio.ByteBuffer; +import java.nio.channels.ClosedChannelException; import java.util.Arrays; import java.util.Random; @@ -102,8 +103,7 @@ public void testClose() throws IOException { @Test public void testValidateOpen() throws IOException { channel.close(); - thrown.expect(IOException.class); - thrown.expectMessage("stream is closed"); + thrown.expect(ClosedChannelException.class); channel.write(ByteBuffer.allocate(42)); } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobReadChannel.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobReadChannel.java index 121f2eb63589..2b9643434ecc 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobReadChannel.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobReadChannel.java @@ -29,6 +29,7 @@ import java.io.IOException; import java.io.Serializable; import java.nio.ByteBuffer; +import java.nio.channels.ClosedChannelException; import java.util.Map; import java.util.Objects; import java.util.concurrent.Callable; @@ -55,7 +56,7 @@ class BlobReadChannel implements ReadChannel { private byte[] buffer; BlobReadChannel(StorageOptions serviceOptions, BlobId blob, - Map requestOptions) { + Map requestOptions) { this.serviceOptions = serviceOptions; this.blob = blob; this.requestOptions = requestOptions; @@ -91,9 +92,9 @@ public void close() { } } - private void validateOpen() throws IOException { + private void validateOpen() throws ClosedChannelException { if (!isOpen) { - throw new IOException("stream is closed"); + throw new ClosedChannelException(); } } diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java index ffb37e8c5032..5dc947df51f8 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java @@ -39,6 +39,7 @@ import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.channels.ClosedChannelException; import java.util.Arrays; import java.util.Map; import java.util.Random; @@ -156,15 +157,15 @@ public void testClose() { } @Test - public void testReadClosed() { + public void testReadClosed() throws IOException { replay(storageRpcMock); reader = new BlobReadChannel(options, BLOB_ID, EMPTY_RPC_OPTIONS); reader.close(); try { ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE); reader.read(readBuffer); - fail("Expected BlobReadChannel read to throw IOException"); - } catch (IOException ex) { + fail("Expected BlobReadChannel read to throw ClosedChannelException"); + } catch (ClosedChannelException ex) { // expected } }