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/03/06 14:09:01 UTC

svn commit: r515112 - in /harmony/enhanced/classlib/trunk/modules: luni/src/main/java/org/apache/harmony/luni/platform/ luni/src/main/native/luni/unix/ luni/src/main/native/luni/windows/ nio/src/main/java/org/apache/harmony/nio/internal/ nio/src/test/j...

Author: ayza
Date: Tue Mar  6 05:08:59 2007
New Revision: 515112

URL: http://svn.apache.org/viewvc?view=rev&rev=515112
Log:
Applying updated version of the patch from HARMONY-3085 ( [classlib][nio] FileChannel.transferTo() and FileChannel.map() throws IOException when used file is large and position is equal or greater than 4096)

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/IFileSystem.java
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSFileSystem.java
    harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/OSFileSystemLinux32.c
    harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/exports.txt
    harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSFileSystemWin32.c
    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/luni/src/main/java/org/apache/harmony/luni/platform/IFileSystem.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/IFileSystem.java?view=diff&rev=515112&r1=515111&r2=515112
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/IFileSystem.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/IFileSystem.java Tue Mar  6 05:08:59 2007
@@ -92,7 +92,10 @@
 
 	public void truncate(long fileDescriptor, long size) throws IOException;
 
-	public int getPageSize() throws IOException;
+	/**
+	 * Returns the granularity for virtual memory allocation.
+	 */
+	public int getAllocGranularity() throws IOException;
 
 	public long open(byte[] fileName, int mode) throws FileNotFoundException;
 

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSFileSystem.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSFileSystem.java?view=diff&rev=515112&r1=515111&r2=515112
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSFileSystem.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSFileSystem.java Tue Mar  6 05:08:59 2007
@@ -56,7 +56,12 @@
 	private native int lockImpl(long fileDescriptor, long start, long length,
 			int type, boolean wait);
 
