harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ghar...@apache.org
Subject svn commit: r420476 - in /incubator/harmony/enhanced/classlib/trunk/modules: luni/src/main/java/org/apache/harmony/luni/platform/ luni/src/main/native/luni/linux/ luni/src/main/native/luni/windows/ nio/src/main/java/org/apache/harmony/nio/internal/ nio...
Date Mon, 10 Jul 2006 10:16:37 GMT
Author: gharley
Date: Mon Jul 10 03:16:25 2006
New Revision: 420476

URL: http://svn.apache.org/viewvc?rev=420476&view=rev
Log:
HARMONY 808 : [nio] mmap native codes are not implemented in Harmony

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSMemory.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/OSMemory.c
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/OSMemoryLinux32.c
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSMemory.c
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSMemoryWin32.c
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileChannelImpl.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/FileChannelTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSMemory.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSMemory.java?rev=420476&r1=420475&r2=420476&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSMemory.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSMemory.java
Mon Jul 10 03:16:25 2006
@@ -17,8 +17,6 @@
 
 import java.io.IOException;
 
-import org.apache.harmony.luni.util.NotYetImplementedException;
-
 
 /**
  * This class enables direct access to OS memory.
@@ -539,12 +537,11 @@
 
 	public PlatformAddress mmap(long fileDescriptor, long alignment, long size,
 			int mapMode) throws IOException {
-		throw new NotYetImplementedException();
-//		long address = mmapImpl(fileDescriptor, alignment, size, mapMode);
-//		if (address == -1) {
-//			throw new IOException();
-//		}
-//		return PlatformAddress.on(address, true);
+		long address = mmapImpl(fileDescriptor, alignment, size, mapMode);
+		if (address == -1) {
+			throw new IOException();
+		}
+		return PlatformAddress.on(address, true);
 	}
 
 	private native void unmapImpl(long addr);

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/OSMemory.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/OSMemory.c?rev=420476&r1=420475&r2=420476&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/OSMemory.c
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/OSMemory.c
Mon Jul 10 03:16:25 2006
@@ -161,59 +161,3 @@
 {
   *(jdouble *) ((IDATA) address) = value;
 }
-
-
-/*
- * Class:     org_apache_harmony_luni_platform_OSMemory
- * Method:    unmapImpl
- * Signature: (J)V
- */
-JNIEXPORT void JNICALL Java_org_apache_harmony_luni_platform_OSMemory_unmapImpl
-  (JNIEnv * env, jobject thiz, jlong fd)
-{
-  PORT_ACCESS_FROM_ENV (env);
-  hymmap_unmap_file((void *)fd);
-}
-
-/*
- * Class:     org_apache_harmony_luni_platform_OSMemory
- * Method:    mmapImpl
- * Signature: (JJJI)J
- */
-JNIEXPORT jlong JNICALL Java_org_apache_harmony_luni_platform_OSMemory_mmapImpl
-  (JNIEnv * env, jobject thiz, jlong fd, jlong alignment, jlong size, jint mmode)
-{
-  PORT_ACCESS_FROM_ENV (env);
-  void *mapAddress = NULL;
-  int prot, flags;
-		  
-  // Convert from Java mapping mode to port library mapping mode.
-  switch (mmode)
-    {
-      case org_apache_harmony_luni_platform_IMemorySystem_MMAP_READ_ONLY:
-	prot = PROT_READ;
-	flags = MAP_SHARED;
-        break;
-      case org_apache_harmony_luni_platform_IMemorySystem_MMAP_READ_WRITE:
-	prot = PROT_READ|PROT_WRITE;
-	flags = MAP_SHARED;
-        break;
-      case org_apache_harmony_luni_platform_IMemorySystem_MMAP_WRITE_COPY:
-	prot = PROT_READ|PROT_WRITE;
-	flags = MAP_PRIVATE;
-        break;
-      default:
-        return -1;
-    }
-
-//TODO: how to unmap
- // mapAddress = hymmap_map_filehandler(fd, &mapAddress, mapmode, (IDATA)alignment, (IDATA)size);
-
-   mapAddress = mmap(0,size, prot, flags,fd,alignment);
-  if (mapAddress == NULL)
-    {
-      return -1;
-    }
-  return (jlong) mapAddress;
-}
-

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/OSMemoryLinux32.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/OSMemoryLinux32.c?rev=420476&r1=420475&r2=420476&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/OSMemoryLinux32.c
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/OSMemoryLinux32.c
Mon Jul 10 03:16:25 2006
@@ -23,6 +23,7 @@
 #include <unistd.h>
 #include "vmi.h"
 #include "OSMemory.h"
