1 /*
  2  * Copyright (c) 2014, 2025, 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  */
 25 
 26 package jdk.tools.jlink.internal;
 27 
 28 import java.nio.ByteBuffer;
 29 import java.util.HashMap;
 30 import jdk.internal.jimage.ImageStream;
 31 import jdk.internal.jimage.ImageStrings;
 32 import jdk.internal.jimage.ImageStringsReader;
 33 
 34 class ImageStringsWriter implements ImageStrings {
 35     private static final int NOT_FOUND = -1;
 36 
 37     private final HashMap<String, Integer> stringToOffsetMap;
 38     private final ImageStream stream;
 39 
 40     ImageStringsWriter() {
 41         this.stringToOffsetMap = new HashMap<>();
 42         this.stream = new ImageStream();
 43 
 44         // Frequently used/special strings for which the offset is useful.
 45         // New strings can be reserved after existing strings without having to
 46         // change the jimage file version, but any change to existing entries
 47         // requires the jimage file version to be increased at the same time.
 48         reserveString("", ImageStrings.EMPTY_STRING_OFFSET);
 49         reserveString("class", ImageStrings.CLASS_STRING_OFFSET);
 50         reserveString("modules", ImageStrings.MODULES_STRING_OFFSET);
 51         reserveString("packages", ImageStrings.PACKAGES_STRING_OFFSET);
 52     }
 53 
 54     private void reserveString(String value, int expectedOffset) {
 55         int offset = addString(value);
 56         if (offset != expectedOffset) {
 57             throw new InternalError("Reserved string \"" + value + "\" not at expected offset " + expectedOffset + "[was " + offset + "]");
 58         }
 59     }
 60 
 61     private int addString(final String string) {
 62         int offset = stream.getPosition();
 63         byte[] bytes = ImageStringsReader.mutf8FromString(string);
 64         stream.put(bytes, 0, bytes.length);
 65         stream.put('\0');
 66         stringToOffsetMap.put(string, offset);
 67 
 68         return offset;
 69     }
 70 
 71     @Override
 72     public int add(final String string) {
 73         int offset = find(string);
 74 
 75         return offset == NOT_FOUND ? addString(string) : offset;
 76     }
 77 
 78     int find(final String string) {
 79         Integer offset = stringToOffsetMap.get(string);
 80 
 81         return offset != null ? offset : NOT_FOUND;
 82     }
 83 
 84     @Override
 85     public String get(int offset) {
 86         ByteBuffer buffer = stream.getBuffer();
 87         int capacity = buffer.capacity();
 88         if (offset < 0 || offset >= capacity) {
 89             throw new InternalError("String buffer offset out of range");
 90         }
 91         int zero = NOT_FOUND;
 92         for (int i = offset; i < capacity; i++) {
 93             if (buffer.get(i) == '\0') {
 94                 zero = i;
 95                 break;
 96             }
 97         }
 98         if (zero == NOT_FOUND) {
 99             throw new InternalError("String zero terminator not found");
100         }
101         int length = zero - offset;
102         byte[] bytes = new byte[length];
103         int mark = buffer.position();
104         buffer.position(offset);
105         buffer.get(bytes);
106         buffer.position(mark);
107 
108         return ImageStringsReader.stringFromMUTF8(bytes);
109     }
110 
111     ImageStream getStream() {
112         return stream;
113     }
114 
115     int getSize() {
116         return stream.getSize();
117     }
118 
119     int getCount() {
120         return stringToOffsetMap.size();
121     }
122 }