Basic.java revision 0
0N/A/*
0N/A * Copyright 2001-2005 Sun Microsystems, Inc. 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 *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4417152 4481572 6248930
0N/A * @summary Test Channels basic functionality
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.nio.*;
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
0N/A private static void test() throws Exception {
0N/A try {
0N/A ReadableByteChannel channel = Channels.newChannel((InputStream)null);
0N/A
0N/A throw new RuntimeException("Did not get the expected NullPointerException.");
0N/A } catch (NullPointerException ne) {
0N/A // OK. As expected.
0N/A }
0N/A
0N/A try {
0N/A WritableByteChannel channel = Channels.newChannel((OutputStream)null);
0N/A
0N/A throw new RuntimeException("Did not get the expected NullPointerException.");
0N/A } catch (NullPointerException ne) {
0N/A // OK. As expected.
0N/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();
0N/A ReadableByteChannel rbc = (ReadableByteChannel)fc;
0N/A InputStream is = Channels.newInputStream(rbc);
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;
0N/A bytesRead = is.read(bb, totalRead, messageSize - totalRead);
0N/A }
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}