-	public native int getPageSize();
+	/**
+	 * Returns the granularity for virtual memory allocation.
+	 * Note that this value for Windows differs from the one for the
+	 * page size (64K and 4K respectively).
+	 */
+	public native int getAllocGranularity() throws IOException;
 
 	public boolean lock(long fileDescriptor, long start, long length, int type,
 			boolean waitFlag) throws IOException {

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/OSFileSystemLinux32.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/OSFileSystemLinux32.c?view=diff&rev=515112&r1=515111&r2=515112
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/OSFileSystemLinux32.c (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/OSFileSystemLinux32.c Tue Mar  6 05:08:59 2007
@@ -129,15 +129,21 @@
   return (rc == -1) ? -1 : 0;
 }
 
-
-JNIEXPORT jint JNICALL Java_org_apache_harmony_luni_platform_OSFileSystem_getPageSize
+/**
+ * Returns the granularity of the starting address for virtual memory allocation.
+ * (It's the same as the page size.)
+ * Class:     org_apache_harmony_luni_platform_OSFileSystem
+ * Method:    getAllocGranularity
+ * Signature: ()I
+ */
+JNIEXPORT jint JNICALL Java_org_apache_harmony_luni_platform_OSFileSystem_getAllocGranularity
   (JNIEnv * env, jobject thiz)
 {
-  static int pageSize = 0;
-  if(pageSize == 0){
-    pageSize = getpagesize();
+  static int allocGranularity = 0;
+  if(allocGranularity == 0){
+    allocGranularity = getpagesize();
   }
-  return pageSize;
+  return allocGranularity;
 }
 
 /*

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/exports.txt
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/exports.txt?view=diff&rev=515112&r1=515111&r2=515112
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/exports.txt (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/exports.txt Tue Mar  6 05:08:59 2007
@@ -154,7 +154,7 @@
 Java_org_apache_harmony_luni_util_FloatingPointParser_parseFltImpl
 Java_org_apache_harmony_luni_util_NumberConverter_bigIntDigitGeneratorInstImpl
 Java_org_apache_harmony_luni_platform_OSFileSystem_truncateImpl
-Java_org_apache_harmony_luni_platform_OSFileSystem_getPageSize
+Java_org_apache_harmony_luni_platform_OSFileSystem_getAllocGranularity
 Java_org_apache_harmony_luni_platform_OSFileSystem_writevImpl
 Java_org_apache_harmony_luni_platform_OSFileSystem_readvImpl
 Java_org_apache_harmony_luni_platform_OSFileSystem_writeImpl

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSFileSystemWin32.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSFileSystemWin32.c?view=diff&rev=515112&r1=515111&r2=515112
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSFileSystemWin32.c (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSFileSystemWin32.c Tue Mar  6 05:08:59 2007
@@ -114,16 +114,23 @@
   return (jint) - 1;
 }
 
-JNIEXPORT jint JNICALL Java_org_apache_harmony_luni_platform_OSFileSystem_getPageSize
+/**
+ * Returns the granularity of the starting address for virtual memory allocation.
+ * (Note, that it differs from page size.)
+ * Class:     org_apache_harmony_luni_platform_OSFileSystem
+ * Method:    getAllocGranularity
+ * Signature: ()I
+ */
+JNIEXPORT jint JNICALL Java_org_apache_harmony_luni_platform_OSFileSystem_getAllocGranularity
   (JNIEnv * env, jobject thiz)
 {
-  static DWORD pageSize = 0;
-  if(!pageSize){
+  static DWORD allocGranularity = 0;
+  if(!allocGranularity){
       SYSTEM_INFO info;
       GetSystemInfo(&info);
-      pageSize = info.dwPageSize;
+      allocGranularity = info.dwAllocationGranularity;
   }
-  return pageSize;
+  return allocGranularity;
 }
 
 /*

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=515112&r1=515111&r2=515112
==============================================================================
--- 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 Tue Mar  6 05:08:59 2007
@@ -48,11 +48,11 @@
 	// Reference to the portable file system code.
 	private static final IFileSystem fileSystem = Platform.getFileSystem();
 
-    private static final int NATIVE_PAGE_SIZE;
+    private static final int ALLOC_GRANULARITY;
 
     static {
         try {
-            NATIVE_PAGE_SIZE = fileSystem.getPageSize();
+            ALLOC_GRANULARITY = fileSystem.getAllocGranularity();
         } catch (IOException e) {
             throw new Error(e);
         }
@@ -188,7 +188,7 @@
         if (position + size > size()) {
             fileSystem.truncate(handle, position + size);
         }
-        long alignment = position - position % NATIVE_PAGE_SIZE;
+        long alignment = position - position % ALLOC_GRANULARITY;
         int offset = (int) (position - alignment);
         PlatformAddress address = PlatformAddressFactory.allocMap(handle, alignment, size
                 + offset, mapMode);

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=515112&r1=515111&r2=515112
==============================================================================
--- 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 Tue Mar  6 05:08:59 2007
@@ -43,6 +43,7 @@
 import java.nio.channels.WritableByteChannel;
 import java.nio.channels.FileChannel.MapMode;
 import java.util.Arrays;
+import org.apache.harmony.luni.platform.Platform;
 
 import junit.framework.TestCase;
 
@@ -307,6 +308,27 @@
     }
 
     /**
+     * Initializes large test file.
+     * 
+     * @param file the file to be written
+     * @param size the content size to be written
+     * @throws FileNotFoundException
+     * @throws IOException
+     */
+    private void writeLargeDataToFile(File file, int size) throws FileNotFoundException,
+            IOException {
+        FileOutputStream fos = new FileOutputStream(file);
+        byte[] buf = new byte[size];
+
+        try {
+            // we don't care about content - just need a particular file size
+            fos.write(buf);
+        } finally {
+            fos.close();
+        }
+    }
+
+    /**
      * @tests java.nio.channels.FileChannel#position()
      */
     public void test_position_WriteOnly() throws Exception {
@@ -1929,6 +1951,51 @@
         String expected = CONTENT.substring(0, 10) + "test"
                 + CONTENT.substring(10 + "test".length());
         assertEquals(expected, new String(checkBuffer.array(), "iso8859-1"));
+    }
+
+    /**
+     * Tests map() method for the value of positions exceeding memory
+     * page size and allocation granularity size.
+     *
+     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
+     */
+    public void test_map_LargePosition() throws IOException {
+        // Regression test for HARMONY-3085
+        int[] sizes = {
+            4096, // 4K size (normal page size for Linux & Windows)
+            65536, // 64K size (alocation granularity size for Windows)
+            Platform.getFileSystem().getAllocGranularity() // alloc granularity
+        };
+        final int CONTENT_LEN = 10;
+
+        for (int i = 0; i < sizes.length; ++i) {
+            // reset the file and the channel for the iterations
+            // (for the first iteration it was done by setUp()
+            if (i > 0 ) {
+                fileOfReadOnlyFileChannel = File.createTempFile(
+                        "File_of_readOnlyFileChannel", "tmp");
+                fileOfReadOnlyFileChannel.deleteOnExit();
+                readOnlyFileChannel = new FileInputStream(fileOfReadOnlyFileChannel)
+                        .getChannel();
+            }
+
+            writeLargeDataToFile(fileOfReadOnlyFileChannel, sizes[i] + 2 * CONTENT_LEN);
+            MappedByteBuffer mapped = readOnlyFileChannel.map(MapMode.READ_ONLY,
+                    sizes[i], CONTENT_LEN);
+            assertEquals("Incorrectly mapped file channel for " + sizes[i]
+                    + " position (capacity)", CONTENT_LEN, mapped.capacity());
+            assertEquals("Incorrectly mapped file channel for " + sizes[i]
+                    + " position (limit)", CONTENT_LEN, mapped.limit());
+            assertEquals("Incorrectly mapped file channel for " + sizes[i]
+                    + " position (position)", 0, mapped.position());
+
+            // map not change channel's position
+            assertEquals(0, readOnlyFileChannel.position());
+
+            // Close the file and the channel before the next iteration
+            readOnlyFileChannel.close();
+            fileOfReadOnlyFileChannel.delete();
+        }
     }
 
     /**