1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * The contents of this file are subject to the terms of either the Universal Permissive License 7 * v 1.0 as shown at http://oss.oracle.com/licenses/upl 8 * 9 * or the following license: 10 * 11 * Redistribution and use in source and binary forms, with or without modification, are permitted 12 * provided that the following conditions are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions 15 * and the following disclaimer. 16 * 17 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of 18 * conditions and the following disclaimer in the documentation and/or other materials provided with 19 * the distribution. 20 * 21 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to 22 * endorse or promote products derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 26 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 31 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 package org.openjdk.jmc.agent.test.util; 34 35 import java.io.BufferedReader; 36 import java.io.ByteArrayInputStream; 37 import java.io.Closeable; 38 import java.io.EOFException; 39 import java.io.IOException; 40 import java.io.InputStream; 41 import java.io.InputStreamReader; 42 import java.util.Arrays; 43 import java.util.Random; 44 45 import org.openjdk.jmc.agent.test.InstrumentMe; 46 47 public final class TestToolkit { 48 public static final Random RND = new Random(); 49 50 private TestToolkit() { 51 throw new UnsupportedOperationException("Not to be instantiated."); //$NON-NLS-1$ 52 } 53 54 public static byte[] getByteCode(Class<?> c) throws IOException { 55 InputStream is = c.getClassLoader().getResourceAsStream(c.getName().replace('.', '/') + ".class"); //$NON-NLS-1$ 56 return readFully(is, -1, true); 57 } 58 59 public static byte[] readFully(InputStream is, int length, boolean readAll) throws IOException { 60 byte[] output = {}; 61 if (length == -1) { 62 length = Integer.MAX_VALUE; 63 } 64 int pos = 0; 65 while (pos < length) { 66 int bytesToRead; 67 if (pos >= output.length) { // Only expand when there's no room 68 bytesToRead = Math.min(length - pos, output.length + 1024); 69 if (output.length < pos + bytesToRead) { 70 output = Arrays.copyOf(output, pos + bytesToRead); 71 } 72 } else { 73 bytesToRead = output.length - pos; 74 } 75 int cc = is.read(output, pos, bytesToRead); 76 if (cc < 0) { 77 if (readAll && length != Integer.MAX_VALUE) { 78 throw new EOFException("Detect premature EOF"); //$NON-NLS-1$ 79 } else { 80 if (output.length != pos) { 81 output = Arrays.copyOf(output, pos); 82 } 83 break; 84 } 85 } 86 pos += cc; 87 } 88 return output; 89 } 90 91 public static long randomLong() { 92 return RND.nextLong(); 93 } 94 95 public static String randomString() { 96 StringBuilder builder = new StringBuilder(); 97 for (int i = 0; i < RND.nextInt(10) + 1; i++) { 98 builder.append(Character.toString((char) (RND.nextInt(26) + 64))); 99 } 100 return builder.toString(); 101 } 102 103 public static InputStream getProbesXML(String testName) { 104 try { 105 String s = readTemplate(); 106 s = s.replaceAll("%TEST_NAME%", testName); //$NON-NLS-1$ 107 return new ByteArrayInputStream(s.getBytes()); 108 109 } catch (IOException e) { 110 e.printStackTrace(); 111 } 112 return null; 113 } 114 115 private static String readTemplate() throws IOException { 116 InputStream inputStream = InstrumentMe.class.getResourceAsStream("jfrprobes_template.xml"); //$NON-NLS-1$ 117 String s = readString(inputStream); 118 closeSilently(inputStream); 119 return s; 120 } 121 122 public static String readString(InputStream in) throws IOException { 123 return readString(new BufferedReader(new InputStreamReader(in), 8192)); 124 } 125 126 public static String readString(InputStream in, String charsetName) throws IOException { 127 return readString(new BufferedReader(new InputStreamReader(in, charsetName), 8192)); 128 } 129 130 private static String readString(BufferedReader reader) throws IOException { 131 String s; 132 StringBuilder builder = new StringBuilder(); 133 try { 134 while ((s = reader.readLine()) != null) { 135 builder.append(s + "\r"); //$NON-NLS-1$ 136 } 137 s = builder.toString(); 138 } finally { 139 closeSilently(reader); 140 } 141 return s; 142 } 143 144 private static void closeSilently(Closeable closeable) { 145 if (closeable != null) { 146 try { 147 closeable.close(); 148 } catch (Throwable t) { 149 150 } 151 } 152 } 153 }