Path: blob/master/test/hotspot/jtreg/testlibrary/jvmti/TransformUtil.java
40942 views
/*1* Copyright (c) 2016, 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*/222324public class TransformUtil {25public static final String BeforePattern = "this-should-be-transformed";26public static final String AfterPattern = "this-has-been--transformed";27public static final String ParentCheckPattern = "parent-transform-check: ";28public static final String ChildCheckPattern = "child-transform-check: ";2930/**31* @return the number of occurrences of the <code>from</code> string that32* have been replaced.33*/34public static int replace(byte buff[], String from, String to) {35if (to.length() != from.length()) {36throw new RuntimeException("bad strings");37}38byte f[] = asciibytes(from);39byte t[] = asciibytes(to);40byte f0 = f[0];4142int numReplaced = 0;43int max = buff.length - f.length;44for (int i = 0; i < max; ) {45if (buff[i] == f0 && replace(buff, f, t, i)) {46i += f.length;47numReplaced++;48} else {49i++;50}51}52return numReplaced;53}5455public static boolean replace(byte buff[], byte f[], byte t[], int i) {56for (int x = 0; x < f.length; x++) {57if (buff[x+i] != f[x]) {58return false;59}60}61for (int x = 0; x < f.length; x++) {62buff[x+i] = t[x];63}64return true;65}6667static byte[] asciibytes(String s) {68byte b[] = new byte[s.length()];69for (int i = 0; i < b.length; i++) {70b[i] = (byte)s.charAt(i);71}72return b;73}74}757677