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.ModuleFileType;
  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(ModuleFileType.JMOD, ModuleArchitecture.ANY,
  63                        Arrays.copyOfRange(bb.array(), 0, n),
  64                        42, 93,
  65                        HashType.SHA256, new byte[0]);
  66                 modules.put(ms.parseModuleInfo(bb.array()).id(),
  67                             Arrays.copyOfRange(bb.array(), 0, n));
  68             }
  69         }
  70         try (OutputStream rco = new FileOutputStream(CAT_FILE)) {
  71             rc.store(rco);
  72         }
  73     }
  74 
  75     static StreamedRepositoryCatalog readStreamed(Map<ModuleId,byte[]> modules)
  76         throws Exception
  77     {
  78         InputStream in = new FileInputStream(CAT_FILE);
  79         StreamedRepositoryCatalog rc = null;
  80         try {
  81             rc = RepositoryCatalog.load(in);
  82         } finally {
  83             in.close();
  84         }
  85         Set<ModuleId> mids = new HashSet<>();
  86         rc.gatherDeclaringModuleIds(mids);
  87         assert eq(mids, modules.keySet());
  88         for (ModuleId mid : mids) {
  89             assert Arrays.equals(rc.readModuleInfoBytes(mid),
  90                                  modules.get(mid))
  91                 : mid;
  92         }
  93         return rc;
  94     }
  95 
  96     static void deleteStreamed(Map<ModuleId,byte[]> modules,
  97                                StreamedRepositoryCatalog rc)
  98         throws Exception
  99     {
 100         ModuleId dmid = ms.parseModuleId("twisty@1");
 101         assert rc.remove(dmid);
 102         OutputStream out = new FileOutputStream(CAT_FILE);
 103         rc.store(out);
 104         Map<ModuleId,byte[]> mods = new HashMap<>(modules);
 105         for (Iterator<ModuleId> i = mods.keySet().iterator(); i.hasNext();) {
 106             ModuleId mid = i.next();
 107             if (dmid.equals(mid))
 108                 i.remove();
 109         }
 110         readStreamed(mods);
 111     }
 112 
 113     public static void main(String[] args)
 114         throws Exception
 115     {
 116         Map<ModuleId,byte[]> modules = new HashMap<>();
 117         writeStreamed(modules, args);
 118         StreamedRepositoryCatalog rc = readStreamed(modules);
 119         deleteStreamed(modules, rc);
 120     }
 121 
 122 }
--- EOF ---