+#include "IMemorySystem.h"
 
 #define	OS_JNI(func) JNICALL Java_org_apache_harmony_luni_platform_OSMemory_##func
 
@@ -101,3 +102,54 @@
   (JNIEnv * env, jobject thiz, jlong addr, jlong size){
   return msync((void *)addr, size, MS_SYNC);
 };
+
+/*
+ * Class:     org_apache_harmony_luni_platform_OSMemory
+ * Method:    unmapImpl
+ * Signature: (J)V
+ */
+JNIEXPORT void JNICALL Java_org_apache_harmony_luni_platform_OSMemory_unmapImpl
+  (JNIEnv * env, jobject thiz, jlong fd)
+{
+ //FIXME: need buffer size to unmap
+}
+
+/*
+ * Class:     org_apache_harmony_luni_platform_OSMemory
+ * Method:    mmapImpl
+ * Signature: (JJJI)J
+ */
+JNIEXPORT jlong JNICALL Java_org_apache_harmony_luni_platform_OSMemory_mmapImpl
+  (JNIEnv * env, jobject thiz, jlong fd, jlong alignment, jlong size, jint mmode)
+{
+  PORT_ACCESS_FROM_ENV (env);
+  void *mapAddress = NULL;
+  int prot, flags;
+		  
+  // Convert from Java mapping mode to port library mapping mode.
+  switch (mmode)
+    {
+      case org_apache_harmony_luni_platform_IMemorySystem_MMAP_READ_ONLY:
+	prot = PROT_READ;
+	flags = MAP_SHARED;
+        break;
+      case org_apache_harmony_luni_platform_IMemorySystem_MMAP_READ_WRITE:
+	prot = PROT_READ|PROT_WRITE;
+	flags = MAP_SHARED;
+        break;
+      case org_apache_harmony_luni_platform_IMemorySystem_MMAP_WRITE_COPY:
+	prot = PROT_READ|PROT_WRITE;
+	flags = MAP_PRIVATE;
+        break;
+      default:
+        return -1;
+    }
+
+  mapAddress = mmap(0, (size_t)(size&0x7fffffff), prot, flags,fd,(off_t)(alignment&0x7fffffff));
+  if (mapAddress == MAP_FAILED)
+    {
+      return -1;
+    }
+  return (jlong) mapAddress;
+}
+

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSMemory.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSMemory.c?rev=420476&r1=420475&r2=420476&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSMemory.c
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSMemory.c
Mon Jul 10 03:16:25 2006
@@ -180,55 +180,3 @@
 {
   *(jdouble *) address = value;
 }
