Path: blob/master/test/jdk/java/nio/channels/FileChannel/Position.java
41154 views
/*1* Copyright (c) 2000, 2011, 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 4429043 652686025* @summary Test position method of FileChannel26* @key randomness27*/2829import java.io.*;30import java.nio.ByteBuffer;31import java.nio.channels.FileChannel;32import java.nio.file.*;33import static java.nio.file.StandardOpenOption.*;34import java.nio.charset.Charset;35import java.util.Random;363738/**39* Testing FileChannel's position method.40*/4142public class Position {4344private static final Charset ISO8859_1 = Charset.forName("8859_1");4546private static final Random generator = new Random();4748public static void main(String[] args) throws Exception {49Path blah = Files.createTempFile("blah", null);50blah.toFile().deleteOnExit();51initTestFile(blah);5253for (int i=0; i<10; i++) {54try (FileChannel fc = (generator.nextBoolean()) ?55FileChannel.open(blah, READ) :56new FileInputStream(blah.toFile()).getChannel()) {57for (int j=0; j<100; j++) {58long newPos = generator.nextInt(1000);59fc.position(newPos);60if (fc.position() != newPos)61throw new RuntimeException("Position failed");62}63}64}6566for (int i=0; i<10; i++) {67try (FileChannel fc = (generator.nextBoolean()) ?68FileChannel.open(blah, APPEND) :69new FileOutputStream(blah.toFile(), true).getChannel()) {70for (int j=0; j<10; j++) {71if (fc.position() != fc.size())72throw new RuntimeException("Position expected to be size");73byte[] buf = new byte[generator.nextInt(100)];74fc.write(ByteBuffer.wrap(buf));75}76}77}7879Files.delete(blah);80}8182/**83* Creates file blah:84* 000085* 000186* 000287* 000388* .89* .90* .91* 399992*93*/94private static void initTestFile(Path blah) throws IOException {95try (BufferedWriter awriter = Files.newBufferedWriter(blah, ISO8859_1)) {96for(int i=0; i<4000; i++) {97String number = new Integer(i).toString();98for (int h=0; h<4-number.length(); h++)99awriter.write("0");100awriter.write(""+i);101awriter.newLine();102}103}104}105}106107108