Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/FileChannel/ScatteringRead.java
38828 views
/*1* Copyright (c) 2001, 2010, 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();4546// Test 3 proves that the system is capable of reading47// more than MAX_INT bytes in one shot. But it is unsuitable48// for automated testing because oftentimes less bytes are49// read for various reasons, and this is allowed by the spec.50// test3(); // for bug 463836551}5253private static void test1() throws Exception {54ByteBuffer dstBuffers[] = new ByteBuffer[NUM_BUFFERS];55for (int i=0; i<NUM_BUFFERS; i++)56dstBuffers[i] = ByteBuffer.allocateDirect(BUFFER_CAP);57File blah = File.createTempFile("blah1", null);58blah.deleteOnExit();59createTestFile(blah);6061FileInputStream fis = new FileInputStream(blah);62FileChannel fc = fis.getChannel();6364byte expectedResult = -128;65for (int k=0; k<20; k++) {66long bytesRead = fc.read(dstBuffers);67for (int i=0; i<NUM_BUFFERS; i++) {68for (int j=0; j<BUFFER_CAP; j++) {69byte b = dstBuffers[i].get(j);70if (b != expectedResult++)71throw new RuntimeException("Test failed");72}73dstBuffers[i].flip();74}75}76fis.close();77}7879private static void createTestFile(File blah) throws Exception {80FileOutputStream fos = new FileOutputStream(blah);81for(int i=-128; i<128; i++)82fos.write((byte)i);83fos.flush();84fos.close();85}8687private static void test2() throws Exception {88ByteBuffer dstBuffers[] = new ByteBuffer[2];89for (int i=0; i<2; i++)90dstBuffers[i] = ByteBuffer.allocateDirect(10);91File blah = File.createTempFile("blah2", null);92blah.deleteOnExit();93FileOutputStream fos = new FileOutputStream(blah);94for(int i=0; i<15; i++)95fos.write((byte)92);96fos.flush();97fos.close();9899FileInputStream fis = new FileInputStream(blah);100FileChannel fc = fis.getChannel();101102long bytesRead = fc.read(dstBuffers);103if (dstBuffers[1].limit() != 10)104throw new Exception("Scattering read changed buf limit.");105fis.close();106}107108private static void test3() throws Exception {109// Only works on 64 bit Solaris110String osName = System.getProperty("os.name");111if (!osName.startsWith("SunOS"))112return;113String dataModel = System.getProperty("sun.arch.data.model");114if (!dataModel.startsWith("64"))115return;116117ByteBuffer dstBuffers[] = new ByteBuffer[NUM_BUFFERS];118File f = File.createTempFile("test3", null);119f.deleteOnExit();120prepTest3File(f, (long)BIG_BUFFER_CAP);121RandomAccessFile raf = new RandomAccessFile(f, "rw");122FileChannel fc = raf.getChannel();123MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0,124BIG_BUFFER_CAP);125for (int i=0; i<NUM_BUFFERS; i++) {126dstBuffers[i] = mbb;127}128fc.close();129raf.close();130131// Source must be large132FileInputStream fis = new FileInputStream("/dev/zero");133fc = fis.getChannel();134135long bytesRead = fc.read(dstBuffers);136if (bytesRead <= Integer.MAX_VALUE)137throw new RuntimeException("Test 3 failed "+bytesRead+" < "+Integer.MAX_VALUE);138139fc.close();140fis.close();141}142143static void prepTest3File(File blah, long testSize) throws Exception {144RandomAccessFile raf = new RandomAccessFile(blah, "rw");145FileChannel fc = raf.getChannel();146fc.write(ByteBuffer.wrap("Use the source!".getBytes()), testSize - 40);147fc.close();148raf.close();149}150151}152153154