Path: blob/master/test/jdk/java/io/RandomAccessFile/SetLength.java
41149 views
/*1* Copyright (c) 1997, 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 General tests of the setLength method26* @library /test/lib27* @build jdk.test.lib.RandomFactory28* @run main SetLength29* @key randomness30*/3132import java.io.IOException;33import java.io.File;34import java.io.RandomAccessFile;3536import jdk.test.lib.RandomFactory;3738public class SetLength {3940static void checkState(RandomAccessFile f, long expectedFilePointer,41long expectedLength)42throws IOException43{44long filePointer = f.getFilePointer();45long length = f.length();46if (length != expectedLength) {47throw new RuntimeException("File length " + length + " != expected "48+ expectedLength);49}50if (filePointer != expectedFilePointer) {51throw new RuntimeException("File pointer " + filePointer52+ " != expected " + expectedFilePointer);53}54}5556static void test(RandomAccessFile f, long quarterLength)57throws IOException58{59long halfLength = 2 * quarterLength;60long threeQuarterLength = 3 * quarterLength;61long fullLength = 4 * quarterLength;6263// initially, empty file64checkState(f, 0, 0);6566// extending the file size67f.setLength(halfLength);68checkState(f, 0, halfLength);6970// writing from the begining71f.write(new byte[(int)fullLength]);72checkState(f, fullLength, fullLength);7374// setting to the same length75f.setLength(fullLength);76checkState(f, fullLength, fullLength);7778// truncating the file79f.setLength(threeQuarterLength);80checkState(f, threeQuarterLength, threeQuarterLength);8182// changing the file pointer83f.seek(quarterLength);84checkState(f, quarterLength, threeQuarterLength);8586// truncating the file again87f.setLength(halfLength);88checkState(f, quarterLength, halfLength);8990// writing from the middle with extending the file91f.write(new byte[(int)halfLength]);92checkState(f, threeQuarterLength, threeQuarterLength);9394// changing the file pointer95f.seek(quarterLength);96checkState(f, quarterLength, threeQuarterLength);9798// writing from the middle without extending the file99f.write(new byte[(int)quarterLength]);100checkState(f, halfLength, threeQuarterLength);101102// changing the file pointer to the end of file103f.seek(threeQuarterLength);104checkState(f, threeQuarterLength, threeQuarterLength);105106// writing to the end of file107f.write(new byte[(int)quarterLength]);108checkState(f, fullLength, fullLength);109110// truncating the file to zero111f.setLength(0);112checkState(f, 0, 0);113114// changing the file pointer beyond the end of file115f.seek(threeQuarterLength);116checkState(f, threeQuarterLength, 0);117118// writing beyont the end of file119f.write(new byte[(int)quarterLength]);120checkState(f, fullLength, fullLength);121122// negative file pointer123try {124f.seek(-1);125throw new RuntimeException("IOE not thrown");126} catch (IOException expected) {127}128checkState(f, fullLength, fullLength);129130// truncating the file after failed seek131f.setLength(halfLength);132checkState(f, halfLength, halfLength);133134// truncating after closing135f.close();136try {137f.setLength(halfLength);138throw new RuntimeException("IOE not thrown");139} catch (IOException expected) {140}141}142143public static void main(String[] args) throws IOException {144File f28b = new File("f28b");145File f28K = new File("f28K");146File frnd = new File("frnd");147148try (RandomAccessFile raf28b = new RandomAccessFile(f28b, "rw");149RandomAccessFile raf28K = new RandomAccessFile(f28K, "rw");150RandomAccessFile rafrnd = new RandomAccessFile(frnd, "rw")) {151test(raf28b, 7);152test(raf28K, 7 * 1024);153test(rafrnd, 1 + RandomFactory.getRandom().nextInt(16000));154}155156// truncating read-only file157try (RandomAccessFile raf28b = new RandomAccessFile(f28b, "r")) {158raf28b.setLength(42);159throw new RuntimeException("IOE not thrown");160} catch (IOException expected) {161}162}163164}165166167