Path: blob/master/test/jdk/java/nio/channels/FileChannel/ScatteringRead.java
41154 views
/*1* Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 4452020 4629048 4638365 486985925* @summary Test FileChannel scattering reads26* @run main/othervm ScatteringRead27*/2829import java.nio.channels.*;30import java.nio.*;31import java.io.*;3233public class ScatteringRead {3435private static final int NUM_BUFFERS = 3;3637private static final int BUFFER_CAP = 3;3839private static final int BIG_BUFFER_CAP = Integer.MAX_VALUE / 3 + 10;4041public static void main(String[] args) throws Exception {42test1(); // for bug 445202043test2(); // for bug 462904844System.gc();45}4647private static void test1() throws Exception {48ByteBuffer dstBuffers[] = new ByteBuffer[NUM_BUFFERS];49for (int i=0; i<NUM_BUFFERS; i++)50dstBuffers[i] = ByteBuffer.allocateDirect(BUFFER_CAP);51File blah = File.createTempFile("blah1", null);52blah.deleteOnExit();53createTestFile(blah);5455FileInputStream fis = new FileInputStream(blah);56FileChannel fc = fis.getChannel();5758byte expectedResult = -128;59for (int k=0; k<20; k++) {60long bytesRead = fc.read(dstBuffers);61for (int i=0; i<NUM_BUFFERS; i++) {62for (int j=0; j<BUFFER_CAP; j++) {63byte b = dstBuffers[i].get(j);64if (b != expectedResult++)65throw new RuntimeException("Test failed");66}67dstBuffers[i].flip();68}69}70fis.close();71}7273private static void createTestFile(File blah) throws Exception {74FileOutputStream fos = new FileOutputStream(blah);75for(int i=-128; i<128; i++)76fos.write((byte)i);77fos.flush();78fos.close();79}8081private static void test2() throws Exception {82ByteBuffer dstBuffers[] = new ByteBuffer[2];83for (int i=0; i<2; i++)84dstBuffers[i] = ByteBuffer.allocateDirect(10);85File blah = File.createTempFile("blah2", null);86blah.deleteOnExit();87FileOutputStream fos = new FileOutputStream(blah);88for(int i=0; i<15; i++)89fos.write((byte)92);90fos.flush();91fos.close();9293FileInputStream fis = new FileInputStream(blah);94FileChannel fc = fis.getChannel();9596long bytesRead = fc.read(dstBuffers);97if (dstBuffers[1].limit() != 10)98throw new Exception("Scattering read changed buf limit.");99fis.close();100}101}102103104