< prev index next >

src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ImageStringsWriter.java

Print this page

  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.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     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 

  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 
< prev index next >