Path: blob/master/src/java.base/share/classes/jdk/internal/jimage/ImageHeader.java
41159 views
/*1* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.jimage;2627import java.nio.ByteBuffer;28import java.nio.IntBuffer;29import java.util.Objects;3031/**32* @implNote This class needs to maintain JDK 8 source compatibility.33*34* It is used internally in the JDK to implement jimage/jrtfs access,35* but also compiled and delivered as part of the jrtfs.jar to support access36* to the jimage file provided by the shipped JDK by tools running on JDK 8.37*/38public final class ImageHeader {39public static final int MAGIC = 0xCAFEDADA;40public static final int MAJOR_VERSION = 1;41public static final int MINOR_VERSION = 0;42private static final int HEADER_SLOTS = 7;4344private final int magic;45private final int majorVersion;46private final int minorVersion;47private final int flags;48private final int resourceCount;49private final int tableLength;50private final int locationsSize;51private final int stringsSize;5253public ImageHeader(int resourceCount, int tableCount,54int locationsSize, int stringsSize) {55this(MAGIC, MAJOR_VERSION, MINOR_VERSION, 0, resourceCount,56tableCount, locationsSize, stringsSize);57}5859public ImageHeader(int magic, int majorVersion, int minorVersion,60int flags, int resourceCount,61int tableLength, int locationsSize, int stringsSize)62{63this.magic = magic;64this.majorVersion = majorVersion;65this.minorVersion = minorVersion;66this.flags = flags;67this.resourceCount = resourceCount;68this.tableLength = tableLength;69this.locationsSize = locationsSize;70this.stringsSize = stringsSize;71}7273public static int getHeaderSize() {74return HEADER_SLOTS * 4;75}7677static ImageHeader readFrom(IntBuffer buffer) {78Objects.requireNonNull(buffer);7980if (buffer.capacity() != HEADER_SLOTS) {81throw new InternalError(82"jimage header not the correct size: " + buffer.capacity());83}8485int magic = buffer.get(0);86int version = buffer.get(1);87int majorVersion = version >>> 16;88int minorVersion = version & 0xFFFF;89int flags = buffer.get(2);90int resourceCount = buffer.get(3);91int tableLength = buffer.get(4);92int locationsSize = buffer.get(5);93int stringsSize = buffer.get(6);9495return new ImageHeader(magic, majorVersion, minorVersion, flags,96resourceCount, tableLength, locationsSize, stringsSize);97}9899public void writeTo(ImageStream stream) {100Objects.requireNonNull(stream);101stream.ensure(getHeaderSize());102writeTo(stream.getBuffer());103}104105public void writeTo(ByteBuffer buffer) {106Objects.requireNonNull(buffer);107buffer.putInt(magic);108buffer.putInt(majorVersion << 16 | minorVersion);109buffer.putInt(flags);110buffer.putInt(resourceCount);111buffer.putInt(tableLength);112buffer.putInt(locationsSize);113buffer.putInt(stringsSize);114}115116public int getMagic() {117return magic;118}119120public int getMajorVersion() {121return majorVersion;122}123124public int getMinorVersion() {125return minorVersion;126}127128public int getFlags() {129return flags;130}131132public int getResourceCount() {133return resourceCount;134}135136public int getTableLength() {137return tableLength;138}139140public int getRedirectSize() {141return tableLength * 4;142}143144public int getOffsetsSize() {145return tableLength * 4;146}147148public int getLocationsSize() {149return locationsSize;150}151152public int getStringsSize() {153return stringsSize;154}155156public int getIndexSize() {157return getHeaderSize() +158getRedirectSize() +159getOffsetsSize() +160getLocationsSize() +161getStringsSize();162}163164int getRedirectOffset() {165return getHeaderSize();166}167168int getOffsetsOffset() {169return getRedirectOffset() +170getRedirectSize();171}172173int getLocationsOffset() {174return getOffsetsOffset() +175getOffsetsSize();176}177178int getStringsOffset() {179return getLocationsOffset() +180getLocationsSize();181}182}183184185