iuliana commented on a change in pull request #1301:
URL: https://github.com/apache/brooklyn-server/pull/1301#discussion_r809922289



##########
File path: 
utils/common/src/main/java/org/apache/brooklyn/util/stream/StreamGobbler.java
##########
@@ -23,17 +23,20 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintStream;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.slf4j.Logger;
 
 public class StreamGobbler extends Thread implements Closeable {
-    
+
+    private static char REPLACEMENT_CHARACTER = 0xfffd;

Review comment:
       @algairim Make this final.

##########
File path: 
utils/common/src/main/java/org/apache/brooklyn/util/stream/StreamGobbler.java
##########
@@ -60,50 +63,84 @@ public void shutdown() {
 
     String logPrefix = "";
     String printPrefix = "";
+
     public StreamGobbler setPrefix(String prefix) {
         setLogPrefix(prefix);
         setPrintPrefix(prefix);
         return this;
     }
+
     public StreamGobbler setPrintPrefix(String prefix) {
         printPrefix = prefix;
         return this;
     }
+
     public StreamGobbler setLogPrefix(String prefix) {
         logPrefix = prefix;
         return this;
-    }    
-    
+    }
+
     @Override
     public void run() {
-        int c = -1;
+        int c, bytes = 0;
+        char[] utfSymbol = new char[2];
         try {
-            while (running.get() && (c=stream.read())>=0) {
-                onChar(c);
+            ByteBuffer bb = ByteBuffer.allocate(4);
+            while (running.get() && (c = stream.read()) >= 0) {
+
+                if (bytes == 0) {
+                    // Identify utf symbol size by Unicode page.
+                    if (c >= 0xF0) {
+                        bytes = 4;
+                    } else if (c >= 0xE0) {
+                        bytes = 3;
+                    } else if (c >= 0xC2) {
+                        bytes = 2;
+                    } else {
+                        bytes = 1;
+                    }
+                }
+
+                //System.out.println(" [" + bb.position() + "] " + 
Integer.toHexString(c)); // for DEBUG
+                bb.put((byte) c);
+                bytes--;
+
+                if (bytes == 0) {
+                    bb.rewind();
+                    StandardCharsets.UTF_8.decode(bb).get(utfSymbol);
+                    bb.clear();
+                    //System.out.println("[ch] 0x" + 
Integer.toHexString(utfSymbol[0]) + " 0x" + Integer.toHexString(utfSymbol[1])); 
// for DEBUG

Review comment:
       @algairim  If this is useful put it in a trace log statement.

##########
File path: 
utils/common/src/main/java/org/apache/brooklyn/util/stream/StreamGobbler.java
##########
@@ -60,50 +63,84 @@ public void shutdown() {
 
     String logPrefix = "";
     String printPrefix = "";
+
     public StreamGobbler setPrefix(String prefix) {
         setLogPrefix(prefix);
         setPrintPrefix(prefix);
         return this;
     }
+
     public StreamGobbler setPrintPrefix(String prefix) {
         printPrefix = prefix;
         return this;
     }
+
     public StreamGobbler setLogPrefix(String prefix) {
         logPrefix = prefix;
         return this;
-    }    
-    
+    }
+
     @Override
     public void run() {
-        int c = -1;
+        int c, bytes = 0;
+        char[] utfSymbol = new char[2];
         try {
-            while (running.get() && (c=stream.read())>=0) {
-                onChar(c);
+            ByteBuffer bb = ByteBuffer.allocate(4);
+            while (running.get() && (c = stream.read()) >= 0) {
+
+                if (bytes == 0) {
+                    // Identify utf symbol size by Unicode page.
+                    if (c >= 0xF0) {
+                        bytes = 4;
+                    } else if (c >= 0xE0) {
+                        bytes = 3;
+                    } else if (c >= 0xC2) {
+                        bytes = 2;
+                    } else {
+                        bytes = 1;
+                    }
+                }
+
+                //System.out.println(" [" + bb.position() + "] " + 
Integer.toHexString(c)); // for DEBUG

Review comment:
       @algairim  If this is useful put it in a trace log statement.

##########
File path: 
utils/common/src/test/java/org/apache/brooklyn/util/stream/StreamGobblerTest.java
##########
@@ -29,12 +29,65 @@
 
 import org.apache.brooklyn.test.Asserts;
 import org.apache.brooklyn.util.os.Os;
-import org.apache.brooklyn.util.stream.StreamGobbler;
+import org.apache.brooklyn.util.text.Strings;
+import org.apache.commons.lang3.RandomStringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.Assert;
 import org.testng.annotations.Test;
 
 public class StreamGobblerTest {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(StreamGobblerTest.class);
+
     private String NL = Os.LINE_SEPARATOR;

Review comment:
       @algairim Make this final.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to