1 /*
   2  * Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25    @bug 4452020 4629048 4638365 4869859
  26  * @summary Test FileChannel scattering reads
  27  * @run main/othervm ScatteringRead
  28  */
  29 
  30 import java.nio.channels.*;
  31 import java.nio.*;
  32 import java.io.*;
  33 
  34 public class ScatteringRead {
  35 
  36     private static final int NUM_BUFFERS = 3;
  37 
  38     private static final int BUFFER_CAP = 3;
  39 
  40     private static final int BIG_BUFFER_CAP = Integer.MAX_VALUE / 3 + 10;
  41 
  42     public static void main(String[] args) throws Exception {
  43         test1(); // for bug 4452020
  44         test2(); // for bug 4629048
  45         System.gc();
  46     }
  47 
  48     private static void test1() throws Exception {
  49         ByteBuffer dstBuffers[] = new ByteBuffer[NUM_BUFFERS];
  50         for (int i=0; i<NUM_BUFFERS; i++)
  51             dstBuffers[i] = ByteBuffer.allocateDirect(BUFFER_CAP);
  52         File blah = File.createTempFile("blah1", null);
  53         blah.deleteOnExit();
  54         createTestFile(blah);
  55 
  56         FileInputStream fis = new FileInputStream(blah);
  57         FileChannel fc = fis.getChannel();
  58 
  59         byte expectedResult = -128;
  60         for (int k=0; k<20; k++) {
  61             long bytesRead = fc.read(dstBuffers);
  62             for (int i=0; i<NUM_BUFFERS; i++) {
  63                 for (int j=0; j<BUFFER_CAP; j++) {
  64                     byte b = dstBuffers[i].get(j);
  65                     if (b != expectedResult++)
  66                         throw new RuntimeException("Test failed");
  67                 }
  68                 dstBuffers[i].flip();
  69             }
  70         }
  71         fis.close();
  72     }
  73 
  74     private static void createTestFile(File blah) throws Exception {
  75         FileOutputStream fos = new FileOutputStream(blah);
  76         for(int i=-128; i<128; i++)
  77             fos.write((byte)i);
  78         fos.flush();
  79         fos.close();
  80     }
  81 
  82     private static void test2() throws Exception {
  83         ByteBuffer dstBuffers[] = new ByteBuffer[2];
  84         for (int i=0; i<2; i++)
  85             dstBuffers[i] = ByteBuffer.allocateDirect(10);
  86         File blah = File.createTempFile("blah2", null);
  87         blah.deleteOnExit();
  88         FileOutputStream fos = new FileOutputStream(blah);
  89         for(int i=0; i<15; i++)
  90             fos.write((byte)92);
  91         fos.flush();
  92         fos.close();
  93 
  94         FileInputStream fis = new FileInputStream(blah);
  95         FileChannel fc = fis.getChannel();
  96 
  97         long bytesRead = fc.read(dstBuffers);
  98         if (dstBuffers[1].limit() != 10)
  99             throw new Exception("Scattering read changed buf limit.");
 100         fis.close();
 101     }
 102 }