diff options
author | Jesse Wilson <jessewilson@google.com> | 2009-12-17 17:38:33 -0800 |
---|---|---|
committer | Jesse Wilson <jessewilson@google.com> | 2009-12-18 13:41:15 -0800 |
commit | f41de2a4a1c22e3f3ee9a8cd65ec7997c9587cdb (patch) | |
tree | a605f9ec166dc4665bc02065c5631a8920dcf5db /tests | |
parent | ff3e4c834ff62620398c830a02002cb762c77c53 (diff) | |
download | frameworks_base-f41de2a4a1c22e3f3ee9a8cd65ec7997c9587cdb.zip frameworks_base-f41de2a4a1c22e3f3ee9a8cd65ec7997c9587cdb.tar.gz frameworks_base-f41de2a4a1c22e3f3ee9a8cd65ec7997c9587cdb.tar.bz2 |
Adding support for LoggingPrintStream.write(byte[]) and friends.
By default, Android's System.out and System.err are implemented by
the AndroidPrintStream subclass of LoggingPrintStream. Until now,
that class has silently discarded the raw bytes it has received.
This causes two problems:
Applications may be accidentally wasting CPU+memory writing to
System.out. By making this output visible, the developers of such
applications can silence the problem at the source.
Application developers may be purposefully writing to these streams
and perplexed by the data's disappearance. For example, the core
library's own java.util.logging.ConsoleHandler sends its log data
into this black hole. By making the data visible, we save the data
and remove an unnecessary sharp edge from our API.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/framework-tests/src/com/android/internal/os/LoggingPrintStreamTest.java | 61 |
1 files changed, 56 insertions, 5 deletions
diff --git a/tests/framework-tests/src/com/android/internal/os/LoggingPrintStreamTest.java b/tests/framework-tests/src/com/android/internal/os/LoggingPrintStreamTest.java index 8e3a034..4d016d1 100644 --- a/tests/framework-tests/src/com/android/internal/os/LoggingPrintStreamTest.java +++ b/tests/framework-tests/src/com/android/internal/os/LoggingPrintStreamTest.java @@ -18,12 +18,12 @@ package com.android.internal.os; import junit.framework.TestCase; -import java.util.Arrays; -import java.util.List; +import java.io.PrintWriter; +import java.io.StringWriter; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; -import java.io.StringWriter; -import java.io.PrintWriter; +import java.util.List; public class LoggingPrintStreamTest extends TestCase { @@ -121,6 +121,58 @@ public class LoggingPrintStreamTest extends TestCase { assertEquals(Arrays.asList("Foo", "4", "a"), out.lines); } + public void testMultiByteCharactersSpanningBuffers() throws Exception { + // assume 3*1000 bytes won't fit in LoggingPrintStream's internal buffer + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < 1000; i++) { + builder.append("\u20AC"); // a Euro character; 3 bytes in UTF-8 + } + String expected = builder.toString(); + + out.write(expected.getBytes("UTF-8")); + out.flush(); + assertEquals(Arrays.asList(expected), out.lines); + } + + public void testWriteOneByteAtATimeMultibyteCharacters() throws Exception { + String expected = " \u20AC \u20AC \u20AC \u20AC "; + for (byte b : expected.getBytes()) { + out.write(b); + } + out.flush(); + assertEquals(Arrays.asList(expected), out.lines); + } + + public void testWriteByteArrayAtATimeMultibyteCharacters() throws Exception { + String expected = " \u20AC \u20AC \u20AC \u20AC "; + out.write(expected.getBytes()); + out.flush(); + assertEquals(Arrays.asList(expected), out.lines); + } + + public void testWriteWithOffsetsMultibyteCharacters() throws Exception { + String expected = " \u20AC \u20AC \u20AC \u20AC "; + byte[] bytes = expected.getBytes(); + int i = 0; + while (i < bytes.length - 5) { + out.write(bytes, i, 5); + i += 5; + } + out.write(bytes, i, bytes.length - i); + out.flush(); + assertEquals(Arrays.asList(expected), out.lines); + } + + public void testWriteFlushesOnNewlines() throws Exception { + String a = " \u20AC \u20AC "; + String b = " \u20AC \u20AC "; + String c = " "; + String toWrite = a + "\n" + b + "\n" + c; + out.write(toWrite.getBytes()); + out.flush(); + assertEquals(Arrays.asList(a, b, c), out.lines); + } + static class TestPrintStream extends LoggingPrintStream { final List<String> lines = new ArrayList<String>(); @@ -129,5 +181,4 @@ public class LoggingPrintStreamTest extends TestCase { lines.add(line); } } - } |