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