1 /*
2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package jdk.jfr.event.runtime;
27 
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 /**
34  * Helper class for working with class files and byte arrays
35  */
36 public final class Bytes {
37     public final static byte[] WORLD = Bytes.asBytes("world");
38     public final static byte[] EARTH = Bytes.asBytes("earth");
39 
asBytes(String string)40     public static byte[] asBytes(String string) {
41         byte[] result = new byte[string.length()];
42         for (int i = 0; i < string.length(); i++) {
43             result[i] = (byte)string.charAt(i);
44         }
45         return result;
46     }
47 
classBytes(ClassLoader classLoader, String className)48     public static byte[] classBytes(ClassLoader classLoader, String className) throws IOException {
49         String classFileName = className.replace(".", "/") + ".class";
50         try (InputStream is = classLoader.getResourceAsStream(classFileName)) {
51             if (is == null) {
52                 throw new IOException("Could not find class file " + classFileName);
53             }
54             return is.readAllBytes();
55         }
56     }
57 
classBytes(Class<?> clazz)58     public static byte[] classBytes(Class<?> clazz) throws IOException {
59         return classBytes(clazz.getClassLoader(), clazz.getName());
60     }
61 
replaceAll(byte[] input, byte[] target, byte[] replacement)62     public static byte[] replaceAll(byte[] input, byte[] target, byte[] replacement) {
63         List<Byte> result = new ArrayList<>();
64         for (int i = 0; i < input.length; i++) {
65             if (hasTarget(input, i, target)) {
66                 for (int j = 0; j < replacement.length; j++) {
67                     result.add(replacement[j]);
68                 }
69                 i += target.length - 1;
70             } else {
71                 result.add(input[i]);
72             }
73         }
74         byte[] resultArray = new byte[result.size()];
75         for (int i = 0; i < resultArray.length; i++) {
76             resultArray[i] = result.get(i);
77         }
78         return resultArray;
79     }
80 
hasTarget(byte[] input, int start, byte[] target)81     private static boolean hasTarget(byte[] input, int start, byte[] target) {
82         for (int i = 0; i < target.length; i++) {
83             if (start + i == input.length) {
84                 return false;
85             }
86             if (input[start + i] != target[i]) {
87                 return false;
88             }
89         }
90         return true;
91     }
92 }
93