diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/java/android/os/MemoryFile.java | 7 | ||||
-rw-r--r-- | core/tests/coretests/src/android/os/MemoryFileTest.java | 23 |
2 files changed, 24 insertions, 6 deletions
diff --git a/core/java/android/os/MemoryFile.java b/core/java/android/os/MemoryFile.java index f82702a..e8148f7 100644 --- a/core/java/android/os/MemoryFile.java +++ b/core/java/android/os/MemoryFile.java @@ -28,7 +28,7 @@ import java.io.OutputStream; * MemoryFile is a wrapper for the Linux ashmem driver. * MemoryFiles are backed by shared memory, which can be optionally * set to be purgeable. - * Purgeable files may have their contents reclaimed by the kernel + * Purgeable files may have their contents reclaimed by the kernel * in low memory conditions (only if allowPurging is set to true). * After a file is purged, attempts to read or write the file will * cause an IOException to be thrown. @@ -126,7 +126,7 @@ public class MemoryFile close(); } } - + /** * Returns the length of the memory file. * @@ -190,7 +190,7 @@ public class MemoryFile * @return number of bytes read. * @throws IOException if the memory file has been purged or deactivated. */ - public int readBytes(byte[] buffer, int srcOffset, int destOffset, int count) + public int readBytes(byte[] buffer, int srcOffset, int destOffset, int count) throws IOException { if (isDeactivated()) { throw new IOException("Can't read from deactivated memory file."); @@ -330,6 +330,7 @@ public class MemoryFile @Override public void write(byte buffer[], int offset, int count) throws IOException { writeBytes(buffer, offset, mOffset, count); + mOffset += count; } @Override diff --git a/core/tests/coretests/src/android/os/MemoryFileTest.java b/core/tests/coretests/src/android/os/MemoryFileTest.java index e627bb4..82af662 100644 --- a/core/tests/coretests/src/android/os/MemoryFileTest.java +++ b/core/tests/coretests/src/android/os/MemoryFileTest.java @@ -20,13 +20,11 @@ import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.LargeTest; import android.test.suitebuilder.annotation.SmallTest; -import java.io.File; -import java.io.FileDescriptor; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class MemoryFileTest extends AndroidTestCase { @@ -101,6 +99,25 @@ public class MemoryFileTest extends AndroidTestCase { file.close(); } + // http://code.google.com/p/android/issues/detail?id=11415 + public void testOutputStreamAdvances() throws IOException { + MemoryFile file = new MemoryFile("MemoryFileTest", 10); + + OutputStream os = file.getOutputStream(); + os.write(new byte[] { 1, 2, 3, 4, 5 }); + os.write(new byte[] { -1, -1, 6, 7, 8, -1 }, 2, 3); + os.write(9); + try { + os.write(new byte[] { -1, -1 }); + fail(); + } catch (IndexOutOfBoundsException expected) { + } + + byte[] copy = new byte[file.length()]; + file.readBytes(copy, 0, 0, file.length()); + assertEquals("[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]", Arrays.toString(copy)); + } + // Tests for the IndexOutOfBoundsException cases in read(). private void readIndexOutOfBoundsException(int offset, int count, String msg) |