1/* 2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 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 */ 25package jdk.internal.jimage.decompressor; 26 27import java.nio.ByteBuffer; 28import java.nio.ByteOrder; 29import java.util.Objects; 30import jdk.internal.jimage.decompressor.ResourceDecompressor.StringsProvider; 31 32/** 33 * 34 * A resource header for compressed resource. This class is handled internally, 35 * you don't have to add header to the resource, headers are added automatically 36 * for compressed resources. 37 * 38 * @implNote This class needs to maintain JDK 8 source compatibility. 39 * 40 * It is used internally in the JDK to implement jimage/jrtfs access, 41 * but also compiled and delivered as part of the jrtfs.jar to support access 42 * to the jimage file provided by the shipped JDK by tools running on JDK 8. 43 */ 44public final class CompressedResourceHeader { 45 46 private static final int SIZE = 29; 47 public static final int MAGIC = 0xCAFEFAFA; 48 private final long uncompressedSize; 49 private final long compressedSize; 50 private final int decompressorNameOffset; 51 private final int contentOffset; 52 private final boolean isTerminal; 53 54 public CompressedResourceHeader(long compressedSize, 55 long uncompressedSize, int decompressorNameOffset, int contentOffset, 56 boolean isTerminal) { 57 this.compressedSize = compressedSize; 58 this.uncompressedSize = uncompressedSize; 59 this.decompressorNameOffset = decompressorNameOffset; 60 this.contentOffset = contentOffset; 61 this.isTerminal = isTerminal; 62 } 63 64 public boolean isTerminal() { 65 return isTerminal; 66 } 67 68 public int getDecompressorNameOffset() { 69 return decompressorNameOffset; 70 } 71 72 public int getContentOffset() { 73 return contentOffset; 74 } 75 76 public String getStoredContent(StringsProvider provider) { 77 Objects.requireNonNull(provider); 78 if(contentOffset == -1) { 79 return null; 80 } 81 return provider.getString(contentOffset); 82 } 83 84 public long getUncompressedSize() { 85 return uncompressedSize; 86 } 87 88 public long getResourceSize() { 89 return compressedSize; 90 } 91 92 public byte[] getBytes(ByteOrder order) { 93 Objects.requireNonNull(order); 94 ByteBuffer buffer = ByteBuffer.allocate(SIZE); 95 buffer.order(order); 96 buffer.putInt(MAGIC); 97 buffer.putLong(compressedSize); 98 buffer.putLong(uncompressedSize); 99 buffer.putInt(decompressorNameOffset); 100 buffer.putInt(contentOffset); 101 buffer.put(isTerminal ? (byte)1 : (byte)0); 102 return buffer.array(); 103 } 104 105 public static int getSize() { 106 return SIZE; 107 } 108 109 public static CompressedResourceHeader readFromResource(ByteOrder order, 110 byte[] resource) { 111 Objects.requireNonNull(order); 112 Objects.requireNonNull(resource); 113 if (resource.length < getSize()) { 114 return null; 115 } 116 ByteBuffer buffer = ByteBuffer.wrap(resource, 0, SIZE); 117 buffer.order(order); 118 int magic = buffer.getInt(); 119 if(magic != MAGIC) { 120 return null; 121 } 122 long size = buffer.getLong(); 123 long uncompressedSize = buffer.getLong(); 124 int decompressorNameOffset = buffer.getInt(); 125 int contentIndex = buffer.getInt(); 126 byte isTerminal = buffer.get(); 127 return new CompressedResourceHeader(size, uncompressedSize, 128 decompressorNameOffset, contentIndex, isTerminal == 1); 129 } 130} 131