< prev index next >

src/jdk.jcmd/share/classes/sun/tools/jcmd/JCmd.java

Print this page
rev 55749 : 8227868: jinfo and jstack can fail converting UTF8 output to strings
Reviewed-by:

@@ -23,27 +23,25 @@
  * questions.
  */
 
 package sun.tools.jcmd;
 
-import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
-import java.util.List;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.Comparator;
 import java.net.URISyntaxException;
 
 import com.sun.tools.attach.AttachOperationFailedException;
 import com.sun.tools.attach.VirtualMachine;
 import com.sun.tools.attach.VirtualMachineDescriptor;
 import com.sun.tools.attach.AttachNotSupportedException;
 
 import sun.tools.attach.HotSpotVirtualMachine;
 import sun.tools.common.ProcessArgumentMatcher;
+import sun.tools.common.PrintStreamPrinter;
 import sun.tools.jstat.JStatLogger;
 import sun.jvmstat.monitor.Monitor;
 import sun.jvmstat.monitor.MonitoredHost;
 import sun.jvmstat.monitor.MonitoredVm;
 import sun.jvmstat.monitor.MonitorException;

@@ -120,29 +118,17 @@
         String lines[] = command.split("\\n");
         for (String line : lines) {
             if (line.trim().equals("stop")) {
                 break;
             }
-            try (InputStream in = hvm.executeJCmd(line);
-                 InputStreamReader isr = new InputStreamReader(in, "UTF-8")) {
-                // read to EOF and just print output
-                char c[] = new char[256];
-                int n;
-                boolean messagePrinted = false;
-                do {
-                    n = isr.read(c);
-                    if (n > 0) {
-                        String s = new String(c, 0, n);
-                        System.out.print(s);
-                        messagePrinted = true;
-                    }
-                } while (n > 0);
-                if (!messagePrinted) {
+
+            InputStream is = hvm.executeJCmd(line);
+
+            if (PrintStreamPrinter.drainUTF8(is, System.out) == 0) {
                     System.out.println("Command executed successfully");
                 }
             }
-        }
         vm.detach();
     }
 
     private static void listCounters(String pid) {
         // Code from JStat (can't call it directly since it does System.exit)
< prev index next >