20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package jdk.internal.jimage;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.lang.reflect.InvocationTargetException;
31 import java.lang.reflect.Method;
32 import java.nio.ByteBuffer;
33 import java.nio.ByteOrder;
34 import java.nio.IntBuffer;
35 import java.nio.channels.FileChannel;
36 import java.nio.file.Path;
37 import java.nio.file.StandardOpenOption;
38 import java.security.AccessController;
39 import java.security.PrivilegedAction;
40 import java.util.Objects;
41 import java.util.stream.IntStream;
42 import jdk.internal.jimage.decompressor.Decompressor;
43
44 /**
45 * @implNote This class needs to maintain JDK 8 source compatibility.
46 *
47 * It is used internally in the JDK to implement jimage/jrtfs access,
48 * but also compiled and delivered as part of the jrtfs.jar to support access
49 * to the jimage file provided by the shipped JDK by tools running on JDK 8.
50 */
51 public class BasicImageReader implements AutoCloseable {
52 @SuppressWarnings({ "removal", "suppression" })
53 private static boolean isSystemProperty(String key, String value, String def) {
54 // No lambdas during bootstrap
55 return AccessController.doPrivileged(
56 new PrivilegedAction<Boolean>() {
57 @Override
58 public Boolean run() {
59 return value.equals(System.getProperty(key, def));
60 }
61 });
299 // index is twos complement of location attributes index.
300 return -index - 1;
301 } else if (index > 0) {
302 // index is hash seed needed to compute location attributes index.
303 return ImageStringsReader.hashCode(module, name, index) % count;
304 } else {
305 // No entry.
306 return -1;
307 }
308 }
309
310 public String[] getEntryNames() {
311 return IntStream.range(0, offsets.capacity())
312 .map(offsets::get)
313 .filter(o -> o != 0)
314 .mapToObj(o -> ImageLocation.readFrom(this, o).getFullName())
315 .sorted()
316 .toArray(String[]::new);
317 }
318
319 ImageLocation getLocation(int offset) {
320 return ImageLocation.readFrom(this, offset);
321 }
322
323 public long[] getAttributes(int offset) {
324 if (offset < 0 || offset >= locations.limit()) {
325 throw new IndexOutOfBoundsException("offset");
326 }
327 return ImageLocation.decompress(locations, offset);
328 }
329
330 public String getString(int offset) {
331 if (offset < 0 || offset >= strings.limit()) {
332 throw new IndexOutOfBoundsException("offset");
333 }
334 return ImageStringsReader.stringFromByteBuffer(strings, offset);
335 }
336
337 public int match(int offset, String string, int stringOffset) {
338 if (offset < 0 || offset >= strings.limit()) {
|
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package jdk.internal.jimage;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.lang.reflect.InvocationTargetException;
31 import java.lang.reflect.Method;
32 import java.nio.ByteBuffer;
33 import java.nio.ByteOrder;
34 import java.nio.IntBuffer;
35 import java.nio.channels.FileChannel;
36 import java.nio.file.Path;
37 import java.nio.file.StandardOpenOption;
38 import java.security.AccessController;
39 import java.security.PrivilegedAction;
40 import java.util.NoSuchElementException;
41 import java.util.Objects;
42 import java.util.stream.IntStream;
43 import java.util.stream.Stream;
44
45 import jdk.internal.jimage.decompressor.Decompressor;
46
47 /**
48 * @implNote This class needs to maintain JDK 8 source compatibility.
49 *
50 * It is used internally in the JDK to implement jimage/jrtfs access,
51 * but also compiled and delivered as part of the jrtfs.jar to support access
52 * to the jimage file provided by the shipped JDK by tools running on JDK 8.
53 */
54 public class BasicImageReader implements AutoCloseable {
55 @SuppressWarnings({ "removal", "suppression" })
56 private static boolean isSystemProperty(String key, String value, String def) {
57 // No lambdas during bootstrap
58 return AccessController.doPrivileged(
59 new PrivilegedAction<Boolean>() {
60 @Override
61 public Boolean run() {
62 return value.equals(System.getProperty(key, def));
63 }
64 });
302 // index is twos complement of location attributes index.
303 return -index - 1;
304 } else if (index > 0) {
305 // index is hash seed needed to compute location attributes index.
306 return ImageStringsReader.hashCode(module, name, index) % count;
307 } else {
308 // No entry.
309 return -1;
310 }
311 }
312
313 public String[] getEntryNames() {
314 return IntStream.range(0, offsets.capacity())
315 .map(offsets::get)
316 .filter(o -> o != 0)
317 .mapToObj(o -> ImageLocation.readFrom(this, o).getFullName())
318 .sorted()
319 .toArray(String[]::new);
320 }
321
322 /**
323 * Returns the "raw" API for accessing underlying jimage resource entries.
324 *
325 * <p>This is only meaningful for use by code dealing directly with jimage
326 * files, and cannot be used to reliably lookup resources used at runtime.
327 *
328 * <p>This API remains valid until the image reader from which it was
329 * obtained is closed.
330 */
331 // Package visible for use by ImageReader.
332 ResourceEntries getResourceEntries() {
333 return new ResourceEntries() {
334 @Override
335 public Stream<String> getEntryNames(String module) {
336 if (module.isEmpty() || module.equals("modules") || module.equals("packages")) {
337 throw new IllegalArgumentException("Invalid module name: " + module);
338 }
339 return IntStream.range(0, offsets.capacity())
340 .map(offsets::get)
341 .filter(offset -> offset != 0)
342 // Reusing a location instance or getting the module
343 // offset directly would save a lot of allocations here.
344 .mapToObj(offset -> ImageLocation.readFrom(BasicImageReader.this, offset))
345 // Reverse lookup of module offset would be faster here.
346 .filter(loc -> module.equals(loc.getModule()))
347 .map(ImageLocation::getFullName);
348 }
349
350 private ImageLocation getResourceLocation(String name) {
351 if (!name.startsWith("/modules/") && !name.startsWith("/packages/")) {
352 ImageLocation location = BasicImageReader.this.findLocation(name);
353 if (location != null) {
354 return location;
355 }
356 }
357 throw new NoSuchElementException("No such resource entry: " + name);
358 }
359
360 @Override
361 public long getSize(String name) {
362 return getResourceLocation(name).getUncompressedSize();
363 }
364
365 @Override
366 public byte[] getBytes(String name) {
367 return BasicImageReader.this.getResource(getResourceLocation(name));
368 }
369 };
370 }
371
372 ImageLocation getLocation(int offset) {
373 return ImageLocation.readFrom(this, offset);
374 }
375
376 public long[] getAttributes(int offset) {
377 if (offset < 0 || offset >= locations.limit()) {
378 throw new IndexOutOfBoundsException("offset");
379 }
380 return ImageLocation.decompress(locations, offset);
381 }
382
383 public String getString(int offset) {
384 if (offset < 0 || offset >= strings.limit()) {
385 throw new IndexOutOfBoundsException("offset");
386 }
387 return ImageStringsReader.stringFromByteBuffer(strings, offset);
388 }
389
390 public int match(int offset, String string, int stringOffset) {
391 if (offset < 0 || offset >= strings.limit()) {
|