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