8196298: Add null Reader and Writer
Reviewed-by: bpb, forax, smarks, alanb, rriggs
This commit is contained in:
parent
ec605aa039
commit
4c7f1ac52c
src/java.base/share/classes/java/io
test/jdk/java/io
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -26,6 +26,7 @@
|
||||
package java.io;
|
||||
|
||||
|
||||
import java.nio.CharBuffer;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@ -54,6 +55,85 @@ public abstract class Reader implements Readable, Closeable {
|
||||
|
||||
private static final int TRANSFER_BUFFER_SIZE = 8192;
|
||||
|
||||
/**
|
||||
* Returns a new {@code Reader} that reads no characters. The returned
|
||||
* stream is initially open. The stream is closed by calling the
|
||||
* {@code close()} method. Subsequent calls to {@code close()} have no
|
||||
* effect.
|
||||
*
|
||||
* <p> While the stream is open, the {@code read()}, {@code read(char[])},
|
||||
* {@code read(char[], int, int)}, {@code read(Charbuffer)}, {@code
|
||||
* ready())}, {@code skip(long)}, and {@code transferTo()} methods all
|
||||
* behave as if end of stream has been reached. After the stream has been
|
||||
* closed, these methods all throw {@code IOException}.
|
||||
*
|
||||
* <p> The {@code markSupported()} method returns {@code false}. The
|
||||
* {@code mark()} method does nothing, and the {@code reset()} method
|
||||
* throws {@code IOException}.
|
||||
*
|
||||
* <p> The {@link #lock object} used to synchronize operations on the
|
||||
* returned {@code Reader} is not specified.
|
||||
*
|
||||
* @return a {@code Reader} which reads no characters
|
||||
*
|
||||
* @since 11
|
||||
*/
|
||||
public static Reader nullReader() {
|
||||
return new Reader() {
|
||||
private volatile boolean closed;
|
||||
|
||||
private void ensureOpen() throws IOException {
|
||||
if (closed) {
|
||||
throw new IOException("Stream closed");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
ensureOpen();
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(char[] cbuf, int off, int len) throws IOException {
|
||||
Objects.checkFromIndexSize(off, len, cbuf.length);
|
||||
ensureOpen();
|
||||
if (len == 0) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(CharBuffer target) throws IOException {
|
||||
Objects.requireNonNull(target);
|
||||
ensureOpen();
|
||||
if (target.hasRemaining()) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long skip(long n) throws IOException {
|
||||
ensureOpen();
|
||||
return 0L;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long transferTo(Writer out) throws IOException {
|
||||
Objects.requireNonNull(out);
|
||||
ensureOpen();
|
||||
return 0L;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
closed = true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The object used to synchronize operations on this stream. For
|
||||
* efficiency, a character-stream object may use an object other than
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -26,6 +26,8 @@
|
||||
package java.io;
|
||||
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Abstract class for writing to character streams. The only methods that a
|
||||
* subclass must implement are write(char[], int, int), flush(), and close().
|
||||
@ -58,6 +60,91 @@ public abstract class Writer implements Appendable, Closeable, Flushable {
|
||||
*/
|
||||
private static final int WRITE_BUFFER_SIZE = 1024;
|
||||
|
||||
/**
|
||||
* Returns a new {@code Writer} which discards all characters. The
|
||||
* returned stream is initially open. The stream is closed by calling
|
||||
* the {@code close()} method. Subsequent calls to {@code close()} have
|
||||
* no effect.
|
||||
*
|
||||
* <p> While the stream is open, the {@code append(char)}, {@code
|
||||
* append(CharSequence)}, {@code append(CharSequence, int, int)},
|
||||
* {@code flush()}, {@code write(int)}, {@code write(char[])}, and
|
||||
* {@code write(char[], int, int)} methods do nothing. After the stream
|
||||
* has been closed, these methods all throw {@code IOException}.
|
||||
*
|
||||
* <p> The {@link #lock object} used to synchronize operations on the
|
||||
* returned {@code Writer} is not specified.
|
||||
*
|
||||
* @return a {@code Writer} which discards all characters
|
||||
*
|
||||
* @since 11
|
||||
*/
|
||||
public static Writer nullWriter() {
|
||||
return new Writer() {
|
||||
private volatile boolean closed;
|
||||
|
||||
private void ensureOpen() throws IOException {
|
||||
if (closed) {
|
||||
throw new IOException("Stream closed");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Writer append(char c) throws IOException {
|
||||
ensureOpen();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Writer append(CharSequence csq) throws IOException {
|
||||
ensureOpen();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Writer append(CharSequence csq, int start, int end) throws IOException {
|
||||
ensureOpen();
|
||||
if (csq != null) {
|
||||
Objects.checkFromToIndex(start, end, csq.length());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int c) throws IOException {
|
||||
ensureOpen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(char[] cbuf, int off, int len) throws IOException {
|
||||
Objects.checkFromIndexSize(off, len, cbuf.length);
|
||||
ensureOpen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(String str) throws IOException {
|
||||
Objects.requireNonNull(str);
|
||||
ensureOpen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(String str, int off, int len) throws IOException {
|
||||
Objects.checkFromIndexSize(off, len, str.length());
|
||||
ensureOpen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() throws IOException {
|
||||
ensureOpen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
closed = true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The object used to synchronize operations on this stream. For
|
||||
* efficiency, a character-stream object may use an object other than
|
||||
|
140
test/jdk/java/io/Reader/NullReader.java
Normal file
140
test/jdk/java/io/Reader/NullReader.java
Normal file
@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.io.Reader;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.ReadOnlyBufferException;
|
||||
|
||||
import org.testng.annotations.AfterGroups;
|
||||
import org.testng.annotations.BeforeGroups;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8196298
|
||||
* @run testng NullReader
|
||||
* @summary Check for expected behavior of Reader.nullReader().
|
||||
*/
|
||||
public class NullReader {
|
||||
private static Reader openReader;
|
||||
private static Reader closedReader;
|
||||
|
||||
@BeforeGroups(groups = "open")
|
||||
public static void openStream() {
|
||||
openReader = Reader.nullReader();
|
||||
}
|
||||
|
||||
@BeforeGroups(groups = "closed")
|
||||
public static void openAndCloseStream() throws IOException {
|
||||
closedReader = Reader.nullReader();
|
||||
closedReader.close();
|
||||
}
|
||||
|
||||
@AfterGroups(groups = "open")
|
||||
public static void closeStream() throws IOException {
|
||||
openReader.close();
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testOpen() {
|
||||
assertNotNull(openReader, "Reader.nullReader() returned null");
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testRead() throws IOException {
|
||||
assertEquals(-1, openReader.read(), "read() != -1");
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testReadBII() throws IOException {
|
||||
assertEquals(-1, openReader.read(new char[1], 0, 1),
|
||||
"read(char[],int,int) != -1");
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testReadBIILenZero() throws IOException {
|
||||
assertEquals(0, openReader.read(new char[1], 0, 0),
|
||||
"read(char[],int,int) != 0");
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testReadCharBuffer() throws IOException {
|
||||
CharBuffer charBuffer = CharBuffer.allocate(1);
|
||||
assertEquals(-1, openReader.read(charBuffer),
|
||||
"read(CharBuffer) != -1");
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testReadCharBufferZeroRemaining() throws IOException {
|
||||
CharBuffer charBuffer = CharBuffer.allocate(0);
|
||||
assertEquals(0, openReader.read(charBuffer),
|
||||
"read(CharBuffer) != 0");
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testSkip() throws IOException {
|
||||
assertEquals(0, openReader.skip(1), "skip() != 0");
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testTransferTo() throws IOException {
|
||||
assertEquals(0, openReader.transferTo(new StringWriter(7)),
|
||||
"transferTo() != 0");
|
||||
}
|
||||
|
||||
@Test(groups = "closed", expectedExceptions = IOException.class)
|
||||
public static void testReadClosed() throws IOException {
|
||||
closedReader.read();
|
||||
}
|
||||
|
||||
@Test(groups = "closed", expectedExceptions = IOException.class)
|
||||
public static void testReadBIIClosed() throws IOException {
|
||||
closedReader.read(new char[1], 0, 1);
|
||||
}
|
||||
|
||||
@Test(groups = "closed", expectedExceptions = IOException.class)
|
||||
public static void testReadCharBufferClosed() throws IOException {
|
||||
CharBuffer charBuffer = CharBuffer.allocate(0);
|
||||
closedReader.read(charBuffer);
|
||||
}
|
||||
|
||||
@Test(groups = "closed", expectedExceptions = IOException.class)
|
||||
public static void testReadCharBufferZeroRemainingClosed() throws IOException {
|
||||
CharBuffer charBuffer = CharBuffer.allocate(0);
|
||||
closedReader.read(charBuffer);
|
||||
}
|
||||
|
||||
@Test(groups = "closed", expectedExceptions = IOException.class)
|
||||
public static void testSkipClosed() throws IOException {
|
||||
closedReader.skip(1);
|
||||
}
|
||||
|
||||
@Test(groups = "closed", expectedExceptions = IOException.class)
|
||||
public static void testTransferToClosed() throws IOException {
|
||||
closedReader.transferTo(new StringWriter(7));
|
||||
}
|
||||
}
|
167
test/jdk/java/io/Writer/NullWriter.java
Normal file
167
test/jdk/java/io/Writer/NullWriter.java
Normal file
@ -0,0 +1,167 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
import org.testng.annotations.AfterGroups;
|
||||
import org.testng.annotations.BeforeGroups;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8196298
|
||||
* @run testng NullWriter
|
||||
* @summary Check for expected behavior of Writer.nullWriter().
|
||||
*/
|
||||
public class NullWriter {
|
||||
private static Writer openWriter;
|
||||
private static Writer closedWriter;
|
||||
|
||||
@BeforeGroups(groups = "open")
|
||||
public static void openStream() {
|
||||
openWriter = Writer.nullWriter();
|
||||
}
|
||||
|
||||
@BeforeGroups(groups = "closed")
|
||||
public static void openAndCloseStream() throws IOException {
|
||||
closedWriter = Writer.nullWriter();
|
||||
closedWriter.close();
|
||||
}
|
||||
|
||||
@AfterGroups(groups = "open")
|
||||
public static void closeStream() throws IOException {
|
||||
openWriter.close();
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testOpen() {
|
||||
assertNotNull(openWriter, "Writer.nullWriter() returned null");
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testAppendChar() throws IOException {
|
||||
assertSame(openWriter, openWriter.append('x'));
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testAppendCharSequence() throws IOException {
|
||||
CharSequence cs = "abc";
|
||||
assertSame(openWriter, openWriter.append(cs));
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testAppendCharSequenceNull() throws IOException {
|
||||
assertSame(openWriter, openWriter.append(null));
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testAppendCharSequenceII() throws IOException {
|
||||
CharSequence cs = "abc";
|
||||
assertSame(openWriter, openWriter.append(cs, 0, 1));
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testAppendCharSequenceIINull() throws IOException {
|
||||
assertSame(openWriter, openWriter.append(null, 2, 1));
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testFlush() throws IOException {
|
||||
openWriter.flush();
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testWrite() throws IOException {
|
||||
openWriter.write(62832);
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testWriteString() throws IOException {
|
||||
openWriter.write("");
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testWriteStringII() throws IOException {
|
||||
openWriter.write("", 0, 0);
|
||||
}
|
||||
|
||||
@Test(groups = "open")
|
||||
public static void testWriteBII() throws IOException, Exception {
|
||||
openWriter.write(new char[]{(char) 6}, 0, 1);
|
||||
}
|
||||
|
||||
@Test(groups = "closed", expectedExceptions = IOException.class)
|
||||
public static void testAppendCharClosed() throws IOException {
|
||||
closedWriter.append('x');
|
||||
}
|
||||
|
||||
@Test(groups = "closed", expectedExceptions = IOException.class)
|
||||
public static void testAppendCharSequenceClosed() throws IOException {
|
||||
CharSequence cs = "abc";
|
||||
closedWriter.append(cs);
|
||||
}
|
||||
|
||||
@Test(groups = "closed", expectedExceptions = IOException.class)
|
||||
public static void testAppendCharSequenceNullClosed() throws IOException {
|
||||
closedWriter.append(null);
|
||||
}
|
||||
|
||||
@Test(groups = "closed", expectedExceptions = IOException.class)
|
||||
public static void testAppendCharSequenceIIClosed() throws IOException {
|
||||
CharSequence cs = "abc";
|
||||
closedWriter.append(cs, 0, 1);
|
||||
}
|
||||
|
||||
@Test(groups = "closed", expectedExceptions = IOException.class)
|
||||
public static void testAppendCharSequenceIINullClosed() throws IOException {
|
||||
closedWriter.append(null, 2, 1);
|
||||
}
|
||||
|
||||
@Test(groups = "closed", expectedExceptions = IOException.class)
|
||||
public static void testFlushClosed() throws IOException {
|
||||
closedWriter.flush();
|
||||
}
|
||||
|
||||
@Test(groups = "closed", expectedExceptions = IOException.class)
|
||||
public static void testWriteClosed() throws IOException {
|
||||
closedWriter.write(62832);
|
||||
}
|
||||
|
||||
@Test(groups = "closed", expectedExceptions = IOException.class)
|
||||
public static void testWriteStringClosed() throws IOException {
|
||||
closedWriter.write("");
|
||||
}
|
||||
|
||||
@Test(groups = "closed", expectedExceptions = IOException.class)
|
||||
public static void testWriteStringIIClosed() throws IOException {
|
||||
closedWriter.write("", 0, 0);
|
||||
}
|
||||
|
||||
@Test(groups = "closed", expectedExceptions = IOException.class)
|
||||
public static void testWriteBIIClosed() throws IOException {
|
||||
closedWriter.write(new char[]{(char) 6}, 0, 1);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user