0N/A/*
3470N/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
3200N/A * @bug 6191269 6709457
0N/A * @summary Test truncate method of FileChannel
0N/A */
0N/A
0N/Aimport java.io.*;
3200N/Aimport java.nio.ByteBuffer;
0N/Aimport java.nio.channels.FileChannel;
3200N/Aimport static java.nio.file.StandardOpenOption.*;
0N/Aimport java.util.Random;
0N/A
0N/A
0N/A/**
0N/A * Testing FileChannel's truncate method.
0N/A */
0N/A
0N/Apublic class Truncate {
3200N/A private static final Random generator = new Random();
0N/A
0N/A public static void main(String[] args) throws Exception {
3200N/A File blah = File.createTempFile("blah", null);
0N/A blah.deleteOnExit();
3200N/A try {
3200N/A basicTest(blah);
3200N/A appendTest(blah);
3200N/A } finally {
3200N/A blah.delete();
3200N/A }
3200N/A }
3200N/A
3200N/A /**
3200N/A * Basic test of asserts in truncate's specification.
3200N/A */
3200N/A static void basicTest(File blah) throws Exception {
0N/A for(int i=0; i<100; i++) {
0N/A long testSize = generator.nextInt(1000) + 10;
0N/A initTestFile(blah, testSize);
3200N/A
3470N/A try (FileChannel fc = (i < 50) ?
3470N/A new RandomAccessFile(blah, "rw").getChannel() :
3470N/A FileChannel.open(blah.toPath(), READ, WRITE))
3470N/A {
3470N/A if (fc.size() != testSize)
3470N/A throw new RuntimeException("Size failed");
3470N/A
3470N/A long position = generator.nextInt((int)testSize);
3470N/A fc.position(position);
3200N/A
3470N/A long newSize = generator.nextInt((int)testSize);
3470N/A fc.truncate(newSize);
0N/A
3470N/A if (fc.size() != newSize)
3470N/A throw new RuntimeException("Truncate failed");
0N/A
3470N/A if (position > newSize) {
3470N/A if (fc.position() != newSize)
3470N/A throw new RuntimeException("Position greater than size");
3470N/A } else {
3470N/A if (fc.position() != position)
3470N/A throw new RuntimeException("Truncate changed position");
3470N/A };
3470N/A }
3200N/A }
3200N/A }
0N/A
3200N/A /**
3200N/A * Test behavior of truncate method when file is opened for append
3200N/A */
3200N/A static void appendTest(File blah) throws Exception {
3200N/A for (int i=0; i<10; i++) {
3200N/A long testSize = generator.nextInt(1000) + 10;
3200N/A initTestFile(blah, testSize);
3470N/A try (FileChannel fc = (i < 5) ?
3470N/A new FileOutputStream(blah, true).getChannel() :
3470N/A FileChannel.open(blah.toPath(), APPEND))
3470N/A {
3470N/A // truncate file
3470N/A long newSize = generator.nextInt((int)testSize);
3470N/A fc.truncate(newSize);
3470N/A if (fc.size() != newSize)
3470N/A throw new RuntimeException("Truncate failed");
0N/A
3470N/A // write one byte
3470N/A ByteBuffer buf = ByteBuffer.allocate(1);
3470N/A buf.put((byte)'x');
3470N/A buf.flip();
3470N/A fc.write(buf);
3470N/A if (fc.size() != (newSize+1))
3470N/A throw new RuntimeException("Unexpected size");
3470N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates file blah of specified size in bytes.
0N/A *
0N/A */
0N/A private static void initTestFile(File blah, long size) throws Exception {
0N/A if (blah.exists())
0N/A blah.delete();
0N/A FileOutputStream fos = new FileOutputStream(blah);
0N/A BufferedWriter awriter
0N/A = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
0N/A
0N/A for(int i=0; i<size; i++) {
0N/A awriter.write("e");
0N/A }
0N/A awriter.flush();
0N/A awriter.close();
0N/A }
0N/A}