ImageStringsWriter.java revision 13901:b2a69d66dc65
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 assert offset == 0 : "Empty string not zero offset"; 48 // Reserve 1 offset for frequently used ".class". 49 addString("class"); 50 } 51 52 private int addString(final String string) { 53 int offset = stream.getPosition(); 54 byte[] bytes = ImageStringsReader.mutf8FromString(string); 55 stream.put(bytes, 0, bytes.length); 56 stream.put('\0'); 57 stringToOffsetMap.put(string, offset); 58 59 return offset; 60 } 61 62 @Override 63 public int add(final String string) { 64 int offset = find(string); 65 66 return offset == NOT_FOUND ? addString(string) : offset; 67 } 68 69 int find(final String string) { 70 Integer offset = stringToOffsetMap.get(string); 71 72 return offset != null ? offset : NOT_FOUND; 73 } 74 75 @Override 76 public String get(int offset) { 77 ByteBuffer buffer = stream.getBuffer(); 78 int capacity = buffer.capacity(); 79 assert 0 <= offset && offset < capacity : "String buffer offset out of range"; 80 int zero = NOT_FOUND; 81 for (int i = offset; i < capacity; i++) { 82 if (buffer.get(i) == '\0') { 83 zero = i; 84 break; 85 } 86 } 87 assert zero != NOT_FOUND; 88 int length = zero - offset; 89 byte[] bytes = new byte[length]; 90 int mark = buffer.position(); 91 buffer.position(offset); 92 buffer.get(bytes); 93 buffer.position(mark); 94 95 return ImageStringsReader.stringFromMUTF8(bytes); 96 } 97 98 ImageStream getStream() { 99 return stream; 100 } 101 102 int getSize() { 103 return stream.getSize(); 104 } 105 106 int getCount() { 107 return stringToOffsetMap.size(); 108 } 109} 110