Path: blob/master/test/jdk/java/nio/channels/FileChannel/ReadToLimit.java
41154 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 452635025@summary Verify IOUtil.java reads to buffer limits26*/27import java.io.*;28import java.nio.*;29import java.nio.channels.*;3031public class ReadToLimit {32public static void main(String[] args) throws Exception {33File blah = File.createTempFile("blah", null);34blah.deleteOnExit();35initTestFile(blah);3637ByteBuffer[] dstBuffers = new ByteBuffer[2];38for(int i=0; i<2; i++) {39dstBuffers[i] = ByteBuffer.allocateDirect(10);40dstBuffers[i].limit(5);41}42FileInputStream fis = new FileInputStream(blah);43FileChannel fc = fis.getChannel();44long bytesRead = fc.read(dstBuffers);45for(int i=0; i<2; i++)46if (dstBuffers[i].position() != 5)47throw new Exception("Test failed");48fc.close();49fis.close();50blah.delete();51}5253/**54* Creates file blah:55* 000056* 000157* 000258* 000359*/60private static void initTestFile(File blah) throws Exception {61FileOutputStream fos = new FileOutputStream(blah);62BufferedWriter awriter63= new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));6465for(int i=0; i<4; i++) {66String number = new Integer(i).toString();67for (int h=0; h<4-number.length(); h++)68awriter.write("0");69awriter.write(""+i);70awriter.newLine();71}72awriter.flush();73awriter.close();74}75}767778