< prev index next >

src/java.base/share/classes/jdk/internal/vm/VMSupport.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -22,17 +22,16 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 package jdk.internal.vm;
 
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.util.Properties;
-import java.util.Set;
 import java.util.jar.JarFile;
 import java.util.jar.Manifest;
 import java.util.jar.Attributes;
+import java.util.*;
 
 /*
  * Support class used by JVMTI and VM attach mechanism.
  */
 public class VMSupport {

@@ -48,29 +47,41 @@
         }
         return agentProps;
     }
     private static native Properties initAgentProperties(Properties props);
 
+    private static String convertToBytesStr(String str, boolean escapeSpace) throws Exception {
+        str = str.replace("\n", "\\n")
+                 .replace("\r", "\\r")
+                 .replace("\t", "\\t")
+                 .replace("\f", "\\f");
+        if (escapeSpace) {
+            str = str.replace(" ", "\\ ");
+        }
+
+        String result = "";
+        for (char ch : str.toCharArray()) {
+            result += ((ch < 0x20) || (ch > 0x7e)) ? String.format("\\u%04X", (int)ch)
+                                                   : ch;
+        }
+
+        return result;
+    }
+
     /**
      * Write the given properties list to a byte array and return it. Properties with
-     * a key or value that is not a String is filtered out. The stream written to the byte
-     * array is ISO 8859-1 encoded.
+     * a key or value that is not a String is filtered out.
      */
     private static byte[] serializePropertiesToByteArray(Properties p) throws IOException {
-        ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
-
-        Properties props = new Properties();
-
-        // stringPropertyNames() returns a snapshot of the property keys
-        Set<String> keyset = p.stringPropertyNames();
-        for (String key : keyset) {
-            String value = p.getProperty(key);
-            props.put(key, value);
+        var joiner = new StringJoiner("\n");
+        joiner.add((new Date()).toString());
+        for (var entry : p.entrySet()) {
+            var key = convertToBytes(entry.getKey().toString(), true);
+            var value = convertToBytes(entry.getValue().toString(), false);
+            joiner.add(key + "=" + value);
         }
-
-        props.store(out, null);
-        return out.toByteArray();
+        return joiner.toString().getBytes();
     }
 
     public static byte[] serializePropertiesToByteArray() throws IOException {
         return serializePropertiesToByteArray(System.getProperties());
     }
< prev index next >