-
-/*
- * Class:     org_apache_harmony_luni_platform_OSMemory
- * Method:    unmapImpl
- * Signature: (J)V
- */
-JNIEXPORT void JNICALL Java_org_apache_harmony_luni_platform_OSMemory_unmapImpl
-  (JNIEnv * env, jobject thiz, jlong fd)
-{
-  PORT_ACCESS_FROM_ENV (env);
-  hymmap_unmap_file((void *)fd);
-}
-
-/*
- * Class:     org_apache_harmony_luni_platform_OSMemory
- * Method:    mmapImpl
- * Signature: (JJJI)J
- */
-JNIEXPORT jlong JNICALL Java_org_apache_harmony_luni_platform_OSMemory_mmapImpl
-  (JNIEnv * env, jobject thiz, jlong fd, jlong alignment, jlong size, jint mmode)
-{
-  PORT_ACCESS_FROM_ENV (env);
-  void *mapAddress = NULL;
-  I_32 mapmode = 0;
-
-  // Convert from Java mapping mode to port library mapping mode.
-  /* TPE - removed until portlib fixed
-    switch (mmode)
-    {
-      case org_apache_harmony_luni_platform_IMemorySystem_MMAP_READ_ONLY:
-        mapmode = HYPORT_MMAP_CAPABILITY_READ;
-        break;
-      case org_apache_harmony_luni_platform_IMemorySystem_MMAP_READ_WRITE:
-        mapmode = HYPORT_MMAP_CAPABILITY_READ|HYPORT_MMAP_CAPABILITY_WRITE;
-        break;
-      case org_apache_harmony_luni_platform_IMemorySystem_MMAP_WRITE_COPY:
-        mapmode = HYPORT_MMAP_CAPABILITY_COPYONWRITE;
-        break;
-      default:
-        return -1;
-    }
-
-     hymmap_map_filehandler((void *)fd, &mapAddress, mapmode, (IDATA)alignment, (IDATA)size);
-  */
-
-  if (mapAddress == NULL)
-    {
-      return -1;
-    }
-  
-  return (jlong) mapAddress;
-}

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSMemoryWin32.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSMemoryWin32.c?rev=420476&r1=420475&r2=420476&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSMemoryWin32.c
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSMemoryWin32.c
Mon Jul 10 03:16:25 2006
@@ -21,6 +21,7 @@
 #include <string.h>
 #include <windows.h>
 #include "OSMemory.h"
+#include "IMemorySystem.h"
 
 JNIEXPORT jboolean JNICALL Java_org_apache_harmony_luni_platform_OSMemory_isLittleEndianImpl
   (JNIEnv * env, jclass clazz)
@@ -93,3 +94,60 @@
     return (jint)FlushViewOfFile((void *)addr, (SIZE_T)size);
   }
 
