Skip to content

Commit 119ccdf

Browse files
committed
Merge pull request #669 from mziccard/validate-open-exception
Make validateOpen throw ClosedChannelException
2 parents 332f990 + 28f4a18 commit 119ccdf

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

gcloud-java-core/src/main/java/com/google/gcloud/BaseWriteChannel.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.io.Serializable;
2323
import java.nio.ByteBuffer;
24+
import java.nio.channels.ClosedChannelException;
2425
import java.util.Arrays;
2526
import java.util.Objects;
2627

@@ -114,9 +115,9 @@ private void flush() {
114115
}
115116
}
116117

117-
private void validateOpen() throws IOException {
118+
private void validateOpen() throws ClosedChannelException {
118119
if (!isOpen) {
119-
throw new IOException("stream is closed");
120+
throw new ClosedChannelException();
120121
}
121122
}
122123

gcloud-java-core/src/test/java/com/google/gcloud/BaseWriteChannelTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.io.IOException;
3333
import java.io.Serializable;
3434
import java.nio.ByteBuffer;
35+
import java.nio.channels.ClosedChannelException;
3536
import java.util.Arrays;
3637
import java.util.Random;
3738

@@ -102,8 +103,7 @@ public void testClose() throws IOException {
102103
@Test
103104
public void testValidateOpen() throws IOException {
104105
channel.close();
105-
thrown.expect(IOException.class);
106-
thrown.expectMessage("stream is closed");
106+
thrown.expect(ClosedChannelException.class);
107107
channel.write(ByteBuffer.allocate(42));
108108
}
109109

gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobReadChannel.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.io.IOException;
3030
import java.io.Serializable;
3131
import java.nio.ByteBuffer;
32+
import java.nio.channels.ClosedChannelException;
3233
import java.util.Map;
3334
import java.util.Objects;
3435
import java.util.concurrent.Callable;
@@ -55,7 +56,7 @@ class BlobReadChannel implements ReadChannel {
5556
private byte[] buffer;
5657

5758
BlobReadChannel(StorageOptions serviceOptions, BlobId blob,
58-
Map<StorageRpc.Option, ?> requestOptions) {
59+
Map<StorageRpc.Option, ?> requestOptions) {
5960
this.serviceOptions = serviceOptions;
6061
this.blob = blob;
6162
this.requestOptions = requestOptions;
@@ -91,9 +92,9 @@ public void close() {
9192
}
9293
}
9394

94-
private void validateOpen() throws IOException {
95+
private void validateOpen() throws ClosedChannelException {
9596
if (!isOpen) {
96-
throw new IOException("stream is closed");
97+
throw new ClosedChannelException();
9798
}
9899
}
99100

gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
import java.io.IOException;
4141
import java.nio.ByteBuffer;
42+
import java.nio.channels.ClosedChannelException;
4243
import java.util.Arrays;
4344
import java.util.Map;
4445
import java.util.Random;
@@ -156,15 +157,15 @@ public void testClose() {
156157
}
157158

158159
@Test
159-
public void testReadClosed() {
160+
public void testReadClosed() throws IOException {
160161
replay(storageRpcMock);
161162
reader = new BlobReadChannel(options, BLOB_ID, EMPTY_RPC_OPTIONS);
162163
reader.close();
163164
try {
164165
ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
165166
reader.read(readBuffer);
166-
fail("Expected BlobReadChannel read to throw IOException");
167-
} catch (IOException ex) {
167+
fail("Expected BlobReadChannel read to throw ClosedChannelException");
168+
} catch (ClosedChannelException ex) {
168169
// expected
169170
}
170171
}

0 commit comments

Comments
 (0)