< 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,18 @@
* 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.*;
+import java.nio.*;
+import java.nio.charset.*;
/*
* Support class used by JVMTI and VM attach mechanism.
*/
public class VMSupport {
@@ -48,29 +49,47 @@
}
return agentProps;
}
private static native Properties initAgentProperties(Properties props);
+ private static String convertToBytesStr(String str, boolean escapeSpace) throws Exception {
+ if (escapeSpace) {
+ str = str.replace(" ", "\\ ");
+ }
+
+ var charBuf = CharBuffer.wrap(str);
+ var byteBuf = ByteBuffer.allocate(charBuf.length() * 5); // 1 unicode char might have 4 digits + 2 ('\u')
+ var encoder = StandardCharsets.ISO_8859_1
+ .newEncoder()
+ .onUnmappableCharacter(CodingErrorAction.REPORT);
+ CoderResult result;
+ do {
+ result = encoder.encode(charBuf, byteBuf, false);
+ if (result.isUnmappable()) {
+ byteBuf.put(String.format("\\u%04X", (int)charBuf.get()).getBytes());
+ } else if (result.isError()) {
+ result.throwException();
+ }
+ } while (result.isError());
+
+ return new String(byteBuf.array());
+ }
+
/**
* 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.
*/
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 >