test/jdk/java/nio/channels/Channels/Basic.java
changeset 47494 24e43fd1ad69
parent 47216 71c04702a3d5
child 48252 77b88d8f8380
equal deleted inserted replaced
47493:843c071258a6 47494:24e43fd1ad69
     1 /*
     1 /*
     2  * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2001, 2017, 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.
   165 
   165 
   166             testNewChannelOut(blah);
   166             testNewChannelOut(blah);
   167             readAndCheck(blah);
   167             readAndCheck(blah);
   168             blah.delete();
   168             blah.delete();
   169 
   169 
       
   170             testNewChannelWriteAfterClose(blah);
       
   171 
       
   172             testNewChannelReadAfterClose(blah);
       
   173             blah.delete();
       
   174 
   170             writeOut(blah, ITERATIONS);
   175             writeOut(blah, ITERATIONS);
   171             testNewChannelIn(blah);
   176             testNewChannelIn(blah);
   172             test4481572(blah);
   177             test4481572(blah);
   173             blah.delete();
   178             blah.delete();
   174 
   179 
   253     }
   258     }
   254 
   259 
   255     private static void testNewChannelOut(File blah) throws Exception {
   260     private static void testNewChannelOut(File blah) throws Exception {
   256         ExtendedFileOutputStream fos = new ExtendedFileOutputStream(blah);
   261         ExtendedFileOutputStream fos = new ExtendedFileOutputStream(blah);
   257         WritableByteChannel wbc = Channels.newChannel(fos);
   262         WritableByteChannel wbc = Channels.newChannel(fos);
       
   263 
   258         for (int i=0; i<ITERATIONS; i++)
   264         for (int i=0; i<ITERATIONS; i++)
   259             wbc.write(ByteBuffer.wrap(message.getBytes(encoding)));
   265             wbc.write(ByteBuffer.wrap(message.getBytes(encoding)));
   260         wbc.close();
   266         wbc.close();
   261         fos.close();
   267         fos.close();
   262     }
   268     }
   283             if (!segment.equals(message))
   289             if (!segment.equals(message))
   284                 throw new RuntimeException("Test failed");
   290                 throw new RuntimeException("Test failed");
   285         }
   291         }
   286         rbc.close();
   292         rbc.close();
   287         fis.close();
   293         fis.close();
       
   294     }
       
   295 
       
   296     private static void testNewChannelWriteAfterClose(File blah)
       
   297         throws Exception {
       
   298         try (ExtendedFileOutputStream fos =
       
   299             new ExtendedFileOutputStream(blah)) {
       
   300             WritableByteChannel wbc = Channels.newChannel(fos);
       
   301 
       
   302             wbc.close();
       
   303             try {
       
   304                 wbc.write(ByteBuffer.allocate(0));
       
   305                 throw new RuntimeException
       
   306                     ("No ClosedChannelException on WritableByteChannel::write");
       
   307             } catch (ClosedChannelException expected) {
       
   308             }
       
   309         }
       
   310     }
       
   311 
       
   312     private static void testNewChannelReadAfterClose(File blah)
       
   313         throws Exception {
       
   314         try (ExtendedFileInputStream fis = new ExtendedFileInputStream(blah)) {
       
   315             ReadableByteChannel rbc = Channels.newChannel(fis);
       
   316 
       
   317             rbc.close();
       
   318             try {
       
   319                 rbc.read(ByteBuffer.allocate(0));
       
   320                 throw new RuntimeException
       
   321                     ("No ClosedChannelException on ReadableByteChannel::read");
       
   322             } catch (ClosedChannelException expected) {
       
   323             }
       
   324         }
   288     }
   325     }
   289 
   326 
   290     // Causes BufferOverflowException if bug 4481572 is present.
   327     // Causes BufferOverflowException if bug 4481572 is present.
   291     private static void test4481572(File blah) throws Exception {
   328     private static void test4481572(File blah) throws Exception {
   292         ExtendedFileInputStream fis = new ExtendedFileInputStream(blah);
   329         ExtendedFileInputStream fis = new ExtendedFileInputStream(blah);