提交 eeac871b 编写于 作者: D dxu

8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run

Reviewed-by: alanb, chegar, mchung, lancea
上级 28b17b74
/* /*
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -39,67 +39,57 @@ import java.util.Random; ...@@ -39,67 +39,57 @@ import java.util.Random;
public class Size { public class Size {
private static Random generator = new Random();
private static File blah;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
test1(); testSmallFile();
test2(); testLargeFile();
} }
private static void test1() throws Exception { private static void testSmallFile() throws Exception {
blah = File.createTempFile("blah", null); File smallFile = new File("smallFileTest");
blah.deleteOnExit(); Random generator = new Random();
for(int i=0; i<100; i++) { for(int i=0; i<100; i++) {
long testSize = generator.nextInt(1000); long testSize = generator.nextInt(1000);
initTestFile(blah, testSize); initTestFile(smallFile, testSize);
FileInputStream fis = new FileInputStream(blah); try (FileChannel c = new FileInputStream(smallFile).getChannel()) {
FileChannel c = fis.getChannel(); if (c.size() != testSize) {
if (c.size() != testSize) throw new RuntimeException("Size failed in testSmallFile. "
throw new RuntimeException("Size failed"); + "Expect size " + testSize
c.close(); + ", actual size " + c.size());
fis.close(); }
}
} }
blah.delete(); smallFile.deleteOnExit();
} }
// Test for bug 4563125 // Test for bug 4563125
private static void test2() throws Exception { private static void testLargeFile() throws Exception {
// Windows and Linux can't handle the really large file sizes for a truncate File largeFile = new File("largeFileTest");
// or a positional write required by the test for 4563125 long testSize = ((long)Integer.MAX_VALUE) * 2;
String osName = System.getProperty("os.name"); initTestFile(largeFile, 10);
if (osName.startsWith("SunOS") || osName.contains("OS X")) { try (FileChannel fc = new RandomAccessFile(largeFile, "rw").getChannel())
blah = File.createTempFile("blah", null); {
long testSize = ((long)Integer.MAX_VALUE) * 2;
initTestFile(blah, 10);
RandomAccessFile raf = new RandomAccessFile(blah, "rw");
FileChannel fc = raf.getChannel();
fc.size(); fc.size();
fc.map(FileChannel.MapMode.READ_WRITE, testSize, 10); fc.map(FileChannel.MapMode.READ_WRITE, testSize, 10);
if (fc.size() != testSize + 10) if (fc.size() != testSize + 10) {
throw new RuntimeException("Size failed " + fc.size()); throw new RuntimeException("Size failed in testLargeFile. "
fc.close(); + "Expect size " + (testSize + 10)
raf.close(); + ", actual size " + fc.size());
blah.delete(); }
} }
largeFile.deleteOnExit();
} }
/** /**
* Creates file blah of specified size in bytes. * Create a file with the specified size in bytes.
* *
*/ */
private static void initTestFile(File blah, long size) throws Exception { private static void initTestFile(File f, long size) throws Exception {
if (blah.exists()) try (BufferedWriter awriter = new BufferedWriter(
blah.delete(); new OutputStreamWriter(new FileOutputStream(f), "8859_1")))
FileOutputStream fos = new FileOutputStream(blah); {
BufferedWriter awriter for(int i=0; i<size; i++) {
= new BufferedWriter(new OutputStreamWriter(fos, "8859_1")); awriter.write("e");
}
for(int i=0; i<size; i++) {
awriter.write("e");
} }
awriter.flush();
awriter.close();
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册