Path: blob/master/test/jdk/java/nio/channels/FileChannel/TruncateRAF.java
41154 views
/*1* Copyright (c) 2018, 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 820431025* @summary Check how FileChannel behaves if the file size/offset change via26* RAF.setLength() and other methods.27* @run main TruncateRAF28*/2930import java.io.File;31import java.io.RandomAccessFile;32import java.io.IOException;33import java.nio.ByteBuffer;34import java.nio.channels.FileChannel;3536public class TruncateRAF {3738static void checkState(RandomAccessFile raf, FileChannel fch,39long expectedOffset, long expectedLength)40throws IOException41{42long rafLength = raf.length();43long rafOffset = raf.getFilePointer();44long fchLength = fch.size();45long fchOffset = fch.position();4647if (rafLength != expectedLength)48throw new RuntimeException("rafLength (" + rafLength + ") != " +49"expectedLength (" + expectedLength + ")");50if (rafOffset != expectedOffset)51throw new RuntimeException("rafOffset (" + rafOffset + ") != " +52"expectedOffset (" + expectedOffset + ")");53if (fchLength != expectedLength)54throw new RuntimeException("fchLength (" + fchLength + ") != " +55"expectedLength (" + expectedLength + ")");56if (fchOffset != expectedOffset)57throw new RuntimeException("fchOffset (" + fchOffset + ") != " +58"expectedOffset (" + expectedOffset + ")");59}6061public static void main(String[] args) throws Throwable {62File file = new File("tmp");63try (RandomAccessFile raf = new RandomAccessFile(file, "rw");64FileChannel fch = raf.getChannel()) {6566// initially empty67checkState(raf, fch, 0, 0);6869// seeking beyond EOF70raf.seek(42);71checkState(raf, fch, 42, 0);7273// seeking beyond EOF74fch.position(84);75checkState(raf, fch, 84, 0);7677// writing at offset beyond EOF78raf.write(1);79checkState(raf, fch, 85, 85);8081// truncating82raf.setLength(63);83checkState(raf, fch, 63, 63);8485// writing at EOF86fch.write(ByteBuffer.wrap(new byte[1]));87checkState(raf, fch, 64, 64);8889// seeking at the middle90fch.position(32);91checkState(raf, fch, 32, 64);9293// truncating beyond offset94fch.truncate(42);95checkState(raf, fch, 32, 42);9697// truncating before offset98fch.truncate(16);99checkState(raf, fch, 16, 16);100101// writing at position beyond EOF102fch.write(ByteBuffer.wrap(new byte[1]), 127);103checkState(raf, fch, 16, 128);104105// writing at position before EOF106fch.write(ByteBuffer.wrap(new byte[1]), 42);107checkState(raf, fch, 16, 128);108109// truncating110raf.setLength(64);111checkState(raf, fch, 16, 64);112113// changing offset114raf.seek(21);115checkState(raf, fch, 21, 64);116117// skipping should change offset118raf.skipBytes(4);119checkState(raf, fch, 25, 64);120121// reading should change offset122raf.read();123checkState(raf, fch, 26, 64);124125// truncating to zero126raf.setLength(0);127checkState(raf, fch, 0, 0);128129// FileChannel cannot expand size130fch.truncate(42);131checkState(raf, fch, 0, 0);132133// expanding134raf.setLength(42);135checkState(raf, fch, 0, 42);136137// seeking beyond EOF138raf.seek(512);139checkState(raf, fch, 512, 42);140141// truncating to the same size142fch.truncate(256);143checkState(raf, fch, 256, 42);144145// truncating to the same size146fch.truncate(42);147checkState(raf, fch, 42, 42);148149// truncating to zero150fch.truncate(0);151checkState(raf, fch, 0, 0);152}153}154}155156157