2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
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 |
|
|
34 |
import java.security.*;
|
|
35 |
import java.nio.*;
|
|
36 |
import java.nio.channels.*;
|
|
37 |
import java.io.*;
|
|
38 |
|
|
39 |
public class DefineClassByteBuffer {
|
|
40 |
|
|
41 |
static void test(ClassLoader cl) throws Exception {
|
|
42 |
Class c = Class.forName("TestClass", true, cl);
|
|
43 |
if (!"TestClass".equals(c.getName())) {
|
|
44 |
throw new RuntimeException("Got wrong class: " + c);
|
|
45 |
}
|
|
46 |
}
|
|
47 |
|
|
48 |
public static void main(String arg[]) throws Exception {
|
|
49 |
ClassLoader[] cls = new ClassLoader[DummyClassLoader.MAX_TYPE];
|
|
50 |
for (int i = 0; i < cls.length; i++) {
|
|
51 |
cls[i] = new DummyClassLoader(i);
|
|
52 |
}
|
|
53 |
|
|
54 |
/* Create several instances of the class using different classloaders,
|
|
55 |
which are using different types of ByteBuffer. */
|
|
56 |
for (int i = 0; i < cls.length; i++) {
|
|
57 |
test(cls[i]);
|
|
58 |
}
|
|
59 |
}
|
|
60 |
|
|
61 |
/** Always loads the same class, using various types of ByteBuffers */
|
|
62 |
public static class DummyClassLoader extends SecureClassLoader {
|
|
63 |
|
|
64 |
public static final String CLASS_NAME = "TestClass";
|
|
65 |
|
|
66 |
public static final int MAPPED_BUFFER = 0;
|
|
67 |
public static final int DIRECT_BUFFER = 1;
|
|
68 |
public static final int ARRAY_BUFFER = 2;
|
|
69 |
public static final int WRAPPED_BUFFER = 3;
|
|
70 |
public static final int READ_ONLY_ARRAY_BUFFER = 4;
|
|
71 |
public static final int READ_ONLY_DIRECT_BUFFER = 5;
|
|
72 |
public static final int DUP_ARRAY_BUFFER = 6;
|
|
73 |
public static final int DUP_DIRECT_BUFFER = 7;
|
|
74 |
public static final int MAX_TYPE = 7;
|
|
75 |
|
|
76 |
int loaderType;
|
|
77 |
|
|
78 |
DummyClassLoader(int loaderType) {
|
|
79 |
this.loaderType = loaderType;
|
|
80 |
}
|
|
81 |
|
|
82 |
static ByteBuffer[] buffers = new ByteBuffer[MAX_TYPE + 1];
|
|
83 |
|
|
84 |
static ByteBuffer readClassFile(String name) {
|
|
85 |
try {
|
|
86 |
File f = new File(System.getProperty("test.classes", "."),
|
|
87 |
name);
|
|
88 |
FileInputStream fin = new FileInputStream(f);
|
|
89 |
FileChannel fc = fin.getChannel();
|
|
90 |
return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
|
|
91 |
} catch (FileNotFoundException e) {
|
|
92 |
throw new RuntimeException("Can't open file: " + name, e);
|
|
93 |
} catch (IOException e) {
|
|
94 |
throw new RuntimeException("Can't open file: " + name, e);
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
|
98 |
static {
|
|
99 |
/* create a bunch of different ByteBuffers, starting with a mapped
|
|
100 |
buffer from a class file, and create various duplicate and wrapped
|
|
101 |
buffers. */
|
|
102 |
buffers[MAPPED_BUFFER] = readClassFile(CLASS_NAME + ".class");
|
|
103 |
byte[] array = new byte[buffers[MAPPED_BUFFER].limit()];
|
|
104 |
|
|
105 |
buffers[DIRECT_BUFFER] = ByteBuffer.allocateDirect(array.length);
|
|
106 |
buffers[DIRECT_BUFFER].put(array);
|
|
107 |
|
|
108 |
buffers[ARRAY_BUFFER] = ByteBuffer.allocate(array.length);
|
|
109 |
buffers[ARRAY_BUFFER].put(array);
|
|
110 |
|
|
111 |
buffers[WRAPPED_BUFFER] = ByteBuffer.wrap(array);
|
|
112 |
|
|
113 |
buffers[READ_ONLY_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].asReadOnlyBuffer();
|
|
114 |
|
|
115 |
buffers[READ_ONLY_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].asReadOnlyBuffer();
|
|
116 |
|
|
117 |
buffers[DUP_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].duplicate();
|
|
118 |
|
|
119 |
buffers[DUP_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].duplicate();
|
|
120 |
}
|
|
121 |
|
|
122 |
public Class findClass(String name) {
|
|
123 |
CodeSource cs = null;
|
|
124 |
return defineClass(name, buffers[loaderType], cs);
|
|
125 |
}
|
|
126 |
} /* DummyClassLoader */
|
|
127 |
|
|
128 |
} /* DefineClassByteBuffer */
|