提交 430c4c4e 编写于 作者: A alanb

6526860: (fc) FileChannel.position returns 0 when FileOutputStream opened in append mode

Reviewed-by: forax
上级 e22c5084
...@@ -51,6 +51,7 @@ public class FileChannelImpl ...@@ -51,6 +51,7 @@ public class FileChannelImpl
// File access mode (immutable) // File access mode (immutable)
private final boolean writable; private final boolean writable;
private final boolean readable; private final boolean readable;
private final boolean append;
// Required to prevent finalization of creating stream (immutable) // Required to prevent finalization of creating stream (immutable)
private final Object parent; private final Object parent;
...@@ -67,6 +68,7 @@ public class FileChannelImpl ...@@ -67,6 +68,7 @@ public class FileChannelImpl
this.fd = fd; this.fd = fd;
this.readable = readable; this.readable = readable;
this.writable = writable; this.writable = writable;
this.append = append;
this.parent = parent; this.parent = parent;
this.nd = new FileDispatcherImpl(append); this.nd = new FileDispatcherImpl(append);
} }
...@@ -242,7 +244,8 @@ public class FileChannelImpl ...@@ -242,7 +244,8 @@ public class FileChannelImpl
if (!isOpen()) if (!isOpen())
return 0; return 0;
do { do {
p = position0(fd, -1); // in append-mode then position is advanced to end before writing
p = (append) ? nd.size(fd) : position0(fd, -1);
} while ((p == IOStatus.INTERRUPTED) && isOpen()); } while ((p == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(p); return IOStatus.normalize(p);
} finally { } finally {
......
...@@ -136,7 +136,7 @@ class UnixChannelFactory { ...@@ -136,7 +136,7 @@ class UnixChannelFactory {
throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed"); throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");
FileDescriptor fdObj = open(dfd, path, pathForPermissionCheck, flags, mode); FileDescriptor fdObj = open(dfd, path, pathForPermissionCheck, flags, mode);
return FileChannelImpl.open(fdObj, flags.read, flags.write, null); return FileChannelImpl.open(fdObj, flags.read, flags.write, flags.append, null);
} }
/** /**
......
...@@ -22,13 +22,16 @@ ...@@ -22,13 +22,16 @@
*/ */
/* @test /* @test
* @bug 4429043 6526860
* @summary Test position method of FileChannel * @summary Test position method of FileChannel
*/ */
import java.io.*; import java.io.*;
import java.nio.MappedByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.file.*;
import static java.nio.file.StandardOpenOption.*;
import java.nio.charset.Charset;
import java.util.Random; import java.util.Random;
...@@ -38,32 +41,42 @@ import java.util.Random; ...@@ -38,32 +41,42 @@ import java.util.Random;
public class Position { public class Position {
private static PrintStream err = System.err; private static final Charset ISO8859_1 = Charset.forName("8859_1");
private static Random generator = new Random(); private static final Random generator = new Random();
private static int CHARS_PER_LINE = File.separatorChar == '/' ? 5 : 6;
private static File blah;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
blah = File.createTempFile("blah", null); Path blah = Files.createTempFile("blah", null);
blah.deleteOnExit(); blah.toFile().deleteOnExit();
initTestFile(blah); initTestFile(blah);
FileInputStream fis = new FileInputStream(blah); for (int i=0; i<10; i++) {
FileChannel c = fis.getChannel(); try (FileChannel fc = (generator.nextBoolean()) ?
FileChannel.open(blah, READ) :
new FileInputStream(blah.toFile()).getChannel()) {
for (int j=0; j<100; j++) {
long newPos = generator.nextInt(1000);
fc.position(newPos);
if (fc.position() != newPos)
throw new RuntimeException("Position failed");
}
}
}
for(int i=0; i<100; i++) { for (int i=0; i<10; i++) {
long newPos = generator.nextInt(1000); try (FileChannel fc = (generator.nextBoolean()) ?
c.position(newPos); FileChannel.open(blah, APPEND) :
if (c.position() != newPos) new FileOutputStream(blah.toFile(), true).getChannel()) {
throw new RuntimeException("Position failed"); for (int j=0; j<10; j++) {
if (fc.position() != fc.size())
throw new RuntimeException("Position expected to be size");
byte[] buf = new byte[generator.nextInt(100)];
fc.write(ByteBuffer.wrap(buf));
}
}
} }
c.close(); Files.delete(blah);
fis.close();
blah.delete();
} }
/** /**
...@@ -78,19 +91,15 @@ public class Position { ...@@ -78,19 +91,15 @@ public class Position {
* 3999 * 3999
* *
*/ */
private static void initTestFile(File blah) throws Exception { private static void initTestFile(Path blah) throws IOException {
FileOutputStream fos = new FileOutputStream(blah); try (BufferedWriter awriter = Files.newBufferedWriter(blah, ISO8859_1)) {
BufferedWriter awriter for(int i=0; i<4000; i++) {
= new BufferedWriter(new OutputStreamWriter(fos, "8859_1")); String number = new Integer(i).toString();
for (int h=0; h<4-number.length(); h++)
for(int i=0; i<4000; i++) { awriter.write("0");
String number = new Integer(i).toString(); awriter.write(""+i);
for (int h=0; h<4-number.length(); h++) awriter.newLine();
awriter.write("0"); }
awriter.write(""+i);
awriter.newLine();
} }
awriter.flush();
awriter.close();
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册