0N/A/*
3909N/A * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
3585N/A * @bug 4429043 6526860
0N/A * @summary Test position method of FileChannel
0N/A */
0N/A
0N/Aimport java.io.*;
3585N/Aimport java.nio.ByteBuffer;
0N/Aimport java.nio.channels.FileChannel;
3585N/Aimport java.nio.file.*;
3585N/Aimport static java.nio.file.StandardOpenOption.*;
3585N/Aimport java.nio.charset.Charset;
0N/Aimport java.util.Random;
0N/A
0N/A
0N/A/**
0N/A * Testing FileChannel's position method.
0N/A */
0N/A
0N/Apublic class Position {
0N/A
3585N/A private static final Charset ISO8859_1 = Charset.forName("8859_1");
0N/A
3585N/A private static final Random generator = new Random();
0N/A
0N/A public static void main(String[] args) throws Exception {
3585N/A Path blah = Files.createTempFile("blah", null);
3585N/A blah.toFile().deleteOnExit();
0N/A initTestFile(blah);
0N/A
3585N/A for (int i=0; i<10; i++) {
3585N/A try (FileChannel fc = (generator.nextBoolean()) ?
3585N/A FileChannel.open(blah, READ) :
3585N/A new FileInputStream(blah.toFile()).getChannel()) {
3585N/A for (int j=0; j<100; j++) {
3585N/A long newPos = generator.nextInt(1000);
3585N/A fc.position(newPos);
3585N/A if (fc.position() != newPos)
3585N/A throw new RuntimeException("Position failed");
3585N/A }
3585N/A }
0N/A }
0N/A
3585N/A for (int i=0; i<10; i++) {
3585N/A try (FileChannel fc = (generator.nextBoolean()) ?
3585N/A FileChannel.open(blah, APPEND) :
3585N/A new FileOutputStream(blah.toFile(), true).getChannel()) {
3585N/A for (int j=0; j<10; j++) {
3585N/A if (fc.position() != fc.size())
3585N/A throw new RuntimeException("Position expected to be size");
3585N/A byte[] buf = new byte[generator.nextInt(100)];
3585N/A fc.write(ByteBuffer.wrap(buf));
3585N/A }
3585N/A }
3585N/A }
3585N/A
3585N/A Files.delete(blah);
0N/A }
0N/A
0N/A /**
0N/A * Creates file blah:
0N/A * 0000
0N/A * 0001
0N/A * 0002
0N/A * 0003
0N/A * .
0N/A * .
0N/A * .
0N/A * 3999
0N/A *
0N/A */
3585N/A private static void initTestFile(Path blah) throws IOException {
3585N/A try (BufferedWriter awriter = Files.newBufferedWriter(blah, ISO8859_1)) {
3585N/A for(int i=0; i<4000; i++) {
3585N/A String number = new Integer(i).toString();
3585N/A for (int h=0; h<4-number.length(); h++)
3585N/A awriter.write("0");
3585N/A awriter.write(""+i);
3585N/A awriter.newLine();
3585N/A }
0N/A }
0N/A }
0N/A}