Path: blob/master/test/jdk/java/nio/channels/FileChannel/Read.java
41154 views
/*1* Copyright (c) 2000, 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* @summary Test read method of FileChannel25*/2627import java.io.*;28import java.nio.ByteBuffer;29import java.nio.CharBuffer;30import java.nio.channels.*;31import java.nio.channels.FileChannel;32import java.util.Random;333435/**36* Testing FileChannel's mapping capabilities.37*/3839public class Read {4041private static PrintStream err = System.err;4243private static Random generator = new Random();4445private static int CHARS_PER_LINE = File.separatorChar == '/' ? 5 : 6;4647private static File blah;4849public static void main(String[] args) throws Exception {50StringBuffer sb = new StringBuffer();51sb.setLength(4);5253blah = File.createTempFile("blah", null);54blah.deleteOnExit();55initTestFile(blah);5657FileInputStream fis = new FileInputStream(blah);58FileChannel c = fis.getChannel();5960for (int x=0; x<1000; x++) {61long offset = x * CHARS_PER_LINE;62long expectedResult = offset / CHARS_PER_LINE;63offset = expectedResult * CHARS_PER_LINE;64ByteBuffer bleck = ByteBuffer.allocateDirect(CHARS_PER_LINE);6566c.read(bleck);6768for (int i=0; i<4; i++) {69byte aByte = bleck.get(i);70sb.setCharAt(i, (char)aByte);71}72int result = Integer.parseInt(sb.toString());73if (result != expectedResult) {74err.println("I expected "+expectedResult);75err.println("I got "+ result);76throw new Exception("Read test failed");77}78}7980c.close();81fis.close();82blah.delete();83}8485/**86* Creates file blah:87* 000088* 000189* 000290* 000391* .92* .93* .94* 399995*96* Blah extends beyond a single page of memory so that the97* ability to index into a file of multiple pages is tested.98*/99private static void initTestFile(File blah) throws Exception {100FileOutputStream fos = new FileOutputStream(blah);101BufferedWriter awriter102= new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));103104for(int i=0; i<4000; i++) {105String number = new Integer(i).toString();106for (int h=0; h<4-number.length(); h++)107awriter.write("0");108awriter.write(""+i);109awriter.newLine();110}111awriter.flush();112awriter.close();113}114}115116117