Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/utilities/CStringUtilities.java
41161 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.utilities;2526import java.io.*;27import java.nio.charset.Charset;28import java.util.*;2930import sun.jvm.hotspot.debugger.*;3132/** A utility class encapsulating useful operations on C strings33represented as Addresses */3435public class CStringUtilities {36/** Return the length of a null-terminated ASCII string in the37remote process */38public static int getStringLength(Address addr) {39int i = 0;40while (addr.getCIntegerAt(i, 1, false) != 0) {41i++;42}43return i;44}4546private static String encoding = System.getProperty("file.encoding", "US-ASCII");4748public static String getString(Address addr) {49return getString(addr, Charset.forName(encoding));50}5152/** Fetch a null-terminated ASCII string from the remote process.53Returns null if the argument is null, otherwise returns a54non-null string (for example, returns an empty string if the55first character fetched is the null terminator). */56public static String getString(Address addr, Charset charset) {57if (addr == null) {58return null;59}6061List<Byte> data = new ArrayList<>();62byte val = 0;63long i = 0;64do {65val = (byte) addr.getCIntegerAt(i, 1, false);66if (val != 0) {67data.add(val);68}69++i;70} while (val != 0);7172// Convert to byte[] and from there to String73byte[] bytes = new byte[data.size()];74for (i = 0; i < data.size(); ++i) {75bytes[(int) i] = data.get((int) i).byteValue();76}77// FIXME: When we switch to use JDK 6 to build SA,78// we can change the following to just return:79// return new String(bytes, Charset.defaultCharset());80return new String(bytes, charset);81}82}838485