0N/A/*
2362N/A * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
1713N/A * @bug 4417152 4481572 6248930 6725399 6884800
0N/A * @summary Test Channels basic functionality
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.nio.*;
724N/Aimport java.nio.charset.*;
0N/Aimport java.nio.channels.*;
0N/A
0N/A
0N/Apublic class Basic {
0N/A
0N/A static String message;
0N/A
0N/A static String encoding;
0N/A
0N/A static File blah;
0N/A
0N/A static int ITERATIONS = 500;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A message = "ascii data for a test";
0N/A encoding = "ISO-8859-1";
0N/A test();
0N/A message = "\ucafe\ubabe\ucafe\ubabe\ucafe\ubabe";
0N/A encoding = "UTF-8";
0N/A test();
0N/A }
0N/A
724N/A static void failNpeExpected() {
724N/A throw new RuntimeException("Did not get the expected NullPointerException.");
724N/A }
724N/A
0N/A private static void test() throws Exception {
724N/A //Test if methods of Channels throw NPE with null argument(s)
724N/A try {
724N/A Channels.newInputStream((ReadableByteChannel)null);
724N/A failNpeExpected();
724N/A } catch (NullPointerException npe) {}
724N/A
724N/A try {
724N/A Channels.newOutputStream((WritableByteChannel)null);
724N/A failNpeExpected();
724N/A } catch (NullPointerException npe) {}
724N/A
0N/A try {
0N/A ReadableByteChannel channel = Channels.newChannel((InputStream)null);
724N/A failNpeExpected();
724N/A } catch (NullPointerException ne) {} // OK. As expected.
0N/A
0N/A try {
0N/A WritableByteChannel channel = Channels.newChannel((OutputStream)null);
724N/A failNpeExpected();
724N/A } catch (NullPointerException ne) {} // OK. As expected.
0N/A
724N/A WritableByteChannel wbc = new WritableByteChannel() {
724N/A public int write(ByteBuffer src) { return 0; }
724N/A public void close() throws IOException { }
724N/A public boolean isOpen() { return true; }
724N/A };
724N/A
724N/A ReadableByteChannel rbc = new ReadableByteChannel() {
724N/A public int read(ByteBuffer dst) { return 0; }
724N/A public void close() {}
724N/A public boolean isOpen() { return true; }
724N/A };
724N/A
724N/A try {
724N/A Channels.newReader((ReadableByteChannel)null,
724N/A Charset.defaultCharset().newDecoder(),
724N/A -1);
724N/A failNpeExpected();
724N/A } catch (NullPointerException npe) {}
724N/A
724N/A try {
724N/A Channels.newReader(rbc, (CharsetDecoder)null, -1);
724N/A failNpeExpected();
724N/A } catch (NullPointerException npe) {}
724N/A
724N/A try {
724N/A Channels.newReader((ReadableByteChannel)null,
724N/A Charset.defaultCharset().name());
724N/A failNpeExpected();
724N/A } catch (NullPointerException npe) {}
724N/A
724N/A try {
724N/A Channels.newReader(rbc, null);
724N/A failNpeExpected();
724N/A } catch (NullPointerException npe) {}
724N/A
724N/A
724N/A try {
724N/A Channels.newReader(null, null);
724N/A failNpeExpected();
724N/A } catch (NullPointerException npe) {}
724N/A
724N/A try {
724N/A Channels.newWriter((WritableByteChannel)null,
724N/A Charset.defaultCharset().newEncoder(),
724N/A -1);
724N/A failNpeExpected();
724N/A } catch (NullPointerException npe) {}
724N/A
724N/A try {
724N/A Channels.newWriter(null, null, -1);
724N/A failNpeExpected();
724N/A } catch (NullPointerException npe) {}
724N/A
724N/A try {
724N/A Channels.newWriter(wbc, null, -1);
724N/A failNpeExpected();
724N/A } catch (NullPointerException npe) {}
724N/A
724N/A try {
724N/A Channels.newWriter((WritableByteChannel)null,
724N/A Charset.defaultCharset().name());
724N/A failNpeExpected();
724N/A } catch (NullPointerException npe) {}
724N/A
724N/A try {
724N/A Channels.newWriter(wbc, null);
724N/A failNpeExpected();
724N/A } catch (NullPointerException npe) {}
724N/A
724N/A try {
724N/A Channels.newWriter(null, null);
724N/A failNpeExpected();
724N/A } catch (NullPointerException npe) {}
724N/A
0N/A
0N/A try {
0N/A blah = File.createTempFile("blah", null);
0N/A
0N/A testNewOutputStream(blah);
0N/A readAndCheck(blah);
0N/A blah.delete();
0N/A
0N/A writeOut(blah, ITERATIONS);
0N/A testNewInputStream(blah);
0N/A blah.delete();
0N/A
0N/A testNewChannelOut(blah);
0N/A readAndCheck(blah);
0N/A blah.delete();
0N/A
0N/A writeOut(blah, ITERATIONS);
0N/A testNewChannelIn(blah);
0N/A test4481572(blah);
0N/A blah.delete();
0N/A
0N/A testNewWriter(blah);
0N/A readAndCheck(blah);
0N/A blah.delete();
0N/A
0N/A writeOut(blah, ITERATIONS);
0N/A testNewReader(blah);
0N/A
0N/A } finally {
0N/A blah.delete();
0N/A }
0N/A }
0N/A
0N/A private static void readAndCheck(File blah) throws Exception {
0N/A FileInputStream fis = new FileInputStream(blah);
0N/A int messageSize = message.length() * ITERATIONS * 3 + 1;
0N/A byte bb[] = new byte[messageSize];
0N/A int bytesRead = 0;
0N/A int totalRead = 0;
0N/A while (bytesRead != -1) {
0N/A totalRead += bytesRead;
0N/A bytesRead = fis.read(bb, totalRead, messageSize - totalRead);
0N/A }
0N/A String result = new String(bb, 0, totalRead, encoding);
0N/A int len = message.length();
0N/A for (int i=0; i<ITERATIONS; i++) {
0N/A String segment = result.substring(i++ * len, i * len);
0N/A if (!segment.equals(message))
0N/A throw new RuntimeException("Test failed");
0N/A }
0N/A fis.close();
0N/A }
0N/A
0N/A private static void writeOut(File blah, int limit) throws Exception {
0N/A FileOutputStream fos = new FileOutputStream(blah);
0N/A for (int i=0; i<limit; i++)
0N/A fos.write(message.getBytes(encoding));
0N/A fos.close();
0N/A }
0N/A
0N/A private static void testNewOutputStream(File blah) throws Exception {
0N/A FileOutputStream fos = new FileOutputStream(blah);
0N/A FileChannel fc = fos.getChannel();
0N/A WritableByteChannel wbc = (WritableByteChannel)fc;
0N/A OutputStream os = Channels.newOutputStream(wbc);
0N/A for (int i=0; i<ITERATIONS; i++)
0N/A os.write(message.getBytes(encoding));
0N/A os.close();
0N/A fos.close();
0N/A }
0N/A
0N/A private static void testNewInputStream(File blah) throws Exception {
0N/A FileInputStream fis = new FileInputStream(blah);
0N/A FileChannel fc = fis.getChannel();
1713N/A InputStream is = Channels.newInputStream(fc);
0N/A int messageSize = message.length() * ITERATIONS * 3 + 1;
0N/A byte bb[] = new byte[messageSize];
0N/A
0N/A int bytesRead = 0;
0N/A int totalRead = 0;
0N/A while (bytesRead != -1) {
0N/A totalRead += bytesRead;
1713N/A long rem = Math.min(fc.size() - totalRead, (long)Integer.MAX_VALUE);
1713N/A if (is.available() != (int)rem)
1713N/A throw new RuntimeException("available not useful or not maximally useful");
0N/A bytesRead = is.read(bb, totalRead, messageSize - totalRead);
0N/A }
1713N/A if (is.available() != 0)
1713N/A throw new RuntimeException("available() should return 0 at EOF");
0N/A
0N/A String result = new String(bb, 0, totalRead, encoding);
0N/A int len = message.length();
0N/A for (int i=0; i<ITERATIONS; i++) {
0N/A String segment = result.substring(i++ * len, i * len);
0N/A if (!segment.equals(message))
0N/A throw new RuntimeException("Test failed");
0N/A }
0N/A is.close();
0N/A fis.close();
0N/A }
0N/A
0N/A private static void testNewChannelOut(File blah) throws Exception {
0N/A ExtendedFileOutputStream fos = new ExtendedFileOutputStream(blah);
0N/A WritableByteChannel wbc = Channels.newChannel(fos);
0N/A for (int i=0; i<ITERATIONS; i++)
0N/A wbc.write(ByteBuffer.wrap(message.getBytes(encoding)));
0N/A wbc.close();
0N/A fos.close();
0N/A }
0N/A
0N/A private static void testNewChannelIn(File blah) throws Exception {
0N/A ExtendedFileInputStream fis = new ExtendedFileInputStream(blah);
0N/A ReadableByteChannel rbc = Channels.newChannel(fis);
0N/A
0N/A int messageSize = message.length() * ITERATIONS * 3;
0N/A byte data[] = new byte[messageSize+1];
0N/A ByteBuffer bb = ByteBuffer.wrap(data);
0N/A
0N/A int bytesRead = 0;
0N/A int totalRead = 0;
0N/A while (bytesRead != -1) {
0N/A totalRead += bytesRead;
0N/A bytesRead = rbc.read(bb);
0N/A }
0N/A
0N/A String result = new String(data, 0, totalRead, encoding);
0N/A int len = message.length();
0N/A for (int i=0; i<ITERATIONS; i++) {
0N/A String segment = result.substring(i++ * len, i * len);
0N/A if (!segment.equals(message))
0N/A throw new RuntimeException("Test failed");
0N/A }
0N/A rbc.close();
0N/A fis.close();
0N/A }
0N/A
0N/A // Causes BufferOverflowException if bug 4481572 is present.
0N/A private static void test4481572(File blah) throws Exception {
0N/A ExtendedFileInputStream fis = new ExtendedFileInputStream(blah);
0N/A ReadableByteChannel rbc = Channels.newChannel(fis);
0N/A
0N/A byte data[] = new byte[9000];
0N/A ByteBuffer bb = ByteBuffer.wrap(data);
0N/A
0N/A int bytesRead = 1;
0N/A int totalRead = 0;
0N/A while (bytesRead > 0) {
0N/A totalRead += bytesRead;
0N/A bytesRead = rbc.read(bb);
0N/A }
0N/A rbc.close();
0N/A fis.close();
0N/A }
0N/A
0N/A private static void testNewWriter(File blah) throws Exception {
0N/A FileOutputStream fos = new FileOutputStream(blah);
0N/A WritableByteChannel wbc = (WritableByteChannel)fos.getChannel();
0N/A Writer w = Channels.newWriter(wbc, encoding);
0N/A char data[] = new char[40];
0N/A message.getChars(0, message.length(), data, 0);
0N/A for (int i=0; i<ITERATIONS; i++)
0N/A w.write(data, 0, message.length());
0N/A w.flush();
0N/A w.close();
0N/A fos.close();
0N/A }
0N/A
0N/A private static void testNewReader(File blah) throws Exception {
0N/A FileInputStream fis = new FileInputStream(blah);
0N/A ReadableByteChannel rbc = (ReadableByteChannel)fis.getChannel();
0N/A Reader r = Channels.newReader(rbc, encoding);
0N/A
0N/A int messageSize = message.length() * ITERATIONS;
0N/A char data[] = new char[messageSize];
0N/A
0N/A int totalRead = 0;
0N/A int charsRead = 0;
0N/A while (totalRead < messageSize) {
0N/A totalRead += charsRead;
0N/A charsRead = r.read(data, totalRead, messageSize - totalRead);
0N/A }
0N/A String result = new String(data, 0, totalRead);
0N/A int len = message.length();
0N/A for (int i=0; i<ITERATIONS; i++) {
0N/A String segment = result.substring(i++ * len, i * len);
0N/A if (!segment.equals(message))
0N/A throw new RuntimeException("Test failed");
0N/A }
0N/A r.close();
0N/A fis.close();
0N/A }
0N/A}
0N/A
0N/Aclass ExtendedFileInputStream extends java.io.FileInputStream {
0N/A ExtendedFileInputStream(File file) throws FileNotFoundException {
0N/A super(file);
0N/A }
0N/A}
0N/A
0N/Aclass ExtendedFileOutputStream extends java.io.FileOutputStream {
0N/A ExtendedFileOutputStream(File file) throws FileNotFoundException {
0N/A super(file);
0N/A }
0N/A}