0N/A/*
3261N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4894899
0N/A * @summary Test various cases of passing java.nio.ByteBuffers
0N/A * to defineClass().
0N/A *
0N/A * @build DefineClassByteBuffer TestClass
0N/A * @run main DefineClassByteBuffer
0N/A */
0N/A
0N/Aimport java.nio.*;
0N/Aimport java.nio.channels.*;
0N/Aimport java.io.*;
0N/A
0N/Apublic class DefineClassByteBuffer {
0N/A
0N/A static void test(ClassLoader cl) throws Exception {
2487N/A Class<?> c = Class.forName("TestClass", true, cl);
0N/A if (!"TestClass".equals(c.getName())) {
0N/A throw new RuntimeException("Got wrong class: " + c);
0N/A }
2487N/A if (c.getClassLoader() != cl) {
2487N/A throw new RuntimeException("TestClass defined by wrong classloader: " + c.getClassLoader());
2487N/A }
0N/A }
0N/A
0N/A public static void main(String arg[]) throws Exception {
2487N/A DummyClassLoader[] cls = new DummyClassLoader[DummyClassLoader.MAX_TYPE];
0N/A for (int i = 0; i < cls.length; i++) {
0N/A cls[i] = new DummyClassLoader(i);
0N/A }
0N/A
0N/A /* Create several instances of the class using different classloaders,
0N/A which are using different types of ByteBuffer. */
0N/A for (int i = 0; i < cls.length; i++) {
2487N/A test(cls[i]);
0N/A }
0N/A }
0N/A
0N/A /** Always loads the same class, using various types of ByteBuffers */
0N/A public static class DummyClassLoader extends ClassLoader {
0N/A
0N/A public static final String CLASS_NAME = "TestClass";
0N/A
0N/A public static final int MAPPED_BUFFER = 0;
0N/A public static final int DIRECT_BUFFER = 1;
0N/A public static final int ARRAY_BUFFER = 2;
0N/A public static final int WRAPPED_BUFFER = 3;
0N/A public static final int READ_ONLY_ARRAY_BUFFER = 4;
0N/A public static final int READ_ONLY_DIRECT_BUFFER = 5;
0N/A public static final int DUP_ARRAY_BUFFER = 6;
0N/A public static final int DUP_DIRECT_BUFFER = 7;
0N/A public static final int MAX_TYPE = 7;
0N/A
0N/A int loaderType;
0N/A
0N/A DummyClassLoader(int loaderType) {
0N/A this.loaderType = loaderType;
0N/A }
0N/A
0N/A static ByteBuffer[] buffers = new ByteBuffer[MAX_TYPE + 1];
0N/A
0N/A static ByteBuffer readClassFile(String name) {
0N/A try {
0N/A File f = new File(System.getProperty("test.classes", "."),
0N/A name);
0N/A FileInputStream fin = new FileInputStream(f);
0N/A FileChannel fc = fin.getChannel();
0N/A return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
0N/A } catch (FileNotFoundException e) {
0N/A throw new RuntimeException("Can't open file: " + name, e);
0N/A } catch (IOException e) {
0N/A throw new RuntimeException("Can't open file: " + name, e);
0N/A }
0N/A }
0N/A
0N/A static {
0N/A /* create a bunch of different ByteBuffers, starting with a mapped
0N/A buffer from a class file, and create various duplicate and wrapped
0N/A buffers. */
0N/A buffers[MAPPED_BUFFER] = readClassFile(CLASS_NAME + ".class");
0N/A byte[] array = new byte[buffers[MAPPED_BUFFER].limit()];
2487N/A buffers[MAPPED_BUFFER].get(array).flip();
0N/A
0N/A buffers[DIRECT_BUFFER] = ByteBuffer.allocateDirect(array.length);
2487N/A buffers[DIRECT_BUFFER].put(array).flip();
0N/A
0N/A buffers[ARRAY_BUFFER] = ByteBuffer.allocate(array.length);
2487N/A buffers[ARRAY_BUFFER].put(array).flip();
0N/A
0N/A buffers[WRAPPED_BUFFER] = ByteBuffer.wrap(array);
0N/A
0N/A buffers[READ_ONLY_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].asReadOnlyBuffer();
0N/A
0N/A buffers[READ_ONLY_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].asReadOnlyBuffer();
0N/A
0N/A buffers[DUP_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].duplicate();
0N/A
0N/A buffers[DUP_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].duplicate();
0N/A }
0N/A
2487N/A protected Class<?> loadClass(String name, boolean resolve)
2487N/A throws ClassNotFoundException
2487N/A {
2487N/A Class<?> c;
2487N/A if (!"TestClass".equals(name)) {
2487N/A c = super.loadClass(name, resolve);
2487N/A } else {
2487N/A // should not delegate to the system class loader
2487N/A c = findClass(name);
2487N/A if (resolve) {
2487N/A resolveClass(c);
2487N/A }
2487N/A }
2487N/A return c;
2487N/A }
2487N/A
2487N/A protected Class<?> findClass(String name)
2487N/A throws ClassNotFoundException
2487N/A {
2487N/A if (!"TestClass".equals(name)) {
2487N/A throw new ClassNotFoundException("Unexpected class: " + name);
2487N/A }
2487N/A return defineClass(name, buffers[loaderType], null);
2487N/A }
0N/A } /* DummyClassLoader */
0N/A
0N/A} /* DefineClassByteBuffer */