Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/Channels/Basic.java
38821 views
/*1* Copyright (c) 2001, 2008, 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 688480025* @summary Test Channels basic functionality26*/2728import java.io.*;29import java.nio.*;30import java.nio.charset.*;31import java.nio.channels.*;323334public class Basic {3536static String message;3738static String encoding;3940static File blah;4142static int ITERATIONS = 500;4344public static void main(String[] args) throws Exception {45message = "ascii data for a test";46encoding = "ISO-8859-1";47test();48message = "\ucafe\ubabe\ucafe\ubabe\ucafe\ubabe";49encoding = "UTF-8";50test();51}5253static void failNpeExpected() {54throw new RuntimeException("Did not get the expected NullPointerException.");55}5657private static void test() throws Exception {58//Test if methods of Channels throw NPE with null argument(s)59try {60Channels.newInputStream((ReadableByteChannel)null);61failNpeExpected();62} catch (NullPointerException npe) {}6364try {65Channels.newOutputStream((WritableByteChannel)null);66failNpeExpected();67} catch (NullPointerException npe) {}6869try {70ReadableByteChannel channel = Channels.newChannel((InputStream)null);71failNpeExpected();72} catch (NullPointerException ne) {} // OK. As expected.7374try {75WritableByteChannel channel = Channels.newChannel((OutputStream)null);76failNpeExpected();77} catch (NullPointerException ne) {} // OK. As expected.7879WritableByteChannel wbc = new WritableByteChannel() {80public int write(ByteBuffer src) { return 0; }81public void close() throws IOException { }82public boolean isOpen() { return true; }83};8485ReadableByteChannel rbc = new ReadableByteChannel() {86public int read(ByteBuffer dst) { return 0; }87public void close() {}88public boolean isOpen() { return true; }89};9091try {92Channels.newReader((ReadableByteChannel)null,93Charset.defaultCharset().newDecoder(),94-1);95failNpeExpected();96} catch (NullPointerException npe) {}9798try {99Channels.newReader(rbc, (CharsetDecoder)null, -1);100failNpeExpected();101} catch (NullPointerException npe) {}102103try {104Channels.newReader((ReadableByteChannel)null,105Charset.defaultCharset().name());106failNpeExpected();107} catch (NullPointerException npe) {}108109try {110Channels.newReader(rbc, null);111failNpeExpected();112} catch (NullPointerException npe) {}113114115try {116Channels.newReader(null, null);117failNpeExpected();118} catch (NullPointerException npe) {}119120try {121Channels.newWriter((WritableByteChannel)null,122Charset.defaultCharset().newEncoder(),123-1);124failNpeExpected();125} catch (NullPointerException npe) {}126127try {128Channels.newWriter(null, null, -1);129failNpeExpected();130} catch (NullPointerException npe) {}131132try {133Channels.newWriter(wbc, null, -1);134failNpeExpected();135} catch (NullPointerException npe) {}136137try {138Channels.newWriter((WritableByteChannel)null,139Charset.defaultCharset().name());140failNpeExpected();141} catch (NullPointerException npe) {}142143try {144Channels.newWriter(wbc, null);145failNpeExpected();146} catch (NullPointerException npe) {}147148try {149Channels.newWriter(null, null);150failNpeExpected();151} catch (NullPointerException npe) {}152153154try {155blah = File.createTempFile("blah", null);156157testNewOutputStream(blah);158readAndCheck(blah);159blah.delete();160161writeOut(blah, ITERATIONS);162testNewInputStream(blah);163blah.delete();164165testNewChannelOut(blah);166readAndCheck(blah);167blah.delete();168169writeOut(blah, ITERATIONS);170testNewChannelIn(blah);171test4481572(blah);172blah.delete();173174testNewWriter(blah);175readAndCheck(blah);176blah.delete();177178writeOut(blah, ITERATIONS);179testNewReader(blah);180181} finally {182blah.delete();183}184}185186private static void readAndCheck(File blah) throws Exception {187FileInputStream fis = new FileInputStream(blah);188int messageSize = message.length() * ITERATIONS * 3 + 1;189byte bb[] = new byte[messageSize];190int bytesRead = 0;191int totalRead = 0;192while (bytesRead != -1) {193totalRead += bytesRead;194bytesRead = fis.read(bb, totalRead, messageSize - totalRead);195}196String result = new String(bb, 0, totalRead, encoding);197int len = message.length();198for (int i=0; i<ITERATIONS; i++) {199String segment = result.substring(i++ * len, i * len);200if (!segment.equals(message))201throw new RuntimeException("Test failed");202}203fis.close();204}205206private static void writeOut(File blah, int limit) throws Exception {207FileOutputStream fos = new FileOutputStream(blah);208for (int i=0; i<limit; i++)209fos.write(message.getBytes(encoding));210fos.close();211}212213private static void testNewOutputStream(File blah) throws Exception {214FileOutputStream fos = new FileOutputStream(blah);215FileChannel fc = fos.getChannel();216WritableByteChannel wbc = (WritableByteChannel)fc;217OutputStream os = Channels.newOutputStream(wbc);218for (int i=0; i<ITERATIONS; i++)219os.write(message.getBytes(encoding));220os.close();221fos.close();222}223224private static void testNewInputStream(File blah) throws Exception {225FileInputStream fis = new FileInputStream(blah);226FileChannel fc = fis.getChannel();227InputStream is = Channels.newInputStream(fc);228int messageSize = message.length() * ITERATIONS * 3 + 1;229byte bb[] = new byte[messageSize];230231int bytesRead = 0;232int totalRead = 0;233while (bytesRead != -1) {234totalRead += bytesRead;235long rem = Math.min(fc.size() - totalRead, (long)Integer.MAX_VALUE);236if (is.available() != (int)rem)237throw new RuntimeException("available not useful or not maximally useful");238bytesRead = is.read(bb, totalRead, messageSize - totalRead);239}240if (is.available() != 0)241throw new RuntimeException("available() should return 0 at EOF");242243String result = new String(bb, 0, totalRead, encoding);244int len = message.length();245for (int i=0; i<ITERATIONS; i++) {246String segment = result.substring(i++ * len, i * len);247if (!segment.equals(message))248throw new RuntimeException("Test failed");249}250is.close();251fis.close();252}253254private static void testNewChannelOut(File blah) throws Exception {255ExtendedFileOutputStream fos = new ExtendedFileOutputStream(blah);256WritableByteChannel wbc = Channels.newChannel(fos);257for (int i=0; i<ITERATIONS; i++)258wbc.write(ByteBuffer.wrap(message.getBytes(encoding)));259wbc.close();260fos.close();261}262263private static void testNewChannelIn(File blah) throws Exception {264ExtendedFileInputStream fis = new ExtendedFileInputStream(blah);265ReadableByteChannel rbc = Channels.newChannel(fis);266267int messageSize = message.length() * ITERATIONS * 3;268byte data[] = new byte[messageSize+1];269ByteBuffer bb = ByteBuffer.wrap(data);270271int bytesRead = 0;272int totalRead = 0;273while (bytesRead != -1) {274totalRead += bytesRead;275bytesRead = rbc.read(bb);276}277278String result = new String(data, 0, totalRead, encoding);279int len = message.length();280for (int i=0; i<ITERATIONS; i++) {281String segment = result.substring(i++ * len, i * len);282if (!segment.equals(message))283throw new RuntimeException("Test failed");284}285rbc.close();286fis.close();287}288289// Causes BufferOverflowException if bug 4481572 is present.290private static void test4481572(File blah) throws Exception {291ExtendedFileInputStream fis = new ExtendedFileInputStream(blah);292ReadableByteChannel rbc = Channels.newChannel(fis);293294byte data[] = new byte[9000];295ByteBuffer bb = ByteBuffer.wrap(data);296297int bytesRead = 1;298int totalRead = 0;299while (bytesRead > 0) {300totalRead += bytesRead;301bytesRead = rbc.read(bb);302}303rbc.close();304fis.close();305}306307private static void testNewWriter(File blah) throws Exception {308FileOutputStream fos = new FileOutputStream(blah);309WritableByteChannel wbc = (WritableByteChannel)fos.getChannel();310Writer w = Channels.newWriter(wbc, encoding);311char data[] = new char[40];312message.getChars(0, message.length(), data, 0);313for (int i=0; i<ITERATIONS; i++)314w.write(data, 0, message.length());315w.flush();316w.close();317fos.close();318}319320private static void testNewReader(File blah) throws Exception {321FileInputStream fis = new FileInputStream(blah);322ReadableByteChannel rbc = (ReadableByteChannel)fis.getChannel();323Reader r = Channels.newReader(rbc, encoding);324325int messageSize = message.length() * ITERATIONS;326char data[] = new char[messageSize];327328int totalRead = 0;329int charsRead = 0;330while (totalRead < messageSize) {331totalRead += charsRead;332charsRead = r.read(data, totalRead, messageSize - totalRead);333}334String result = new String(data, 0, totalRead);335int len = message.length();336for (int i=0; i<ITERATIONS; i++) {337String segment = result.substring(i++ * len, i * len);338if (!segment.equals(message))339throw new RuntimeException("Test failed");340}341r.close();342fis.close();343}344}345346class ExtendedFileInputStream extends java.io.FileInputStream {347ExtendedFileInputStream(File file) throws FileNotFoundException {348super(file);349}350}351352class ExtendedFileOutputStream extends java.io.FileOutputStream {353ExtendedFileOutputStream(File file) throws FileNotFoundException {354super(file);355}356}357358359