01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.harmony.nio.internal;
19:
20: import java.lang.reflect.Constructor;
21: import java.nio.MappedByteBuffer;
22: import java.security.AccessController;
23: import java.security.PrivilegedAction;
24:
25: import org.apache.harmony.luni.platform.PlatformAddress;
26:
27: class MappedByteBufferFactory {
28:
29: static final Constructor<?> constructor;
30:
31: static {
32: constructor = AccessController
33: .doPrivileged(new PrivilegedAction<Constructor<?>>() {
34: public Constructor<?> run() {
35: try {
36: Class<?> wrapperClazz = ClassLoader
37: .getSystemClassLoader()
38: .loadClass(
39: "java.nio.MappedByteBufferAdapter"); //$NON-NLS-1$
40: Constructor<?> result = wrapperClazz
41: .getConstructor(new Class[] {
42: PlatformAddress.class,
43: int.class, int.class,
44: int.class });
45: result.setAccessible(true);
46: return result;
47: } catch (Exception e) {
48: throw new RuntimeException(e);
49: }
50: }
51: });
52: }
53:
54: static MappedByteBuffer getBuffer(PlatformAddress addr,
55: int mapmode, long size, int offset) throws Exception {
56: /*
57: * Spec points out explicitly that the size of map should be no greater
58: * than Integer.MAX_VALUE, so long to int cast is safe here.
59: */
60: return (MappedByteBuffer) constructor.newInstance(new Object[] {
61: addr, new Integer((int) size), new Integer(offset),
62: new Integer(mapmode) });
63: }
64: }
|