Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/java.base/share/classes/jdk/internal/jimage/ImageLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package jdk.internal.jimage;

import java.nio.ByteBuffer;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;

Expand Down Expand Up @@ -159,6 +160,29 @@ public static int getFlags(String name, Predicate<String> hasEntry) {
}
}

/**
* Helper function to calculate package flags for {@code "/packages/xxx"}
* directory entries.
*
* <p>Based on the module references, the flags are:
* <ul>
* <li>{@code FLAGS_HAS_PREVIEW_VERSION} if <em>any</em> referenced
* package has a preview version.
* <li>{@code FLAGS_IS_PREVIEW_ONLY} if <em>all</em> referenced packages
* are preview only.
* </ul>
*
* @return package flags for {@code "/packages/xxx"} directory entries.
*/
public static int getPackageFlags(List<ModuleReference> moduleReferences) {
boolean hasPreviewVersion =
moduleReferences.stream().anyMatch(ModuleReference::hasPreviewVersion);
boolean isPreviewOnly =
moduleReferences.stream().allMatch(ModuleReference::isPreviewOnly);
return (hasPreviewVersion ? ImageLocation.FLAGS_HAS_PREVIEW_VERSION : 0)
| (isPreviewOnly ? ImageLocation.FLAGS_IS_PREVIEW_ONLY : 0);
}

/**
* Tests a non-preview image location's flags to see if it has preview
* content associated with it.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public final class ModuleReference implements Comparable<ModuleReference> {
/** If set, this package exists in non-preview mode. */
private static final int FLAGS_PKG_HAS_NORMAL_VERSION = 0x2;
/** If set, the associated module has resources (in normal or preview mode). */
// TODO: Make this private again when image writer code is updated.
public static final int FLAGS_PKG_HAS_RESOURCES = 0x4;
private static final int FLAGS_PKG_HAS_RESOURCES = 0x4;

/**
* References are ordered with preview versions first which permits early
Expand Down Expand Up @@ -176,9 +175,9 @@ public static Iterator<Integer> readNameOffsets(
if (bufferSize == 0 || (bufferSize & 0x1) != 0) {
throw new IllegalArgumentException("Invalid buffer size");
}
int testFlags = (includeNormal ? FLAGS_PKG_HAS_NORMAL_VERSION : 0)
int includeMask = (includeNormal ? FLAGS_PKG_HAS_NORMAL_VERSION : 0)
+ (includePreview ? FLAGS_PKG_HAS_PREVIEW_VERSION : 0);
if (testFlags == 0) {
if (includeMask == 0) {
throw new IllegalArgumentException("Invalid flags");
}

Expand All @@ -188,14 +187,7 @@ public static Iterator<Integer> readNameOffsets(
int nextIdx(int idx) {
for (; idx < bufferSize; idx += 2) {
// If any of the test flags are set, include this entry.

// Temporarily allow for *neither* flag to be set. This is what would
// be written by a 1.0 version of the jimage flag, and indicates a
// normal resource without a preview version.
// TODO: Remove the zero-check below once image writer code is updated.
int previewFlags =
buffer.get(idx) & (FLAGS_PKG_HAS_NORMAL_VERSION | FLAGS_PKG_HAS_PREVIEW_VERSION);
if (previewFlags == 0 || (previewFlags & testFlags) != 0) {
if ((buffer.get(idx) & includeMask) != 0) {
return idx;
} else if (!includeNormal) {
// Preview entries are first in the offset buffer, so we
Expand Down
13 changes: 13 additions & 0 deletions src/java.base/share/classes/jdk/internal/jimage/PreviewMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public enum PreviewMode {
* Resolves whether preview mode should be enabled for an {@link ImageReader}.
*/
public boolean isPreviewModeEnabled() {
if (!ENABLE_PREVIEW_MODE) {
return false;
}
// A switch, instead of an abstract method, saves 3 subclasses.
switch (this) {
case DISABLED:
Expand Down Expand Up @@ -83,4 +86,14 @@ public boolean isPreviewModeEnabled() {
throw new IllegalStateException("Invalid mode: " + this);
}
}
;

// Temporary system property to disable preview patching and enable the new preview mode
// feature for testing/development. Once the preview mode feature is finished, the value
// will be always 'true' and this code, and all related dead-code can be removed.
private static final boolean DISABLE_PREVIEW_PATCHING_DEFAULT = false;
private static final boolean ENABLE_PREVIEW_MODE = Boolean.parseBoolean(
System.getProperty(
"DISABLE_PREVIEW_PATCHING",
Boolean.toString(DISABLE_PREVIEW_PATCHING_DEFAULT)));
}
7 changes: 1 addition & 6 deletions src/java.base/share/native/libjimage/imageFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,7 @@ bool ImageFileReader::open() {
!read_at((u1*)&_header, header_size, 0) ||
_header.magic(_endian) != IMAGE_MAGIC ||
_header.major_version(_endian) != MAJOR_VERSION ||
// Temporarily, we allow either version (1.1 or 1.0) of the file to
// be read so this code can be committed before image writing changes
// for preview mode. Preview mode changes do not modify any structure,
// so a 1.0 file will look like a jimage without any preview resources.
// TODO: Restore equality check for MINOR_VERSION.
_header.minor_version(_endian) > MINOR_VERSION) {
_header.minor_version(_endian) != MINOR_VERSION) {
close();
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -36,21 +36,17 @@
public final class BasicImageWriter {
public static final String MODULES_IMAGE_NAME = "modules";

private ByteOrder byteOrder;
private ImageStringsWriter strings;
private final ByteOrder byteOrder;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made obvious fields final and removed unused, non-public, code.

private final ImageStringsWriter strings;
private int length;
private int[] redirect;
private ImageLocationWriter[] locations;
private List<ImageLocationWriter> input;
private ImageStream headerStream;
private ImageStream redirectStream;
private ImageStream locationOffsetStream;
private ImageStream locationStream;
private ImageStream allIndexStream;

public BasicImageWriter() {
this(ByteOrder.nativeOrder());
}
private final List<ImageLocationWriter> input;
private final ImageStream headerStream;
private final ImageStream redirectStream;
private final ImageStream locationOffsetStream;
private final ImageStream locationStream;
private final ImageStream allIndexStream;

public BasicImageWriter(ByteOrder byteOrder) {
this.byteOrder = Objects.requireNonNull(byteOrder);
Expand All @@ -75,11 +71,15 @@ public String getString(int offset) {
return strings.get(offset);
}

public void addLocation(String fullname, long contentOffset,
long compressedSize, long uncompressedSize) {
public void addLocation(
String fullname,
long contentOffset,
long compressedSize,
long uncompressedSize,
int previewFlags) {
ImageLocationWriter location =
ImageLocationWriter.newLocation(fullname, strings,
contentOffset, compressedSize, uncompressedSize);
contentOffset, compressedSize, uncompressedSize, previewFlags);
input.add(location);
length++;
}
Expand All @@ -88,10 +88,6 @@ ImageLocationWriter[] getLocations() {
return locations;
}

int getLocationsCount() {
return input.size();
}

private void generatePerfectHash() {
PerfectHashBuilder<ImageLocationWriter> builder =
new PerfectHashBuilder<>(
Expand Down Expand Up @@ -174,16 +170,4 @@ public byte[] getBytes() {

return allIndexStream.toArray();
}

ImageLocationWriter find(String key) {
int index = redirect[ImageStringsReader.hashCode(key) % length];

if (index < 0) {
index = -index - 1;
} else {
index = ImageStringsReader.hashCode(key, index) % length;
}

return locations[index];
}
}
Loading