8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified

Explicitly verify that the channel is open

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2017-11-03 10:18:27 -07:00
parent 3803dc1401
commit 87eb47a3c8
2 changed files with 47 additions and 2 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -368,6 +368,10 @@ public final class Channels {
@Override @Override
public int read(ByteBuffer dst) throws IOException { public int read(ByteBuffer dst) throws IOException {
if (!isOpen()) {
throw new ClosedChannelException();
}
int len = dst.remaining(); int len = dst.remaining();
int totalRead = 0; int totalRead = 0;
int bytesRead = 0; int bytesRead = 0;
@ -442,6 +446,10 @@ public final class Channels {
@Override @Override
public int write(ByteBuffer src) throws IOException { public int write(ByteBuffer src) throws IOException {
if (!isOpen()) {
throw new ClosedChannelException();
}
int len = src.remaining(); int len = src.remaining();
int totalWritten = 0; int totalWritten = 0;
synchronized (writeLock) { synchronized (writeLock) {

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -167,6 +167,11 @@ public class Basic {
readAndCheck(blah); readAndCheck(blah);
blah.delete(); blah.delete();
testNewChannelWriteAfterClose(blah);
testNewChannelReadAfterClose(blah);
blah.delete();
writeOut(blah, ITERATIONS); writeOut(blah, ITERATIONS);
testNewChannelIn(blah); testNewChannelIn(blah);
test4481572(blah); test4481572(blah);
@ -255,6 +260,7 @@ public class Basic {
private static void testNewChannelOut(File blah) throws Exception { private static void testNewChannelOut(File blah) throws Exception {
ExtendedFileOutputStream fos = new ExtendedFileOutputStream(blah); ExtendedFileOutputStream fos = new ExtendedFileOutputStream(blah);
WritableByteChannel wbc = Channels.newChannel(fos); WritableByteChannel wbc = Channels.newChannel(fos);
for (int i=0; i<ITERATIONS; i++) for (int i=0; i<ITERATIONS; i++)
wbc.write(ByteBuffer.wrap(message.getBytes(encoding))); wbc.write(ByteBuffer.wrap(message.getBytes(encoding)));
wbc.close(); wbc.close();
@ -287,6 +293,37 @@ public class Basic {
fis.close(); fis.close();
} }
private static void testNewChannelWriteAfterClose(File blah)
throws Exception {
try (ExtendedFileOutputStream fos =
new ExtendedFileOutputStream(blah)) {
WritableByteChannel wbc = Channels.newChannel(fos);
wbc.close();
try {
wbc.write(ByteBuffer.allocate(0));
throw new RuntimeException
("No ClosedChannelException on WritableByteChannel::write");
} catch (ClosedChannelException expected) {
}
}
}
private static void testNewChannelReadAfterClose(File blah)
throws Exception {
try (ExtendedFileInputStream fis = new ExtendedFileInputStream(blah)) {
ReadableByteChannel rbc = Channels.newChannel(fis);
rbc.close();
try {
rbc.read(ByteBuffer.allocate(0));
throw new RuntimeException
("No ClosedChannelException on ReadableByteChannel::read");
} catch (ClosedChannelException expected) {
}
}
}
// Causes BufferOverflowException if bug 4481572 is present. // Causes BufferOverflowException if bug 4481572 is present.
private static void test4481572(File blah) throws Exception { private static void test4481572(File blah) throws Exception {
ExtendedFileInputStream fis = new ExtendedFileInputStream(blah); ExtendedFileInputStream fis = new ExtendedFileInputStream(blah);