1 /* 2 * Copyright (c) 2014, 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.internal.jimage; 27 28 import java.nio.ByteBuffer; 29 import java.util.HashMap; 30 31 class ImageStrings { 32 private static final int NOT_FOUND = -1; 33 static final int EMPTY_OFFSET = 0; 34 35 private final HashMap<UTF8String, Integer> stringToOffsetMap; 36 private final ImageStream stream; 37 38 ImageStrings() { 39 this.stringToOffsetMap = new HashMap<>(); 40 this.stream = new ImageStream(); 41 42 // Reserve 0 offset for empty string. 43 int offset = addString(UTF8String.EMPTY_STRING); 44 assert offset == 0 : "Empty string not zero offset"; 45 // Reserve 1 offset for frequently used ".class". 46 addString(UTF8String.CLASS_STRING); 47 } 48 49 ImageStrings(ImageStream stream) { 50 this.stringToOffsetMap = new HashMap<>(); 51 this.stream = stream; 52 } 53 54 private int addString(final UTF8String string) { 55 int offset = stream.getPosition(); 56 string.writeTo(stream); 57 stream.put('\0'); 58 stringToOffsetMap.put(string, offset); 59 60 return offset; 61 } 62 63 int add(final UTF8String string) { 64 int offset = find(string); 65 66 return offset == NOT_FOUND ? addString(string) : offset; 67 } 68 69 int find(final UTF8String string) { 70 Integer offset = stringToOffsetMap.get(string); 71 72 return offset != null ? offset : NOT_FOUND; 73 } 74 75 UTF8String get(int offset) { 76 ByteBuffer buffer = stream.getBuffer(); 77 assert 0 <= offset && offset < buffer.capacity() : "String buffer offset out of range"; 78 int zero = NOT_FOUND; 79 for (int i = offset; i < buffer.capacity(); i++) { 80 if (buffer.get(i) == '\0') { 81 zero = i; 82 break; 83 } 84 } 85 assert zero != UTF8String.NOT_FOUND; 86 int length = zero - offset; 87 byte[] bytes = new byte[length]; 88 int mark = buffer.position(); 89 buffer.position(offset); 90 buffer.get(bytes); 91 buffer.position(mark); 92 93 return new UTF8String(bytes, 0, length); 94 } 95 96 ImageStream getStream() { 97 return stream; 98 } 99 100 int getSize() { 101 return stream.getSize(); 102 } 103 104 int getCount() { 105 return stringToOffsetMap.size(); 106 } 107 }