+/*
+ * Class:     org_apache_harmony_luni_platform_OSMemory
+ * Method:    unmapImpl
+ * Signature: (J)V
+ */
+JNIEXPORT void JNICALL Java_org_apache_harmony_luni_platform_OSMemory_unmapImpl
+  (JNIEnv * env, jobject thiz, jlong fd)
+{
+  UnmapViewOfFile ((HANDLE)fd);
+}
+
+/*
+ * Class:     org_apache_harmony_luni_platform_OSMemory
+ * Method:    mmapImpl
+ * Signature: (JJJI)J
+ */
+JNIEXPORT jlong JNICALL Java_org_apache_harmony_luni_platform_OSMemory_mmapImpl
+  (JNIEnv * env, jobject thiz, jlong fd, jlong alignment, jlong size, jint mmode)
+{
+  void *mapAddress = NULL;
+  int mapmode = 0;
+  int protect = 0;
+  HANDLE mapping;
+
+  switch (mmode)
+  {
+    case org_apache_harmony_luni_platform_IMemorySystem_MMAP_READ_ONLY:
+      mapmode = FILE_MAP_READ;
+      protect = PAGE_READONLY;
+      break;
+    case org_apache_harmony_luni_platform_IMemorySystem_MMAP_READ_WRITE:
+      mapmode = FILE_MAP_WRITE;
+      protect = PAGE_READWRITE;
+      break;
+    case org_apache_harmony_luni_platform_IMemorySystem_MMAP_WRITE_COPY:
+      mapmode = FILE_MAP_COPY;
+      protect = PAGE_WRITECOPY;
+      break;
+    default:
+      return -1;
+  }
+
+  mapping = CreateFileMapping ((HANDLE)fd, NULL, protect, 0, 0, NULL);
+  if (mapping == NULL)
+    {
+      return -1;
+    }
+  mapAddress = MapViewOfFile (mapping, mapmode, (DWORD)((alignment>>0x20)&0x7fffffff),
(DWORD)(alignment&0xffffffff), (SIZE_T)(size&0xffffffff));
+  CloseHandle (mapping);
+
+  if (mapAddress == NULL)
+    {
+      return -1;
+    }
+  
+  return (jlong) mapAddress;
+}

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileChannelImpl.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileChannelImpl.java?rev=420476&r1=420475&r2=420476&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileChannelImpl.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileChannelImpl.java
Mon Jul 10 03:16:25 2006
@@ -188,6 +188,9 @@
 
     protected final MappedByteBuffer mapImpl(int mapMode, long position,
             long size) throws IOException {
+        if (position + size > size()) {
+            fileSystem.truncate(handle, position + size);
+        }
         long alignment = position - position % NATIVE_PAGE_SIZE;
         int offset = (int) (position - alignment);
         PlatformAddress address = memorySystem.mmap(handle, alignment, size

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/FileChannelTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/FileChannelTest.java?rev=420476&r1=420475&r2=420476&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/FileChannelTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/FileChannelTest.java
Mon Jul 10 03:16:25 2006
@@ -21,8 +21,11 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.RandomAccessFile;
+import java.io.UnsupportedEncodingException;
+import java.nio.BufferOverflowException;
 import java.nio.ByteBuffer;
 import java.nio.MappedByteBuffer;
+import java.nio.ReadOnlyBufferException;
 import java.nio.channels.ClosedChannelException;
 import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
@@ -31,13 +34,14 @@
 import java.nio.channels.OverlappingFileLockException;
 import java.nio.channels.ReadableByteChannel;
 import java.nio.channels.WritableByteChannel;
+import java.nio.channels.FileChannel.MapMode;
 import java.util.Arrays;
 
 import junit.framework.TestCase;
 
 public class FileChannelTest extends TestCase {
 
-    private static final String CONTENT = "MYTESTSTRING";
+    private static final String CONTENT = "it is content of a string for test";
 
     private static final int CONTENT_LENGTH = CONTENT.length();
 
@@ -61,6 +65,16 @@
     private FileInputStream fis;
 
     private FileLock fileLock;
+    
+    private static final byte[] TEST_BYTES;
+
+    static {
+        try {
+            TEST_BYTES = "test".getBytes("iso8859-1");
+        } catch (UnsupportedEncodingException e) {
+            throw new Error(e);
+        }
+    }
 
     protected void setUp() throws Exception {
         fileOfReadOnlyFileChannel = File.createTempFile(
@@ -506,17 +520,17 @@
         int truncateLength = CONTENT_LENGTH + 2;
         assertEquals(readWriteFileChannel, readWriteFileChannel
                 .truncate(truncateLength));
-        assertEquals(CONTENT_LENGTH, fileOfReadWriteFileChannel.length());
+        assertEquals(CONTENT_LENGTH, readWriteFileChannel.size());
 
         truncateLength = CONTENT_LENGTH;
         assertEquals(readWriteFileChannel, readWriteFileChannel
                 .truncate(truncateLength));
-        assertEquals(CONTENT_LENGTH, fileOfReadWriteFileChannel.length());
+        assertEquals(CONTENT_LENGTH, readWriteFileChannel.size());
 
         truncateLength = CONTENT_LENGTH / 2;
         assertEquals(readWriteFileChannel, readWriteFileChannel
                 .truncate(truncateLength));
-        assertEquals(truncateLength, fileOfReadWriteFileChannel.length());
+        assertEquals(truncateLength, readWriteFileChannel.size());
     }
 
     /**
@@ -971,6 +985,249 @@
         // Below assertion fails on RI. RI behaviour is counter to spec.
         assertEquals(10, f.getChannel().position());
     }
+    
+
+    /**
+     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
+     */
+    public void test_map_AbnormalMode() throws IOException {
+        try {
+            writeOnlyFileChannel.map(MapMode.READ_ONLY, 0, CONTENT_LENGTH);
+            fail("should throw NonReadableChannelException.");
+        } catch (NonReadableChannelException ex) {
+            // expected;
+        }
+        try {
+            writeOnlyFileChannel.map(MapMode.READ_WRITE, 0, CONTENT_LENGTH);
+            fail("should throw NonReadableChannelException.");
+        } catch (NonReadableChannelException ex) {
+            // expected;
+        }
+        try {
+            writeOnlyFileChannel.map(MapMode.PRIVATE, 0, CONTENT_LENGTH);
+            fail("should throw NonReadableChannelException.");
+        } catch (NonReadableChannelException ex) {
+            // expected;
+        }
+        writeOnlyFileChannel.close();
+        try {
+            writeOnlyFileChannel.map(MapMode.READ_WRITE, 0, -1);
+            fail("should throw ClosedChannelExeption.");
+        } catch (ClosedChannelException ex) {
+            // expected;
+        }
+
+        try {
+            readOnlyFileChannel.map(MapMode.READ_WRITE, 0, CONTENT_LENGTH);
+            fail("should throw NonWritableChannelException .");
+        } catch (NonWritableChannelException ex) {
+            // expected;
+        }
+        try {
+            readOnlyFileChannel.map(MapMode.PRIVATE, 0, CONTENT_LENGTH);
+            fail("should throw NonWritableChannelException .");
+        } catch (NonWritableChannelException ex) {
+            // expected;
+        }
+        try {
+            readOnlyFileChannel.map(MapMode.READ_WRITE, -1, CONTENT_LENGTH);
+            fail("should throw IAE.");
+        } catch (IllegalArgumentException ex) {
+            // expected;
+        }
+        try {
+            readOnlyFileChannel.map(MapMode.READ_WRITE, 0, -1);
+            fail("should throw IAE.");
+        } catch (IllegalArgumentException ex) {
+            // expected;
+        }
+
+        try {
+            readOnlyFileChannel.map(MapMode.READ_ONLY, 0, CONTENT_LENGTH + 1);
+            fail("should throw IOException.");
+        } catch (IOException ex) {
+            // expected;
+        }
+        try {
+            readOnlyFileChannel.map(MapMode.READ_ONLY, 2, CONTENT_LENGTH - 1);
+            fail("should throw IOException.");
+        } catch (IOException ex) {
+            // expected;
+        }
+
+        readOnlyFileChannel.close();
+        try {
+            readOnlyFileChannel.map(MapMode.READ_WRITE, 0, -1);
+            fail("should throw ClosedChannelExeption.");
+        } catch (ClosedChannelException ex) {
+            // expected;
+        }
+        try {
+            readOnlyFileChannel.map(MapMode.READ_ONLY, 2, CONTENT_LENGTH - 1);
+            fail("should throw IOException.");
+        } catch (IOException ex) {
+            // expected;
+        }
+
+        readWriteFileChannel.close();
+        try {
+            readWriteFileChannel.map(MapMode.READ_WRITE, 0, -1);
+            fail("should throw ClosedChannelExeption.");
+        } catch (ClosedChannelException ex) {
+            // expected;
+        }
+    }
+    
+    /**
+     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
+     */
+    public void test_map_ReadOnly_CloseChannel() throws IOException {
+        // close channel has no effect on map if mapped
+        assertEquals(0, readWriteFileChannel.size());
+        MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.READ_ONLY,
+                0, CONTENT_LENGTH);
+        assertEquals(CONTENT_LENGTH, readWriteFileChannel.size());
+        readOnlyFileChannel.close();
+        assertEquals(CONTENT_LENGTH, mapped.limit());
+    }
+
+    /**
+     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
+     */
+    public void test_map_Private_CloseChannel() throws IOException {
+        MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.PRIVATE, 0,
+                CONTENT_LENGTH);
+        readWriteFileChannel.close();
+        mapped.put(TEST_BYTES);
+        assertEquals(CONTENT_LENGTH, mapped.limit());
+        assertEquals("test".length(), mapped.position());
+    }
+
+    /**
+     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
+     */
+    public void test_map_ReadOnly() throws IOException {
+        MappedByteBuffer mapped = null;
+        // try put something to readonly map
+        writeDataToFile(fileOfReadOnlyFileChannel);
+        mapped = readOnlyFileChannel.map(MapMode.READ_ONLY, 0, CONTENT_LENGTH);
+        try {
+            mapped.put(TEST_BYTES);
+            fail("should throw ReadOnlyBufferException.");
+        } catch (ReadOnlyBufferException ex) {
+            // expected;
+        }
+        assertEquals(CONTENT_LENGTH, mapped.limit());
+        assertEquals(CONTENT_LENGTH, mapped.capacity());
+        assertEquals(0, mapped.position());
+
+        // try to get a readonly map from read/write channel
+        writeDataToFile(fileOfReadWriteFileChannel);
+        mapped = readWriteFileChannel.map(MapMode.READ_ONLY, 0, CONTENT
+                .length());
+        assertEquals(CONTENT_LENGTH, mapped.limit());
+        assertEquals(CONTENT_LENGTH, mapped.capacity());
+        assertEquals(0, mapped.position());
+
+        // map not change channel's position
+        assertEquals(0, readOnlyFileChannel.position());
+        assertEquals(0, readWriteFileChannel.position());
+    }
+
+    /**
+     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
+     */
+    public void test_map_ReadOnly_NonZeroPosition() throws IOException {
+        this.writeDataToFile(fileOfReadOnlyFileChannel);
+        MappedByteBuffer mapped = readOnlyFileChannel.map(MapMode.READ_ONLY,
+                10, CONTENT_LENGTH - 10);
+        assertEquals(CONTENT_LENGTH - 10, mapped.limit());
+        assertEquals(CONTENT_LENGTH - 10, mapped.capacity());
+        assertEquals(0, mapped.position());
+    }
+
+    /**
+     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
+     */
+    public void test_map_Private() throws IOException {
+        this.writeDataToFile(fileOfReadWriteFileChannel);
+        MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.PRIVATE, 0,
+                CONTENT_LENGTH);
+        assertEquals(CONTENT_LENGTH, mapped.limit());
+        // test copy on write if private
+        ByteBuffer returnByPut = mapped.put(TEST_BYTES);
+        assertSame(returnByPut, mapped);
+        ByteBuffer checkBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
+        mapped.force();
+        readWriteFileChannel.read(checkBuffer);
+        assertEquals(CONTENT, new String(checkBuffer.array(), "iso8859-1"));
+
+        // test overflow
+        try {
+            mapped.put(("test" + CONTENT).getBytes("iso8859-1"));
+            fail("should throw BufferOverflowException.");
+        } catch (BufferOverflowException ex) {
+            // expected;
+        }
+    }
+
+    /**
+     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
+     */
+    public void test_map_Private_NonZeroPosition() throws IOException {
+        MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.PRIVATE, 10,
+                CONTENT_LENGTH - 10);
+        assertEquals(CONTENT_LENGTH - 10, mapped.limit());
+        assertEquals(CONTENT_LENGTH - 10, mapped.capacity());
+        assertEquals(0, mapped.position());
+    }
+
+    /**
+     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
+     */
+    public void test_map_ReadWrite() throws IOException {
+        MappedByteBuffer mapped = null;
+        writeDataToFile(fileOfReadWriteFileChannel);
+        mapped = readWriteFileChannel.map(MapMode.READ_WRITE, 0, CONTENT
+                .length());
+
+        // put something will change its channel
+        ByteBuffer returnByPut = mapped.put(TEST_BYTES);
+        assertSame(returnByPut, mapped);
+        String checkString = "test" + CONTENT.substring(4);
+        ByteBuffer checkBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
+        mapped.force();
+        readWriteFileChannel.position(0);
+        readWriteFileChannel.read(checkBuffer);
+        assertEquals(checkString, new String(checkBuffer.array(), "iso8859-1"));
+
+        try {
+            mapped.put(("test" + CONTENT).getBytes("iso8859-1"));
+            fail("should throw BufferOverflowException.");
+        } catch (BufferOverflowException ex) {
+            // expected;
+        }
+    }
+
+    /**
+     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
+     */
+    public void test_map_ReadWrite_NonZeroPosition() throws IOException {
+        // test position non-zero
+        writeDataToFile(fileOfReadWriteFileChannel);
+        MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.READ_WRITE,
+                10, CONTENT_LENGTH - 10);
+        assertEquals(CONTENT_LENGTH - 10, mapped.limit());
+        assertEquals(CONTENT.length() - 10, mapped.capacity());
+        assertEquals(0, mapped.position());
+        mapped.put(TEST_BYTES);
+        ByteBuffer checkBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
+        readWriteFileChannel.read(checkBuffer);
+        String expected = CONTENT.substring(0, 10) + "test"
+                + CONTENT.substring(10 + "test".length());
+        assertEquals(expected, new String(checkBuffer.array(), "iso8859-1"));
+    }
+
 
     private class MockFileChannel extends FileChannel {
         



Mime
View raw message