1/* 2 * Copyright (c) 2003, 2010, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 4894899 27 * @summary Test various cases of passing java.nio.ByteBuffers 28 * to defineClass(). 29 * 30 * @build DefineClassByteBuffer TestClass 31 * @run main DefineClassByteBuffer 32 */ 33 34import java.nio.*; 35import java.nio.channels.*; 36import java.io.*; 37 38public class DefineClassByteBuffer { 39 40 static void test(ClassLoader cl) throws Exception { 41 Class<?> c = Class.forName("TestClass", true, cl); 42 if (!"TestClass".equals(c.getName())) { 43 throw new RuntimeException("Got wrong class: " + c); 44 } 45 if (c.getClassLoader() != cl) { 46 throw new RuntimeException("TestClass defined by wrong classloader: " + c.getClassLoader()); 47 } 48 } 49 50 public static void main(String arg[]) throws Exception { 51 DummyClassLoader[] cls = new DummyClassLoader[DummyClassLoader.MAX_TYPE]; 52 for (int i = 0; i < cls.length; i++) { 53 cls[i] = new DummyClassLoader(i); 54 } 55 56 /* Create several instances of the class using different classloaders, 57 which are using different types of ByteBuffer. */ 58 for (int i = 0; i < cls.length; i++) { 59 test(cls[i]); 60 } 61 } 62 63 /** Always loads the same class, using various types of ByteBuffers */ 64 public static class DummyClassLoader extends ClassLoader { 65 66 public static final String CLASS_NAME = "TestClass"; 67 68 public static final int MAPPED_BUFFER = 0; 69 public static final int DIRECT_BUFFER = 1; 70 public static final int ARRAY_BUFFER = 2; 71 public static final int WRAPPED_BUFFER = 3; 72 public static final int READ_ONLY_ARRAY_BUFFER = 4; 73 public static final int READ_ONLY_DIRECT_BUFFER = 5; 74 public static final int DUP_ARRAY_BUFFER = 6; 75 public static final int DUP_DIRECT_BUFFER = 7; 76 public static final int MAX_TYPE = 7; 77 78 int loaderType; 79 80 DummyClassLoader(int loaderType) { 81 this.loaderType = loaderType; 82 } 83 84 static ByteBuffer[] buffers = new ByteBuffer[MAX_TYPE + 1]; 85 86 static ByteBuffer readClassFile(String name) { 87 try { 88 File f = new File(System.getProperty("test.classes", "."), 89 name); 90 FileInputStream fin = new FileInputStream(f); 91 FileChannel fc = fin.getChannel(); 92 return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); 93 } catch (FileNotFoundException e) { 94 throw new RuntimeException("Can't open file: " + name, e); 95 } catch (IOException e) { 96 throw new RuntimeException("Can't open file: " + name, e); 97 } 98 } 99 100 static { 101 /* create a bunch of different ByteBuffers, starting with a mapped 102 buffer from a class file, and create various duplicate and wrapped 103 buffers. */ 104 buffers[MAPPED_BUFFER] = readClassFile(CLASS_NAME + ".class"); 105 byte[] array = new byte[buffers[MAPPED_BUFFER].limit()]; 106 buffers[MAPPED_BUFFER].get(array).flip(); 107 108 buffers[DIRECT_BUFFER] = ByteBuffer.allocateDirect(array.length); 109 buffers[DIRECT_BUFFER].put(array).flip(); 110 111 buffers[ARRAY_BUFFER] = ByteBuffer.allocate(array.length); 112 buffers[ARRAY_BUFFER].put(array).flip(); 113 114 buffers[WRAPPED_BUFFER] = ByteBuffer.wrap(array); 115 116 buffers[READ_ONLY_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].asReadOnlyBuffer(); 117 118 buffers[READ_ONLY_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].asReadOnlyBuffer(); 119 120 buffers[DUP_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].duplicate(); 121 122 buffers[DUP_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].duplicate(); 123 } 124 125 protected Class<?> loadClass(String name, boolean resolve) 126 throws ClassNotFoundException 127 { 128 Class<?> c; 129 if (!"TestClass".equals(name)) { 130 c = super.loadClass(name, resolve); 131 } else { 132 // should not delegate to the system class loader 133 c = findClass(name); 134 if (resolve) { 135 resolveClass(c); 136 } 137 } 138 return c; 139 } 140 141 protected Class<?> findClass(String name) 142 throws ClassNotFoundException 143 { 144 if (!"TestClass".equals(name)) { 145 throw new ClassNotFoundException("Unexpected class: " + name); 146 } 147 return defineClass(name, buffers[loaderType], null); 148 } 149 } /* DummyClassLoader */ 150 151} /* DefineClassByteBuffer */ 152