1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dalvik.system;
18 
19 import libcore.util.NonNull;
20 import libcore.util.Nullable;
21 import java.nio.ByteBuffer;
22 
23 /**
24  * A {@link ClassLoader} implementation that loads classes from a
25  * buffer containing a DEX file. This can be used to execute code that
26  * has not been written to the local file system.
27  */
28 public final class InMemoryDexClassLoader extends BaseDexClassLoader {
29     /**
30      * Create an in-memory DEX class loader with the given dex buffers.
31      *
32      * @param dexBuffers array of buffers containing DEX files between
33      *                       <tt>buffer.position()</tt> and <tt>buffer.limit()</tt>.
34      * @param librarySearchPath the list of directories containing native
35      *   libraries, delimited by {@code File.pathSeparator}; may be {@code null}
36      * @param parent the parent class loader for delegation.
37      */
InMemoryDexClassLoader(@onNull ByteBuffer @onNull [] dexBuffers, @Nullable String librarySearchPath, @Nullable ClassLoader parent)38     public InMemoryDexClassLoader(@NonNull ByteBuffer @NonNull [] dexBuffers,
39             @Nullable String librarySearchPath, @Nullable ClassLoader parent) {
40         super(dexBuffers, librarySearchPath, parent);
41     }
42 
43     /**
44      * Create an in-memory DEX class loader with the given dex buffers.
45      *
46      * @param dexBuffers array of buffers containing DEX files between
47      *                       <tt>buffer.position()</tt> and <tt>buffer.limit()</tt>.
48      * @param parent the parent class loader for delegation.
49      */
InMemoryDexClassLoader(@onNull ByteBuffer @onNull [] dexBuffers, @Nullable ClassLoader parent)50     public InMemoryDexClassLoader(@NonNull ByteBuffer @NonNull [] dexBuffers,
51             @Nullable ClassLoader parent) {
52         this(dexBuffers, null, parent);
53     }
54 
55     /**
56      * Creates a new in-memory DEX class loader.
57      *
58      * @param dexBuffer buffer containing DEX file contents between
59      *                       <tt>buffer.position()</tt> and <tt>buffer.limit()</tt>.
60      * @param parent the parent class loader for delegation.
61      */
InMemoryDexClassLoader(@onNull ByteBuffer dexBuffer, @Nullable ClassLoader parent)62     public InMemoryDexClassLoader(@NonNull ByteBuffer dexBuffer, @Nullable ClassLoader parent) {
63         this(new ByteBuffer[] { dexBuffer }, parent);
64     }
65 }
66