test/jdk/java/nio/channels/Channels/Basic.java
changeset 54569 7689e1cc56fe
parent 48252 77b88d8f8380
equal deleted inserted replaced
54568:b2ed96c35687 54569:7689e1cc56fe
     1 /*
     1 /*
     2  * Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /* @test
    24 /* @test
    25  * @bug 4417152 4481572 6248930 6725399 6884800
    25  * @bug 4417152 4481572 6248930 6725399 6884800 8220477
    26  * @summary Test Channels basic functionality
    26  * @summary Test Channels basic functionality
    27  */
    27  */
    28 
    28 
    29 import java.io.*;
    29 import java.io.*;
    30 import java.nio.*;
    30 import java.nio.*;
    31 import java.nio.charset.*;
    31 import java.nio.charset.*;
    32 import java.nio.channels.*;
    32 import java.nio.channels.*;
    33 
       
    34 
    33 
    35 public class Basic {
    34 public class Basic {
    36 
    35 
    37     static String message;
    36     static String message;
    38 
    37 
   202             blah.delete();
   201             blah.delete();
   203 
   202 
   204             writeOut(blah, ITERATIONS);
   203             writeOut(blah, ITERATIONS);
   205             testNewReader(blah);
   204             testNewReader(blah);
   206 
   205 
       
   206             testNewWriterClose();
       
   207             testNewReaderClose();
   207         } finally {
   208         } finally {
   208             blah.delete();
   209             blah.delete();
   209         }
   210         }
   210     }
   211     }
   211 
   212 
   397                 throw new RuntimeException("Test failed");
   398                 throw new RuntimeException("Test failed");
   398         }
   399         }
   399         r.close();
   400         r.close();
   400         fis.close();
   401         fis.close();
   401     }
   402     }
       
   403 
       
   404     private static void testNewWriterClose() throws Exception {
       
   405         Writer writer = null;
       
   406         try {
       
   407             WritableByteChannel channel = new WritableByteChannel() {
       
   408                 @Override
       
   409                 public int write(ByteBuffer src) throws IOException {
       
   410                     return 0;
       
   411                 }
       
   412 
       
   413                 @Override
       
   414                 public boolean isOpen() {
       
   415                     return true;
       
   416                 }
       
   417 
       
   418                 @Override
       
   419                 public void close() throws IOException {
       
   420                     throw new IOException();
       
   421                 }
       
   422             };
       
   423             writer = Channels.newWriter(channel,
       
   424                 StandardCharsets.UTF_8.newEncoder(), -1);
       
   425             writer.close();
       
   426         } catch (IOException ioe) {
       
   427             Exception theException = null;
       
   428             try {
       
   429                 writer.write(1);
       
   430                 writer.flush();
       
   431             } catch (Exception e) {
       
   432                 theException = e;
       
   433             } finally {
       
   434                 if (theException == null) {
       
   435                     throw new RuntimeException("IOException not thrown");
       
   436                 } else if (!(theException instanceof IOException)) {
       
   437                     throw new RuntimeException("Exception not an IOException: "
       
   438                         + theException);
       
   439                 } else {
       
   440                     String message = theException.getMessage();
       
   441                     if (!message.equals("Stream closed")) {
       
   442                         throw new RuntimeException("Unexpected message "
       
   443                             + message);
       
   444                     }
       
   445                 }
       
   446             }
       
   447         }
       
   448     }
       
   449 
       
   450     private static void testNewReaderClose() throws Exception {
       
   451         Reader reader = null;
       
   452         try {
       
   453             ReadableByteChannel channel = new ReadableByteChannel() {
       
   454                 @Override
       
   455                 public int read(ByteBuffer dst) throws IOException {
       
   456                     dst.put((byte)7);
       
   457                     return 1;
       
   458                 }
       
   459 
       
   460                 @Override
       
   461                 public boolean isOpen() {
       
   462                     return true;
       
   463                 }
       
   464 
       
   465                 @Override
       
   466                 public void close() throws IOException {
       
   467                     throw new IOException();
       
   468                 }
       
   469             };
       
   470             reader = Channels.newReader(channel,
       
   471                 StandardCharsets.UTF_8.newDecoder(), -1);
       
   472             reader.close();
       
   473         } catch (IOException ioe) {
       
   474             Exception theException = null;
       
   475             try {
       
   476                 reader.read();
       
   477             } catch (Exception e) {
       
   478                 theException = e;
       
   479             } finally {
       
   480                 if (theException == null) {
       
   481                     throw new RuntimeException("IOException not thrown");
       
   482                 } else if (!(theException instanceof IOException)) {
       
   483                     throw new RuntimeException("Exception not an IOException: "
       
   484                         + theException);
       
   485                 } else {
       
   486                     String message = theException.getMessage();
       
   487                     if (!message.equals("Stream closed")) {
       
   488                         throw new RuntimeException("Unexpected message "
       
   489                             + message);
       
   490                     }
       
   491                 }
       
   492             }
       
   493         }
       
   494     }
   402 }
   495 }
   403 
   496 
   404 class ExtendedFileInputStream extends java.io.FileInputStream {
   497 class ExtendedFileInputStream extends java.io.FileInputStream {
   405     ExtendedFileInputStream(File file) throws FileNotFoundException {
   498     ExtendedFileInputStream(File file) throws FileNotFoundException {
   406         super(file);
   499         super(file);