Path: blob/master/test/jdk/java/nio/channels/Channels/Basic.java
41152 views
/*1* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4417152 4481572 6248930 6725399 6884800 822047725* @summary Test Channels basic functionality26*/2728import java.io.*;29import java.nio.*;30import java.nio.charset.*;31import java.nio.channels.*;3233public class Basic {3435static String message;3637static String encoding;3839static File blah;4041static int ITERATIONS = 500;4243public static void main(String[] args) throws Exception {44message = "ascii data for a test";45encoding = "ISO-8859-1";46test();47message = "\ucafe\ubabe\ucafe\ubabe\ucafe\ubabe";48encoding = "UTF-8";49test();50}5152static void failNpeExpected() {53throw new RuntimeException("Did not get the expected NullPointerException.");54}5556private static void test() throws Exception {57//Test if methods of Channels throw NPE with null argument(s)58try {59Channels.newInputStream((ReadableByteChannel)null);60failNpeExpected();61} catch (NullPointerException npe) {}6263try {64Channels.newOutputStream((WritableByteChannel)null);65failNpeExpected();66} catch (NullPointerException npe) {}6768try {69ReadableByteChannel channel = Channels.newChannel((InputStream)null);70failNpeExpected();71} catch (NullPointerException ne) {} // OK. As expected.7273try {74WritableByteChannel channel = Channels.newChannel((OutputStream)null);75failNpeExpected();76} catch (NullPointerException ne) {} // OK. As expected.7778WritableByteChannel wbc = new WritableByteChannel() {79public int write(ByteBuffer src) { return 0; }80public void close() throws IOException { }81public boolean isOpen() { return true; }82};8384ReadableByteChannel rbc = new ReadableByteChannel() {85public int read(ByteBuffer dst) { return 0; }86public void close() {}87public boolean isOpen() { return true; }88};8990try {91Channels.newReader((ReadableByteChannel)null,92Charset.defaultCharset().newDecoder(),93-1);94failNpeExpected();95} catch (NullPointerException npe) {}9697try {98Channels.newReader(rbc, (CharsetDecoder)null, -1);99failNpeExpected();100} catch (NullPointerException npe) {}101102try {103Channels.newReader((ReadableByteChannel)null,104Charset.defaultCharset().name());105failNpeExpected();106} catch (NullPointerException npe) {}107108try {109Channels.newReader(rbc, (String)null);110failNpeExpected();111} catch (NullPointerException npe) {}112113114try {115Channels.newReader(null, (String)null);116failNpeExpected();117} catch (NullPointerException npe) {}118119try {120Channels.newReader(rbc, (Charset)null);121failNpeExpected();122} catch (NullPointerException npe) {}123124125try {126Channels.newReader(null, (Charset)null);127failNpeExpected();128} catch (NullPointerException npe) {}129130try {131Channels.newWriter((WritableByteChannel)null,132Charset.defaultCharset().newEncoder(),133-1);134failNpeExpected();135} catch (NullPointerException npe) {}136137try {138Channels.newWriter(null, null, -1);139failNpeExpected();140} catch (NullPointerException npe) {}141142try {143Channels.newWriter(wbc, null, -1);144failNpeExpected();145} catch (NullPointerException npe) {}146147try {148Channels.newWriter((WritableByteChannel)null,149Charset.defaultCharset().name());150failNpeExpected();151} catch (NullPointerException npe) {}152153try {154Channels.newWriter(wbc, (String)null);155failNpeExpected();156} catch (NullPointerException npe) {}157158try {159Channels.newWriter(null, (String)null);160failNpeExpected();161} catch (NullPointerException npe) {}162163try {164Channels.newWriter(wbc, (Charset)null);165failNpeExpected();166} catch (NullPointerException npe) {}167168try {169Channels.newWriter(null, (Charset)null);170failNpeExpected();171} catch (NullPointerException npe) {}172173try {174blah = File.createTempFile("blah", null);175176testNewOutputStream(blah);177readAndCheck(blah);178blah.delete();179180writeOut(blah, ITERATIONS);181testNewInputStream(blah);182blah.delete();183184testNewChannelOut(blah);185readAndCheck(blah);186blah.delete();187188testNewChannelWriteAfterClose(blah);189190testNewChannelReadAfterClose(blah);191blah.delete();192193writeOut(blah, ITERATIONS);194testNewChannelIn(blah);195test4481572(blah);196blah.delete();197198testNewWriter(blah);199readAndCheck(blah);200blah.delete();201202writeOut(blah, ITERATIONS);203testNewReader(blah);204205testNewWriterClose();206testNewReaderClose();207} finally {208blah.delete();209}210}211212private static void readAndCheck(File blah) throws Exception {213FileInputStream fis = new FileInputStream(blah);214int messageSize = message.length() * ITERATIONS * 3 + 1;215byte bb[] = new byte[messageSize];216int bytesRead = 0;217int totalRead = 0;218while (bytesRead != -1) {219totalRead += bytesRead;220bytesRead = fis.read(bb, totalRead, messageSize - totalRead);221}222String result = new String(bb, 0, totalRead, encoding);223int len = message.length();224for (int i=0; i<ITERATIONS; i++) {225String segment = result.substring(i++ * len, i * len);226if (!segment.equals(message))227throw new RuntimeException("Test failed");228}229fis.close();230}231232private static void writeOut(File blah, int limit) throws Exception {233FileOutputStream fos = new FileOutputStream(blah);234for (int i=0; i<limit; i++)235fos.write(message.getBytes(encoding));236fos.close();237}238239private static void testNewOutputStream(File blah) throws Exception {240FileOutputStream fos = new FileOutputStream(blah);241FileChannel fc = fos.getChannel();242WritableByteChannel wbc = (WritableByteChannel)fc;243OutputStream os = Channels.newOutputStream(wbc);244for (int i=0; i<ITERATIONS; i++)245os.write(message.getBytes(encoding));246os.close();247fos.close();248}249250private static void testNewInputStream(File blah) throws Exception {251FileInputStream fis = new FileInputStream(blah);252FileChannel fc = fis.getChannel();253InputStream is = Channels.newInputStream(fc);254int messageSize = message.length() * ITERATIONS * 3 + 1;255byte bb[] = new byte[messageSize];256257int bytesRead = 0;258int totalRead = 0;259while (bytesRead != -1) {260totalRead += bytesRead;261long rem = Math.min(fc.size() - totalRead, (long)Integer.MAX_VALUE);262if (is.available() != (int)rem)263throw new RuntimeException("available not useful or not maximally useful");264bytesRead = is.read(bb, totalRead, messageSize - totalRead);265}266if (is.available() != 0)267throw new RuntimeException("available() should return 0 at EOF");268269String result = new String(bb, 0, totalRead, encoding);270int len = message.length();271for (int i=0; i<ITERATIONS; i++) {272String segment = result.substring(i++ * len, i * len);273if (!segment.equals(message))274throw new RuntimeException("Test failed");275}276is.close();277fis.close();278}279280private static void testNewChannelOut(File blah) throws Exception {281ExtendedFileOutputStream fos = new ExtendedFileOutputStream(blah);282WritableByteChannel wbc = Channels.newChannel(fos);283284for (int i=0; i<ITERATIONS; i++)285wbc.write(ByteBuffer.wrap(message.getBytes(encoding)));286wbc.close();287fos.close();288}289290private static void testNewChannelIn(File blah) throws Exception {291ExtendedFileInputStream fis = new ExtendedFileInputStream(blah);292ReadableByteChannel rbc = Channels.newChannel(fis);293294int messageSize = message.length() * ITERATIONS * 3;295byte data[] = new byte[messageSize+1];296ByteBuffer bb = ByteBuffer.wrap(data);297298int bytesRead = 0;299int totalRead = 0;300while (bytesRead != -1) {301totalRead += bytesRead;302bytesRead = rbc.read(bb);303}304305String result = new String(data, 0, totalRead, encoding);306int len = message.length();307for (int i=0; i<ITERATIONS; i++) {308String segment = result.substring(i++ * len, i * len);309if (!segment.equals(message))310throw new RuntimeException("Test failed");311}312rbc.close();313fis.close();314}315316private static void testNewChannelWriteAfterClose(File blah)317throws Exception {318try (ExtendedFileOutputStream fos =319new ExtendedFileOutputStream(blah)) {320WritableByteChannel wbc = Channels.newChannel(fos);321322wbc.close();323try {324wbc.write(ByteBuffer.allocate(0));325throw new RuntimeException326("No ClosedChannelException on WritableByteChannel::write");327} catch (ClosedChannelException expected) {328}329}330}331332private static void testNewChannelReadAfterClose(File blah)333throws Exception {334try (ExtendedFileInputStream fis = new ExtendedFileInputStream(blah)) {335ReadableByteChannel rbc = Channels.newChannel(fis);336337rbc.close();338try {339rbc.read(ByteBuffer.allocate(0));340throw new RuntimeException341("No ClosedChannelException on ReadableByteChannel::read");342} catch (ClosedChannelException expected) {343}344}345}346347// Causes BufferOverflowException if bug 4481572 is present.348private static void test4481572(File blah) throws Exception {349ExtendedFileInputStream fis = new ExtendedFileInputStream(blah);350ReadableByteChannel rbc = Channels.newChannel(fis);351352byte data[] = new byte[9000];353ByteBuffer bb = ByteBuffer.wrap(data);354355int bytesRead = 1;356int totalRead = 0;357while (bytesRead > 0) {358totalRead += bytesRead;359bytesRead = rbc.read(bb);360}361rbc.close();362fis.close();363}364365private static void testNewWriter(File blah) throws Exception {366FileOutputStream fos = new FileOutputStream(blah);367WritableByteChannel wbc = (WritableByteChannel)fos.getChannel();368Writer w = Channels.newWriter(wbc, encoding);369char data[] = new char[40];370message.getChars(0, message.length(), data, 0);371for (int i=0; i<ITERATIONS; i++)372w.write(data, 0, message.length());373w.flush();374w.close();375fos.close();376}377378private static void testNewReader(File blah) throws Exception {379FileInputStream fis = new FileInputStream(blah);380ReadableByteChannel rbc = (ReadableByteChannel)fis.getChannel();381Reader r = Channels.newReader(rbc, encoding);382383int messageSize = message.length() * ITERATIONS;384char data[] = new char[messageSize];385386int totalRead = 0;387int charsRead = 0;388while (totalRead < messageSize) {389totalRead += charsRead;390charsRead = r.read(data, totalRead, messageSize - totalRead);391}392String result = new String(data, 0, totalRead);393int len = message.length();394for (int i=0; i<ITERATIONS; i++) {395String segment = result.substring(i++ * len, i * len);396if (!segment.equals(message))397throw new RuntimeException("Test failed");398}399r.close();400fis.close();401}402403private static void testNewWriterClose() throws Exception {404Writer writer = null;405try {406WritableByteChannel channel = new WritableByteChannel() {407@Override408public int write(ByteBuffer src) throws IOException {409return 0;410}411412@Override413public boolean isOpen() {414return true;415}416417@Override418public void close() throws IOException {419throw new IOException();420}421};422writer = Channels.newWriter(channel,423StandardCharsets.UTF_8.newEncoder(), -1);424writer.close();425} catch (IOException ioe) {426Exception theException = null;427try {428writer.write(1);429writer.flush();430} catch (Exception e) {431theException = e;432} finally {433if (theException == null) {434throw new RuntimeException("IOException not thrown");435} else if (!(theException instanceof IOException)) {436throw new RuntimeException("Exception not an IOException: "437+ theException);438} else {439String message = theException.getMessage();440if (!message.equals("Stream closed")) {441throw new RuntimeException("Unexpected message "442+ message);443}444}445}446}447}448449private static void testNewReaderClose() throws Exception {450Reader reader = null;451try {452ReadableByteChannel channel = new ReadableByteChannel() {453@Override454public int read(ByteBuffer dst) throws IOException {455dst.put((byte)7);456return 1;457}458459@Override460public boolean isOpen() {461return true;462}463464@Override465public void close() throws IOException {466throw new IOException();467}468};469reader = Channels.newReader(channel,470StandardCharsets.UTF_8.newDecoder(), -1);471reader.close();472} catch (IOException ioe) {473Exception theException = null;474try {475reader.read();476} catch (Exception e) {477theException = e;478} finally {479if (theException == null) {480throw new RuntimeException("IOException not thrown");481} else if (!(theException instanceof IOException)) {482throw new RuntimeException("Exception not an IOException: "483+ theException);484} else {485String message = theException.getMessage();486if (!message.equals("Stream closed")) {487throw new RuntimeException("Unexpected message "488+ message);489}490}491}492}493}494}495496class ExtendedFileInputStream extends java.io.FileInputStream {497ExtendedFileInputStream(File file) throws FileNotFoundException {498super(file);499}500}501502class ExtendedFileOutputStream extends java.io.FileOutputStream {503ExtendedFileOutputStream(File file) throws FileNotFoundException {504super(file);505}506}507508509