01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.object.bytecode.hook.impl;
05:
06: import java.nio.ByteBuffer;
07: import java.security.ProtectionDomain;
08:
09: /**
10: * The only purpose of this class is to localize the references to the java.nio.ByteBuffer type so that we don't have to
11: * use reflection
12: */
13: public class ClassProcessorHelperJDK15 {
14:
15: public static ByteBuffer defineClass0Pre(ClassLoader caller,
16: String name, ByteBuffer buffer, int off, int len,
17: ProtectionDomain pd) {
18:
19: final byte[] origBytes;
20: final int offset;
21:
22: if (buffer.hasArray()) {
23: origBytes = buffer.array();
24: offset = buffer.arrayOffset() + buffer.position();
25: } else {
26: origBytes = new byte[len];
27: offset = 0;
28: int origPos = buffer.position();
29: try {
30: buffer.get(origBytes);
31: } finally {
32: buffer.position(origPos);
33: }
34: }
35:
36: byte[] possiblyTransformed = ClassProcessorHelper
37: .defineClass0Pre(caller, name, origBytes, offset, len,
38: pd);
39:
40: if (possiblyTransformed == origBytes) {
41: return buffer;
42: }
43:
44: ByteBuffer returnValue;
45: if (buffer.isDirect()) {
46: returnValue = ByteBuffer
47: .allocateDirect(possiblyTransformed.length);
48: } else {
49: returnValue = ByteBuffer
50: .allocate(possiblyTransformed.length);
51: }
52:
53: returnValue.put(possiblyTransformed);
54: returnValue.position(0);
55: return returnValue;
56: }
57:
58: }
|