Path: blob/master/test/jdk/java/nio/channels/FileChannel/Pread.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* @bug 4862382 486240825* @summary Test positional read method of FileChannel26* @key randomness27*/2829import java.io.*;30import java.nio.ByteBuffer;31import java.nio.CharBuffer;32import java.nio.channels.*;33import java.nio.channels.FileChannel;34import java.util.Random;353637/**38* Testing FileChannel's positional read method.39*/4041public class Pread {4243private static PrintStream err = System.err;4445private static Random generator = new Random();4647private static int CHARS_PER_LINE = File.separatorChar == '/' ? 5 : 6;4849public static void main(String[] args) throws Exception {50genericTest();51testNegativePosition(); // This test for bug 486238252testUnreadableChannel();// This test for bug 486240853}5455// This test for bug 486238256private static void testNegativePosition() throws Exception {57File blah = File.createTempFile("blah1", null);58blah.deleteOnExit();59FileOutputStream fos = new FileOutputStream(blah);60fos.write(new byte[128]);61fos.close();62FileChannel fc = (new FileInputStream(blah)).getChannel();63try {64fc.read(ByteBuffer.allocate(256), -1L);65throw new RuntimeException("Expected exception not thrown");66} catch(IllegalArgumentException e) {67// Correct result68} finally {69fc.close();70blah.delete();71}72}7374// This test for bug 486240875private static void testUnreadableChannel() throws Exception {76File blah = File.createTempFile("blah2", null);77blah.deleteOnExit();78FileOutputStream fos = new FileOutputStream(blah);79try {80fos.write(new byte[128]);81FileChannel fc = fos.getChannel();82try {83fc.read(ByteBuffer.allocate(256),1);84throw new RuntimeException("Expected exception not thrown");85} catch(NonReadableChannelException e) {86// Correct result87}88} finally {89fos.close();90blah.delete();91}92}9394private static void genericTest() throws Exception {95StringBuffer sb = new StringBuffer();96sb.setLength(4);9798File blah = File.createTempFile("blah3", null);99blah.deleteOnExit();100initTestFile(blah);101102FileInputStream fis = new FileInputStream(blah);103FileChannel c = fis.getChannel();104105for (int x=0; x<100; x++) {106long offset = generator.nextInt(1000);107long expectedResult = offset / CHARS_PER_LINE;108offset = expectedResult * CHARS_PER_LINE;109ByteBuffer bleck = ByteBuffer.allocateDirect(4);110111long originalPosition = c.position();112113int totalRead = 0;114while (totalRead < 4) {115int read = c.read(bleck, offset);116if (read < 0)117throw new Exception("Read failed");118totalRead += read;119}120121long newPosition = c.position();122123for (int i=0; i<4; i++) {124byte aByte = bleck.get(i);125sb.setCharAt(i, (char)aByte);126}127int result = Integer.parseInt(sb.toString());128if (result != expectedResult) {129err.println("I expected "+ expectedResult);130err.println("I got "+ result);131throw new Exception("Read test failed");132}133134// Ensure that file pointer position has not changed135if (originalPosition != newPosition)136throw new Exception("File position modified");137}138139c.close();140fis.close();141blah.delete();142}143144/**145* Creates file blah:146* 0000147* 0001148* 0002149* 0003150* .151* .152* .153* 3999154*155* Blah extends beyond a single page of memory so that the156* ability to index into a file of multiple pages is tested.157*/158private static void initTestFile(File blah) throws Exception {159FileOutputStream fos = new FileOutputStream(blah);160BufferedWriter awriter161= new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));162163for(int i=0; i<4000; i++) {164String number = new Integer(i).toString();165for (int h=0; h<4-number.length(); h++)166awriter.write("0");167awriter.write(""+i);168awriter.newLine();169}170awriter.flush();171awriter.close();172}173}174175176