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.io.DataOutputStream; 29 import java.io.IOException; 30 import java.util.Arrays; 31 import java.util.LinkedHashMap; 32 import java.util.List; 33 import java.util.Map; 34 import java.util.Set; 35 import java.util.function.Function; 36 import java.util.stream.Collectors; 37 38 public class ImageModuleDataWriter { 39 final byte[] bytes; 40 41 public ImageModuleDataWriter(BasicImageWriter writer, 42 Map<String, List<String>> modulePackages) { 43 PerfectHashBuilder<String> packageToModule = new PerfectHashBuilder<>( 44 new PerfectHashBuilder.Entry<String>().getClass(), 45 new PerfectHashBuilder.Bucket<String>().getClass()); 46 PerfectHashBuilder<List<String>> moduleToPackages = new PerfectHashBuilder<>( 47 new PerfectHashBuilder.Entry<List<String>>().getClass(), 48 new PerfectHashBuilder.Bucket<List<String>>().getClass()); 49 50 modulePackages.entrySet().stream().forEach((entry) -> { 51 String moduleName = entry.getKey(); 52 List<String> packages = entry.getValue(); 53 packages.stream().forEach((packageName) -> { 54 packageToModule.put(packageName, moduleName); 55 }); 56 57 moduleToPackages.put(moduleName, packages); 58 }); 59 60 packageToModule.generate(); 61 moduleToPackages.generate(); 62 63 bytes = getBytes(writer, packageToModule, moduleToPackages); 64 } 65 66 public static ImageModuleDataWriter buildModuleData(BasicImageWriter writer, 67 Map<String, Set<String>> modulePackagesMap) { 68 Set<String> modules = modulePackagesMap.keySet(); 69 70 Map<String, List<String>> modulePackages = new LinkedHashMap<>(); 71 modules.stream().sorted().forEach((moduleName) -> { 72 List<String> localPackages = modulePackagesMap.get(moduleName).stream() 73 .map(pn -> pn.replace('.', '/')) 74 .sorted() 75 .collect(Collectors.toList()); 76 modulePackages.put(moduleName, localPackages); 77 }); 78 79 return new ImageModuleDataWriter(writer, modulePackages); 80 } 81 82 public static Map<String, List<String>> toModulePackages(List<String> lines) { 83 Map<String, List<String>> modulePackages = new LinkedHashMap<>(); 84 85 for (String line : lines) { 86 String[] parts = line.split(ImageModuleData.SEPARATOR); 87 String moduleName = parts[0]; 88 List<String> packages = Arrays.asList(Arrays.copyOfRange(parts, 1, parts.length)); 89 modulePackages.put(moduleName, packages); 90 } 91 92 return modulePackages; 93 } 94 95 public void addLocation(String name, BasicImageWriter writer) { 96 writer.addLocation(ImageModuleData.getModuleDataName(name), 0, 0, bytes.length); 97 } 98 99 private byte[] getBytes(BasicImageWriter writer, 100 PerfectHashBuilder<String> packageToModule, 101 PerfectHashBuilder<List<String>> moduleToPackages) { 102 ImageStream stream = new ImageStream(writer.getByteOrder()); 103 104 int[] ptmRedirect = packageToModule.getRedirect(); 105 int[] mtpRedirect = moduleToPackages.getRedirect(); 106 PerfectHashBuilder.Entry<String>[] ptmOrder = packageToModule.getOrder(); 107 PerfectHashBuilder.Entry<List<String>>[] mtpOrder = moduleToPackages.getOrder(); 108 109 stream.putInt(ptmRedirect.length); 110 stream.putInt(mtpRedirect.length); 111 112 for (int value : ptmRedirect) { 113 stream.putInt(value); 114 } 115 116 for (PerfectHashBuilder.Entry<String> entry : ptmOrder) { 117 if (entry != null) { 118 stream.putInt(writer.addString(entry.getKey())); 119 stream.putInt(writer.addString(entry.getValue())); 120 } else { 121 stream.putInt(0); 122 stream.putInt(0); 123 } 124 } 125 126 for (int value : mtpRedirect) { 127 stream.putInt(value); 128 } 129 130 int index = 0; 131 132 for (PerfectHashBuilder.Entry<List<String>> entry : mtpOrder) { 133 if (entry != null) { 134 int count = entry.getValue().size(); 135 stream.putInt(writer.addString(entry.getKey())); 136 stream.putInt(count); 137 stream.putInt(index); 138 index += count; 139 } else { 140 stream.putInt(0); 141 stream.putInt(0); 142 stream.putInt(0); 143 } 144 } 145 146 for (PerfectHashBuilder.Entry<List<String>> entry : mtpOrder) { 147 if (entry != null) { 148 List<String> value = entry.getValue(); 149 value.stream().forEach((packageName) -> { 150 stream.putInt(writer.addString(packageName)); 151 }); 152 } 153 } 154 155 return stream.toArray(); 156 } 157 158 public void writeTo(DataOutputStream out) throws IOException { 159 out.write(bytes, 0, bytes.length); 160 } 161 162 public int size() { 163 return bytes.length; 164 } 165 }