提交 b723941c 编写于 作者: A alanb

8000330: (fc) FileChannel.truncate issues when given size > file size

8002180: (fc) FileChannel.map does not throw NPE if MapMode specified as null
Reviewed-by: chegar
上级 90f14d4e
...@@ -302,12 +302,10 @@ public class FileChannelImpl ...@@ -302,12 +302,10 @@ public class FileChannelImpl
} }
} }
public FileChannel truncate(long size) throws IOException { public FileChannel truncate(long newSize) throws IOException {
ensureOpen(); ensureOpen();
if (size < 0) if (newSize < 0)
throw new IllegalArgumentException(); throw new IllegalArgumentException("Negative size");
if (size > size())
return this;
if (!writable) if (!writable)
throw new NonWritableChannelException(); throw new NonWritableChannelException();
synchronized (positionLock) { synchronized (positionLock) {
...@@ -320,6 +318,14 @@ public class FileChannelImpl ...@@ -320,6 +318,14 @@ public class FileChannelImpl
if (!isOpen()) if (!isOpen())
return null; return null;
// get current size
long size;
do {
size = nd.size(fd);
} while ((size == IOStatus.INTERRUPTED) && isOpen());
if (!isOpen())
return null;
// get current position // get current position
do { do {
p = position0(fd, -1); p = position0(fd, -1);
...@@ -328,16 +334,18 @@ public class FileChannelImpl ...@@ -328,16 +334,18 @@ public class FileChannelImpl
return null; return null;
assert p >= 0; assert p >= 0;
// truncate file // truncate file if given size is less than the current size
if (newSize < size) {
do { do {
rv = nd.truncate(fd, size); rv = nd.truncate(fd, newSize);
} while ((rv == IOStatus.INTERRUPTED) && isOpen()); } while ((rv == IOStatus.INTERRUPTED) && isOpen());
if (!isOpen()) if (!isOpen())
return null; return null;
}
// set position to size if greater than size // if position is beyond new size then adjust it
if (p > size) if (p > newSize)
p = size; p = newSize;
do { do {
rv = (int)position0(fd, p); rv = (int)position0(fd, p);
} while ((rv == IOStatus.INTERRUPTED) && isOpen()); } while ((rv == IOStatus.INTERRUPTED) && isOpen());
...@@ -779,6 +787,8 @@ public class FileChannelImpl ...@@ -779,6 +787,8 @@ public class FileChannelImpl
throws IOException throws IOException
{ {
ensureOpen(); ensureOpen();
if (mode == null)
throw new NullPointerException("Mode is null");
if (position < 0L) if (position < 0L)
throw new IllegalArgumentException("Negative position"); throw new IllegalArgumentException("Negative position");
if (size < 0L) if (size < 0L)
...@@ -787,6 +797,7 @@ public class FileChannelImpl ...@@ -787,6 +797,7 @@ public class FileChannelImpl
throw new IllegalArgumentException("Position + size overflow"); throw new IllegalArgumentException("Position + size overflow");
if (size > Integer.MAX_VALUE) if (size > Integer.MAX_VALUE)
throw new IllegalArgumentException("Size exceeds Integer.MAX_VALUE"); throw new IllegalArgumentException("Size exceeds Integer.MAX_VALUE");
int imode = -1; int imode = -1;
if (mode == MapMode.READ_ONLY) if (mode == MapMode.READ_ONLY)
imode = MAP_RO; imode = MAP_RO;
......
/* /*
* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2012, 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
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
*/ */
/* @test /* @test
* @bug 4429043 8002180
* @summary Test file mapping with FileChannel * @summary Test file mapping with FileChannel
* @run main/othervm MapTest * @run main/othervm MapTest
*/ */
...@@ -29,7 +30,10 @@ ...@@ -29,7 +30,10 @@
import java.io.*; import java.io.*;
import java.nio.MappedByteBuffer; import java.nio.MappedByteBuffer;
import java.nio.channels.*; import java.nio.channels.*;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel.MapMode;
import java.nio.file.Files;
import static java.nio.file.StandardOpenOption.*;
import static java.nio.charset.StandardCharsets.*;
import java.util.Random; import java.util.Random;
...@@ -39,6 +43,7 @@ import java.util.Random; ...@@ -39,6 +43,7 @@ import java.util.Random;
public class MapTest { public class MapTest {
private static PrintStream out = System.out;
private static PrintStream err = System.err; private static PrintStream err = System.err;
private static Random generator = new Random(); private static Random generator = new Random();
...@@ -51,15 +56,21 @@ public class MapTest { ...@@ -51,15 +56,21 @@ public class MapTest {
blah = File.createTempFile("blah", null); blah = File.createTempFile("blah", null);
blah.deleteOnExit(); blah.deleteOnExit();
initTestFile(blah); initTestFile(blah);
err.println("Test file " + blah + " initialized"); try {
out.println("Test file " + blah + " initialized");
testZero(); testZero();
err.println("Zero size: OK"); out.println("Zero size: OK");
testRead(); testRead();
err.println("Read: OK"); out.println("Read: OK");
testWrite(); testWrite();
err.println("Write: OK"); out.println("Write: OK");
testHighOffset(); testHighOffset();
err.println("High offset: OK"); out.println("High offset: OK");
testExceptions();
out.println("Exceptions: OK");
} finally {
blah.delete();
}
} }
/** /**
...@@ -77,30 +88,25 @@ public class MapTest { ...@@ -77,30 +88,25 @@ public class MapTest {
* ability to index into a file of multiple pages is tested. * ability to index into a file of multiple pages is tested.
*/ */
private static void initTestFile(File blah) throws Exception { private static void initTestFile(File blah) throws Exception {
FileOutputStream fos = new FileOutputStream(blah); try (BufferedWriter writer = Files.newBufferedWriter(blah.toPath(), ISO_8859_1)) {
BufferedWriter awriter for (int i=0; i<4000; i++) {
= new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
for(int i=0; i<4000; i++) {
String number = new Integer(i).toString(); String number = new Integer(i).toString();
for (int h=0; h<4-number.length(); h++) for (int h=0; h<4-number.length(); h++)
awriter.write("0"); writer.write("0");
awriter.write(""+i); writer.write(""+i);
awriter.newLine(); writer.newLine();
}
} }
awriter.flush();
awriter.close();
} }
/** /**
* Tests zero size file mapping * Tests zero size file mapping
*/ */
private static void testZero() throws Exception { private static void testZero() throws Exception {
FileInputStream fis = new FileInputStream(blah); try (FileInputStream fis = new FileInputStream(blah)) {
FileChannel c = fis.getChannel(); FileChannel fc = fis.getChannel();
MappedByteBuffer b = c.map(FileChannel.MapMode.READ_ONLY, 0, 0); MappedByteBuffer b = fc.map(MapMode.READ_ONLY, 0, 0);
c.close(); }
fis.close();
} }
/** /**
...@@ -108,18 +114,18 @@ public class MapTest { ...@@ -108,18 +114,18 @@ public class MapTest {
* from the ByteBuffer gets the right line number * from the ByteBuffer gets the right line number
*/ */
private static void testRead() throws Exception { private static void testRead() throws Exception {
StringBuffer sb = new StringBuffer(); StringBuilder sb = new StringBuilder();
sb.setLength(4); sb.setLength(4);
for (int x=0; x<1000; x++) { for (int x=0; x<1000; x++) {
FileInputStream fis = new FileInputStream(blah); try (FileInputStream fis = new FileInputStream(blah)) {
FileChannel c = fis.getChannel(); FileChannel fc = fis.getChannel();
long offset = generator.nextInt(10000); long offset = generator.nextInt(10000);
long expectedResult = offset / CHARS_PER_LINE; long expectedResult = offset / CHARS_PER_LINE;
offset = expectedResult * CHARS_PER_LINE; offset = expectedResult * CHARS_PER_LINE;
MappedByteBuffer b = c.map(FileChannel.MapMode.READ_ONLY, MappedByteBuffer b = fc.map(MapMode.READ_ONLY,
offset, 100); offset, 100);
for (int i=0; i<4; i++) { for (int i=0; i<4; i++) {
...@@ -133,8 +139,7 @@ public class MapTest { ...@@ -133,8 +139,7 @@ public class MapTest {
err.println("I got "+result); err.println("I got "+result);
throw new Exception("Read test failed"); throw new Exception("Read test failed");
} }
c.close(); }
fis.close();
} }
} }
...@@ -143,15 +148,15 @@ public class MapTest { ...@@ -143,15 +148,15 @@ public class MapTest {
* written out to the file can be read back in * written out to the file can be read back in
*/ */
private static void testWrite() throws Exception { private static void testWrite() throws Exception {
StringBuffer sb = new StringBuffer(); StringBuilder sb = new StringBuilder();
sb.setLength(4); sb.setLength(4);
for (int x=0; x<1000; x++) { for (int x=0; x<1000; x++) {
RandomAccessFile raf = new RandomAccessFile(blah, "rw"); try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {
FileChannel c = raf.getChannel(); FileChannel fc = raf.getChannel();
long offset = generator.nextInt(1000); long offset = generator.nextInt(1000);
MappedByteBuffer b = c.map(FileChannel.MapMode.READ_WRITE, MappedByteBuffer b = fc.map(MapMode.READ_WRITE,
offset, 100); offset, 100);
for (int i=0; i<4; i++) { for (int i=0; i<4; i++) {
...@@ -164,25 +169,138 @@ public class MapTest { ...@@ -164,25 +169,138 @@ public class MapTest {
} }
if (!sb.toString().equals("0123")) if (!sb.toString().equals("0123"))
throw new Exception("Write test failed"); throw new Exception("Write test failed");
c.close(); }
raf.close();
} }
} }
private static void testHighOffset() throws Exception { private static void testHighOffset() throws Exception {
StringBuffer sb = new StringBuffer(); StringBuilder sb = new StringBuilder();
sb.setLength(4); sb.setLength(4);
for (int x=0; x<1000; x++) { for (int x=0; x<1000; x++) {
RandomAccessFile raf = new RandomAccessFile(blah, "rw"); try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {
FileChannel fc = raf.getChannel(); FileChannel fc = raf.getChannel();
long offset = 66000; long offset = 66000;
MappedByteBuffer b = fc.map(FileChannel.MapMode.READ_WRITE, MappedByteBuffer b = fc.map(MapMode.READ_WRITE,
offset, 100); offset, 100);
}
}
}
/**
* Test exceptions specified by map method
*/
private static void testExceptions() throws Exception {
// check exceptions when channel opened for read access
try (FileChannel fc = FileChannel.open(blah.toPath(), READ)) {
testExceptions(fc);
checkException(fc, MapMode.READ_WRITE, 0L, fc.size(),
NonWritableChannelException.class);
checkException(fc, MapMode.READ_WRITE, -1L, fc.size(),
NonWritableChannelException.class, IllegalArgumentException.class);
checkException(fc, MapMode.READ_WRITE, 0L, -1L,
NonWritableChannelException.class, IllegalArgumentException.class);
checkException(fc, MapMode.PRIVATE, 0L, fc.size(),
NonWritableChannelException.class);
checkException(fc, MapMode.PRIVATE, -1L, fc.size(),
NonWritableChannelException.class, IllegalArgumentException.class);
checkException(fc, MapMode.PRIVATE, 0L, -1L,
NonWritableChannelException.class, IllegalArgumentException.class);
}
// check exceptions when channel opened for write access
try (FileChannel fc = FileChannel.open(blah.toPath(), WRITE)) {
testExceptions(fc);
checkException(fc, MapMode.READ_ONLY, 0L, fc.size(),
NonReadableChannelException.class);
checkException(fc, MapMode.READ_ONLY, -1L, fc.size(),
NonReadableChannelException.class, IllegalArgumentException.class);
fc.close(); /*
raf.close(); * implementation/spec mismatch, these tests disabled for now
*/
//checkException(fc, MapMode.READ_WRITE, 0L, fc.size(),
// NonWritableChannelException.class);
//checkException(fc, MapMode.PRIVATE, 0L, fc.size(),
// NonWritableChannelException.class);
}
// check exceptions when channel opened for read and write access
try (FileChannel fc = FileChannel.open(blah.toPath(), READ, WRITE)) {
testExceptions(fc);
} }
} }
private static void testExceptions(FileChannel fc) throws IOException {
checkException(fc, null, 0L, fc.size(),
NullPointerException.class);
checkException(fc, MapMode.READ_ONLY, -1L, fc.size(),
IllegalArgumentException.class);
checkException(fc, null, -1L, fc.size(),
IllegalArgumentException.class, NullPointerException.class);
checkException(fc, MapMode.READ_ONLY, 0L, -1L,
IllegalArgumentException.class);
checkException(fc, null, 0L, -1L,
IllegalArgumentException.class, NullPointerException.class);
checkException(fc, MapMode.READ_ONLY, 0L, Integer.MAX_VALUE + 1L,
IllegalArgumentException.class);
checkException(fc, null, 0L, Integer.MAX_VALUE + 1L,
IllegalArgumentException.class, NullPointerException.class);
checkException(fc, MapMode.READ_ONLY, Long.MAX_VALUE, 1L,
IllegalArgumentException.class);
checkException(fc, null, Long.MAX_VALUE, 1L,
IllegalArgumentException.class, NullPointerException.class);
}
/**
* Checks that FileChannel map throws one of the expected exceptions
* when invoked with the given inputs.
*/
private static void checkException(FileChannel fc,
MapMode mode,
long position,
long size,
Class<?>... expected)
throws IOException
{
Exception exc = null;
try {
fc.map(mode, position, size);
} catch (Exception actual) {
exc = actual;
}
if (exc != null) {
for (Class<?> clazz: expected) {
if (clazz.isInstance(exc)) {
return;
}
}
}
System.err.println("Expected one of");
for (Class<?> clazz: expected) {
System.out.println(clazz);
}
if (exc == null) {
throw new RuntimeException("No expection thrown");
} else {
throw new RuntimeException("Unexpected exception thrown", exc);
}
}
} }
/* /*
* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2012, 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
...@@ -22,14 +22,16 @@ ...@@ -22,14 +22,16 @@
*/ */
/* @test /* @test
* @bug 6191269 6709457 * @bug 6191269 6709457 8000330
* @summary Test truncate method of FileChannel * @summary Test truncate method of FileChannel
*/ */
import java.io.*; import java.io.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.*;
import java.nio.file.Files;
import static java.nio.file.StandardOpenOption.*; import static java.nio.file.StandardOpenOption.*;
import static java.nio.charset.StandardCharsets.*;
import java.util.Random; import java.util.Random;
...@@ -46,6 +48,7 @@ public class Truncate { ...@@ -46,6 +48,7 @@ public class Truncate {
try { try {
basicTest(blah); basicTest(blah);
appendTest(blah); appendTest(blah);
exceptionTests(blah);
} finally { } finally {
blah.delete(); blah.delete();
} }
...@@ -66,15 +69,22 @@ public class Truncate { ...@@ -66,15 +69,22 @@ public class Truncate {
if (fc.size() != testSize) if (fc.size() != testSize)
throw new RuntimeException("Size failed"); throw new RuntimeException("Size failed");
long position = generator.nextInt((int)testSize); long position = generator.nextInt((int)testSize*2);
fc.position(position); fc.position(position);
long newSize = generator.nextInt((int)testSize); long newSize = generator.nextInt((int)testSize*2);
fc.truncate(newSize); fc.truncate(newSize);
// check new size
if (newSize > testSize) {
if (fc.size() != testSize)
throw new RuntimeException("Attempt to expand file changed size");
} else {
if (fc.size() != newSize) if (fc.size() != newSize)
throw new RuntimeException("Truncate failed"); throw new RuntimeException("Unexpected size after truncate");
}
// check new position
if (position > newSize) { if (position > newSize) {
if (fc.position() != newSize) if (fc.position() != newSize)
throw new RuntimeException("Position greater than size"); throw new RuntimeException("Position greater than size");
...@@ -114,21 +124,91 @@ public class Truncate { ...@@ -114,21 +124,91 @@ public class Truncate {
} }
} }
/**
* Test exceptions specified by truncate method
*/
static void exceptionTests(File blah) throws Exception {
// check exceptions when channel opened for read access
try (FileChannel fc = FileChannel.open(blah.toPath(), READ)) {
long size = fc.size();
// open channel
checkException(fc, 0L, NonWritableChannelException.class);
checkException(fc, -1L, NonWritableChannelException.class,
IllegalArgumentException.class);
checkException(fc, size+1L, NonWritableChannelException.class);
// closed channel
fc.close();
checkException(fc, 0L, ClosedChannelException.class);
checkException(fc, -1L, ClosedChannelException.class,
IllegalArgumentException.class);
checkException(fc, size+1L, ClosedChannelException.class);
}
// check exceptions when channel opened for write access
try (FileChannel fc = FileChannel.open(blah.toPath(), WRITE)) {
long size = fc.size();
// open channel
checkException(fc, -1L, IllegalArgumentException.class);
// closed channel
fc.close();
checkException(fc, 0L, ClosedChannelException.class);
checkException(fc, -1L, ClosedChannelException.class,
IllegalArgumentException.class);
checkException(fc, size+1L, ClosedChannelException.class);
}
}
/**
* Checks that FileChannel truncate throws one of the expected exceptions
* when invoked with the given size.
*/
private static void checkException(FileChannel fc, long size, Class<?>... expected)
throws IOException
{
Exception exc = null;
try {
fc.truncate(size);
} catch (Exception actual) {
exc = actual;
}
if (exc != null) {
for (Class<?> clazz: expected) {
if (clazz.isInstance(exc)) {
return;
}
}
}
System.err.println("Expected one of");
for (Class<?> clazz: expected) {
System.err.println(clazz);
}
if (exc == null) {
throw new RuntimeException("No expection thrown");
} else {
throw new RuntimeException("Unexpected exception thrown", exc);
}
}
/** /**
* Creates file blah of specified size in bytes. * Creates file blah of specified size in bytes.
*
*/ */
private static void initTestFile(File blah, long size) throws Exception { private static void initTestFile(File blah, long size) throws Exception {
if (blah.exists()) try (BufferedWriter writer = Files.newBufferedWriter(blah.toPath(), ISO_8859_1)) {
blah.delete();
FileOutputStream fos = new FileOutputStream(blah);
BufferedWriter awriter
= new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
for(int i=0; i<size; i++) { for(int i=0; i<size; i++) {
awriter.write("e"); writer.write("e");
}
} }
awriter.flush();
awriter.close();
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册