/hg/icedtea-web: Fixed TeeOutputStream to accept multi-byte enco...
jkang at icedtea.classpath.org
jkang at icedtea.classpath.org
Mon Jul 28 16:36:36 UTC 2014
changeset 6a985d697bf1 in /hg/icedtea-web
details: http://icedtea.classpath.org/hg/icedtea-web?cmd=changeset;node=6a985d697bf1
author: Jie Kang <jkang at redhat.com>
date: Mon Jul 28 12:36:26 2014 -0400
Fixed TeeOutputStream to accept multi-byte encodings.
2014-07-28 Jie Kang <jkang at redhat.com>
Fixed TeeOutputStream to accept multi-byte encodings.
* netx/net/sourceforge/jnlp/util/logging/TeeOutputStream.java: Now uses
ByteArrayOutputStream instead of StringBuffer
* tests/netx/unit/net/sourceforge/jnlp/util/logging/TeeOutputStreamTest.java:
diffstat:
ChangeLog | 14 +-
netx/net/sourceforge/jnlp/util/logging/TeeOutputStream.java | 59 ++++----
tests/netx/unit/net/sourceforge/jnlp/util/logging/TeeOutputStreamTest.java | 67 ++++++++++
3 files changed, 109 insertions(+), 31 deletions(-)
diffs (226 lines):
diff -r 89083ad99374 -r 6a985d697bf1 ChangeLog
--- a/ChangeLog Mon Jul 28 11:55:52 2014 -0400
+++ b/ChangeLog Mon Jul 28 12:36:26 2014 -0400
@@ -1,4 +1,10 @@
-2014-06-20 Jie Kang <jkang at redhat.com>
+2014-07-28 Jie Kang <jkang at redhat.com>
+ Fixed TeeOutputStream to accept multi-byte encodings.
+ * netx/net/sourceforge/jnlp/util/logging/TeeOutputStream.java: Now uses
+ ByteArrayOutputStream instead of StringBuffer
+ * tests/netx/unit/net/sourceforge/jnlp/util/logging/TeeOutputStreamTest.java:
+
+2014-07-28 Jie Kang <jkang at redhat.com>
Made adjustments to the MethodOverloadResolver to follow LiveConnect
specification when dealing with superclasses.
@@ -12,15 +18,15 @@
Fix to Java ConsoleOutputPane for lower resolutions. Addresses bug
PR1859 where part of the pane is hidden and unnaccessible when
clicking "Show Details".
- * netx/net/sourceforge/jnlp/util/logging/ConsoleOutputPane.java
+ * netx/net/sourceforge/jnlp/util/logging/ConsoleOutputPane.java:
2014-07-28 Jie Kang <jkang at redhat.com>
Fix to Control Panel UI for lower resolutions. Addresses bug
PR1856 where part of the dialog is hidden and unaccessible
on lower resolutions such as 800 x 600.
- * netx/net/sourceforge/jnlp/controlpanel/ControlPanel.java
- * netx/net/sourceforge/jnlp/controlpanel/UnsignedAppletsTrustingListPanel.java
+ * netx/net/sourceforge/jnlp/controlpanel/ControlPanel.java:
+ * netx/net/sourceforge/jnlp/controlpanel/UnsignedAppletsTrustingListPanel.java:
2014-07-22 Fridrich Strba <fridrich.strba at suse.com>
diff -r 89083ad99374 -r 6a985d697bf1 netx/net/sourceforge/jnlp/util/logging/TeeOutputStream.java
--- a/netx/net/sourceforge/jnlp/util/logging/TeeOutputStream.java Mon Jul 28 11:55:52 2014 -0400
+++ b/netx/net/sourceforge/jnlp/util/logging/TeeOutputStream.java Mon Jul 28 12:36:26 2014 -0400
@@ -38,14 +38,13 @@
package net.sourceforge.jnlp.util.logging;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.io.PrintStream;
-import net.sourceforge.jnlp.util.logging.JavaConsole;
-import net.sourceforge.jnlp.util.logging.OutputController;
+
import net.sourceforge.jnlp.util.logging.OutputController.Level;
-import net.sourceforge.jnlp.util.logging.SingleStreamLogger;
import net.sourceforge.jnlp.util.logging.headers.Header;
import net.sourceforge.jnlp.util.logging.headers.JavaMessage;
-import net.sourceforge.jnlp.util.logging.headers.MessageWithHeader;
/**
* Behaves like the 'tee' command, sends output to both actual std stream and a
@@ -55,15 +54,15 @@
// Everthing written to TeeOutputStream is written to our log too
- private final StringBuffer string = new StringBuffer();
+ private final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
private final boolean isError;
+ private final String lineSeparator = System.getProperty("line.separator");
public TeeOutputStream(PrintStream stdStream, boolean isError) {
super(stdStream);
this.isError = isError;
}
-
@Override
public void close() {
flushLog();
@@ -82,32 +81,25 @@
@Override
public synchronized void write(byte[] b, int off, int len) {
- if (b == null) {
- throw new NullPointerException();
- } else if ((off < 0) || (off > b.length) || (len < 0)
- || ((off + len) > b.length) || ((off + len) < 0)) {
- throw new IndexOutOfBoundsException();
- } else if (len == 0) {
+ if (len == 0) {
return;
}
- for (int i = 0; i < len; i++) {
- appendChar(b[off + i]);
- }
+ appendByteArray(b, off, len);
super.write(b, off, len);
}
@Override
public synchronized void write(int b) {
- appendChar(b);
+ appendByte(b);
super.write(b);
}
private void flushLog() {
- if (string.length() <= 0 ){
- return;
+ String s = byteArrayOutputStream.toString();
+ if (s.length() > 0) {
+ log(s);
+ byteArrayOutputStream.reset();
}
- log(string.toString());
- string.setLength(0);
}
@Override
@@ -121,21 +113,34 @@
return isError;
}
- private void appendChar(int b) {
- if ( b <= 0 || b == '\n'){
+ private void appendByte(int b) {
+ byteArrayOutputStream.write(b);
+ String s = byteArrayOutputStream.toString();
+ if (s.endsWith(lineSeparator)) {
flushLog();
- } else {
- string.append((char)b);
+ }
+ }
+
+ private void appendByteArray(byte[] b, int off, int len) {
+ byteArrayOutputStream.write(b, off, len);
+ String s = new String(b, off, len);
+ if (s.endsWith(lineSeparator)) {
+ flushLog();
}
}
private Level getlevel() {
- if (isError()){
+ if (isError()) {
return OutputController.Level.ERROR_ALL;
} else {
return OutputController.Level.MESSAGE_ALL;
}
}
-
-
+
+ //For unit testing
+ protected ByteArrayOutputStream getByteArrayOutputStream() throws IOException {
+ ByteArrayOutputStream copy = new ByteArrayOutputStream();
+ copy.write(this.byteArrayOutputStream.toByteArray());
+ return copy;
+ }
}
diff -r 89083ad99374 -r 6a985d697bf1 tests/netx/unit/net/sourceforge/jnlp/util/logging/TeeOutputStreamTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/netx/unit/net/sourceforge/jnlp/util/logging/TeeOutputStreamTest.java Mon Jul 28 12:36:26 2014 -0400
@@ -0,0 +1,67 @@
+package net.sourceforge.jnlp.util.logging;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+
+import static org.junit.Assert.assertTrue;
+
+public class TeeOutputStreamTest {
+
+ private PrintStream teePrintStream;
+ private TeeOutputStream tos;
+
+
+ @Before
+ public void setup() {
+ teePrintStream = new PrintStream(new ByteArrayOutputStream(), true);
+ tos = new TeeOutputStream(teePrintStream, false);
+ }
+ @Test
+ public void testPrintLn() throws IOException {
+ String s = "Helä½ å¥½lo \n World!";
+ tos.println(s); //println should be immediately flushed
+ assertTrue(tos.getByteArrayOutputStream().toString().isEmpty());
+ }
+
+ @Test
+ public void testPrint() throws IOException {
+ String s = "नमसà¥à¤¤Hello!\r";
+ tos.print(s);
+ assertTrue(tos.getByteArrayOutputStream().toString().equals(s));
+ }
+
+ @Test
+ public void testWriteByteArrayString() throws IOException {
+ String s = "He\n\n\\llo chaÍo";
+ tos.write(s.getBytes(), 0, s.getBytes().length);
+ assertTrue(tos.getByteArrayOutputStream().toString().equals(s.toString()));
+ }
+ @Test
+ public void testWriteByte() throws IOException {
+ byte b = 5;
+ tos.write(b);
+ assertTrue(byteArrayEquals(b, tos.getByteArrayOutputStream().toByteArray()));
+ }
+
+ @Test
+ public void testFlush() throws IOException {
+ String s = "Hello";
+ tos.print(s);
+ assertTrue(!tos.getByteArrayOutputStream().toString().isEmpty());
+ tos.flush();
+ assertTrue(tos.getByteArrayOutputStream().toString().isEmpty());
+ }
+
+ private boolean byteArrayEquals(byte b, byte[] arr) {
+ for (byte i : arr) {
+ if (b != i) {
+ return false;
+ }
+ }
+ return true;
+ }
+}
More information about the distro-pkg-dev
mailing list