You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ay...@apache.org on 2007/04/13 13:36:16 UTC
svn commit: r528443 - in /harmony/enhanced/classlib/trunk/modules/nio/src:
main/java/java/nio/ main/java/org/apache/harmony/nio/internal/
test/java/common/org/apache/harmony/nio/tests/java/nio/channels/
Author: ayza
Date: Fri Apr 13 04:36:15 2007
New Revision: 528443
URL: http://svn.apache.org/viewvc?view=rev&rev=528443
Log:
Applying patch from HARMONY-3324 ([classlib][nio] It is not possible to delete file after transferring data from it)
Modified:
harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/MappedByteBufferAdapter.java
harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileChannelImpl.java
harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/FileChannelTest.java
Modified: harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/MappedByteBufferAdapter.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/MappedByteBufferAdapter.java?view=diff&rev=528443&r1=528442&r2=528443
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/MappedByteBufferAdapter.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/MappedByteBufferAdapter.java Fri Apr 13 04:36:15 2007
@@ -357,7 +357,6 @@
}
public void free() {
- ((MappedPlatformAddress)this.wrapped.getBaseAddress()).free();
this.wrapped.free();
}
Modified: harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileChannelImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileChannelImpl.java?view=diff&rev=528443&r1=528442&r2=528443
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileChannelImpl.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileChannelImpl.java Fri Apr 13 04:36:15 2007
@@ -404,19 +404,29 @@
}
ByteBuffer buffer = null;
- if (src instanceof FileChannel) {
- FileChannel fileSrc = (FileChannel) src;
- long size = fileSrc.size();
- long filePosition = fileSrc.position();
- count = Math.min(count, size - filePosition);
- buffer = fileSrc.map(MapMode.READ_ONLY, filePosition, count);
- fileSrc.position(filePosition + count);
- } else {
- buffer = ByteBuffer.allocateDirect((int) count);
- src.read(buffer);
- buffer.flip();
+
+ try {
+ if (src instanceof FileChannel) {
+ FileChannel fileSrc = (FileChannel) src;
+ long size = fileSrc.size();
+ long filePosition = fileSrc.position();
+ count = Math.min(count, size - filePosition);
+ buffer = fileSrc.map(MapMode.READ_ONLY, filePosition, count);
+ fileSrc.position(filePosition + count);
+ } else {
+ buffer = ByteBuffer.allocateDirect((int) count);
+ src.read(buffer);
+ buffer.flip();
+ }
+ return write(buffer, position);
+ } finally {
+ // unmap the buffer
+ if (buffer != null) {
+ // all children of FileChannelImpl currently returns
+ // an instance of DirectBuffer from map() method
+ ((DirectBuffer) buffer).free();
+ }
}
- return write(buffer, position);
}
public long transferTo(long position, long count, WritableByteChannel target)
@@ -443,8 +453,18 @@
return kernelTransfer(handle, ((SocketChannelImpl) target).getFD(),
position, count);
}
- buffer = map(MapMode.READ_ONLY, position, count);
- return target.write(buffer);
+
+ try {
+ buffer = map(MapMode.READ_ONLY, position, count);
+ return target.write(buffer);
+ } finally {
+ // unmap the buffer
+ if (buffer != null) {
+ // all children of FileChannelImpl currently returns
+ // an instance of DirectBuffer from map() method
+ ((DirectBuffer) buffer).free();
+ }
+ }
}
private long kernelTransfer(long l, FileDescriptor fd, long position,
Modified: harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/FileChannelTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/FileChannelTest.java?view=diff&rev=528443&r1=528442&r2=528443
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/FileChannelTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/FileChannelTest.java Fri Apr 13 04:36:15 2007
@@ -3162,6 +3162,52 @@
assertEquals(CONTENT_AS_BYTES[i], readBuffer.get());
}
}
+
+ /**
+ * Regression test for Harmony-3324
+ * Make sure we could delete the file after we called transferTo() method.
+ */
+ public void test_transferTo_couldDelete() throws Exception {
+ // init data in files
+ writeDataToFile(fileOfReadOnlyFileChannel);
+ writeDataToFile(fileOfWriteOnlyFileChannel);
+
+ // call transferTo() method
+ readOnlyFileChannel.transferTo(0 , 2, writeOnlyFileChannel);
+
+ // delete both files
+ readOnlyFileChannel.close();
+ writeOnlyFileChannel.close();
+ boolean rDel = fileOfReadOnlyFileChannel.delete();
+ boolean wDel = fileOfWriteOnlyFileChannel.delete();
+
+ // make sure both files were deleted
+ assertTrue("File " + readOnlyFileChannel + " exists", rDel);
+ assertTrue("File " + writeOnlyFileChannel + " exists", wDel);
+ }
+
+ /**
+ * Regression test for Harmony-3324
+ * Make sure we could delete the file after we called transferFrom() method.
+ */
+ public void test_transferFrom_couldDelete() throws Exception {
+ // init data in files
+ writeDataToFile(fileOfReadOnlyFileChannel);
+ writeDataToFile(fileOfWriteOnlyFileChannel);
+
+ // call transferTo() method
+ writeOnlyFileChannel.transferFrom(readOnlyFileChannel, 0 , 2);
+
+ // delete both files
+ readOnlyFileChannel.close();
+ writeOnlyFileChannel.close();
+ boolean rDel = fileOfReadOnlyFileChannel.delete();
+ boolean wDel = fileOfWriteOnlyFileChannel.delete();
+
+ // make sure both files were deleted
+ assertTrue("File " + readOnlyFileChannel + " exists", rDel);
+ assertTrue("File " + writeOnlyFileChannel + " exists", wDel);
+ }
private class MockFileChannel extends FileChannel {