Path: blob/master/test/jdk/java/io/RandomAccessFile/FileLengthTest.java
41149 views
/*1* Copyright (c) 2015, 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/*24* @test25* @bug 482313326* @summary optimize RandomAccessFile.length() and length() is thread safe now.27*/28import java.io.File;29import java.io.FileNotFoundException;30import java.io.FileOutputStream;31import java.io.IOException;32import java.io.RandomAccessFile;3334/**35*36* @author [email protected]37*/38public class FileLengthTest {3940private static final int BUF_SIZE = 4096;41private static RandomAccessFile randomAccessFile;42private static Thread fileLengthCaller;43private static Thread fileContentReader;44private static StringBuilder fileContents;45private static volatile boolean isFailed = false;4647/**48* this thread will call length() in loop49*/50private static void startLengthThread() {51if (randomAccessFile == null) {52return;53}54fileLengthCaller = new Thread(() -> {55while (true) {56try {57long length = randomAccessFile.length();58if (length < 0) {59return;60}61} catch (IOException ex) {62return;63}64}65});66fileLengthCaller.setName("RandomAccessFile-length-caller");67fileLengthCaller.setDaemon(true);68fileLengthCaller.start();69}7071/**72* this thread will call read() and store the content in internal buffer.73*/74private static void startReaderThread() {75if (randomAccessFile == null) {76return;77}78fileContentReader = new Thread(() -> {79StringBuilder sb = new StringBuilder(BUF_SIZE);80int i;81byte arr[] = new byte[8];82try {83while ((i = randomAccessFile.read(arr)) != -1) {84sb.append(new String(arr, 0, i));85}86if (!sb.toString().equals(fileContents.toString())) {87isFailed = true;88}89} catch (IOException ex) {90}91});92fileContentReader.setName("RandomAccessFile-content-reader");93fileContentReader.setDaemon(true);94fileContentReader.start();95}9697public static void main(String args[]) {98byte arr[] = new byte[BUF_SIZE];99String testFile = "testfile.txt";100try {101createDummyFile(testFile);102File file = new File(testFile);103file.deleteOnExit();104randomAccessFile = new RandomAccessFile(file, "r");105int count = randomAccessFile.read(arr);106randomAccessFile.seek(0);107fileContents = new StringBuilder(BUF_SIZE);108fileContents.append(new String(arr, 0, count));109startLengthThread();110startReaderThread();111fileContentReader.join();112} catch (FileNotFoundException | InterruptedException ex) {113} catch (IOException ex) {114} finally {115try {116randomAccessFile.close();117} catch (IOException ex) {118}119}120if (isFailed) {121throw new RuntimeException("RandomAccessFile.length() changed the underlying file pointer.");122}123}124125private static void createDummyFile(String fileName) throws FileNotFoundException, IOException {126try (FileOutputStream outputStream = new FileOutputStream(new File(fileName))) {127String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";128int count = 0;129while ((count + str.length()) < BUF_SIZE) {130outputStream.write(str.getBytes());131count += str.length();132}133outputStream.flush();134}135}136}137138139