1 /* 2 * Copyright (c) 2010, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 // Compiled and invoked by repocat.sh 25 26 import java.io.*; 27 import java.util.*; 28 import java.lang.module.*; 29 import java.nio.*; 30 import java.nio.channels.*; 31 import org.openjdk.jigsaw.*; 32 import org.openjdk.jigsaw.RepositoryCatalog.StreamedRepositoryCatalog; 33 34 import static org.openjdk.jigsaw.Repository.ModuleType; 35 import static org.openjdk.jigsaw.FileConstants.ModuleFile.HashType; 36 37 38 public class _RepositoryCatalog { 39 40 private static ModuleSystem ms = ModuleSystem.base(); 41 42 private static <T> boolean eq(Collection<T> c1, Collection<T> c2) { 43 return c1.containsAll(c2) && c2.containsAll(c1); 44 } 45 46 static File CAT_FILE = new File("z.scat"); 47 48 static void writeStreamed(Map<ModuleId,byte[]> modules, String[] args) 49 throws Exception 50 { 51 StreamedRepositoryCatalog rc = RepositoryCatalog.load(null); 52 ByteBuffer bb = ByteBuffer.allocate(8192); 53 for (int i = 0; i < args.length; i++) { 54 bb.clear(); 55 try (FileChannel fc = new FileInputStream(args[i]).getChannel()) { 56 int s = (int)fc.size(); 57 if (bb.capacity() < s) 58 bb = ByteBuffer.allocate(s); 59 int n = fc.read(bb); 60 if (n != s) 61 throw new IOException("Mis-sized read"); 62 rc.add(ModuleType.JMOD, Arrays.copyOfRange(bb.array(), 0, n), 63 42, 93, 64 HashType.SHA256, new byte[0]); 65 modules.put(ms.parseModuleInfo(bb.array()).id(), 66 Arrays.copyOfRange(bb.array(), 0, n)); 67 } 68 } 69 try (OutputStream rco = new FileOutputStream(CAT_FILE)) { 70 rc.store(rco); 71 } 72 } 73 74 static StreamedRepositoryCatalog readStreamed(Map<ModuleId,byte[]> modules) 75 throws Exception 76 { 77 InputStream in = new FileInputStream(CAT_FILE); 78 StreamedRepositoryCatalog rc = null; 79 try { 80 rc = RepositoryCatalog.load(in); 81 } finally { 82 in.close(); 83 } 84 Set<ModuleId> mids = new HashSet<>(); 85 rc.gatherDeclaringModuleIds(mids); 86 assert eq(mids, modules.keySet()); 87 for (ModuleId mid : mids) { 88 assert Arrays.equals(rc.readModuleInfoBytes(mid), 89 modules.get(mid)) 90 : mid; 91 } 92 return rc; 93 } 94 95 static void deleteStreamed(Map<ModuleId,byte[]> modules, 96 StreamedRepositoryCatalog rc) 97 throws Exception 98 { 99 ModuleId dmid = ms.parseModuleId("twisty@1"); 100 assert rc.remove(dmid); 101 OutputStream out = new FileOutputStream(CAT_FILE); 102 rc.store(out); 103 Map<ModuleId,byte[]> mods = new HashMap<>(modules); 104 for (Iterator<ModuleId> i = mods.keySet().iterator(); i.hasNext();) { 105 ModuleId mid = i.next(); 106 if (dmid.equals(mid)) 107 i.remove(); 108 } 109 readStreamed(mods); 110 } 111 112 public static void main(String[] args) 113 throws Exception 114 { 115 Map<ModuleId,byte[]> modules = new HashMap<>(); 116 writeStreamed(modules, args); 117 StreamedRepositoryCatalog rc = readStreamed(modules); 118 deleteStreamed(modules, rc); 119 } 120 121 }