1/* 2 * Copyright (c) 2015, 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 */ 25package jdk.tools.jlink.internal.plugins; 26 27import java.io.ByteArrayOutputStream; 28import java.io.IOException; 29import java.nio.ByteBuffer; 30import java.util.Arrays; 31import java.util.Map; 32import java.util.function.Predicate; 33import java.util.zip.Deflater; 34import jdk.tools.jlink.internal.ResourcePoolManager; 35import jdk.tools.jlink.internal.ResourcePoolManager.ResourcePoolImpl; 36import jdk.tools.jlink.plugin.ResourcePool; 37import jdk.tools.jlink.plugin.ResourcePoolBuilder; 38import jdk.tools.jlink.plugin.ResourcePoolEntry; 39import jdk.tools.jlink.plugin.Plugin; 40 41/** 42 * 43 * ZIP Compression plugin 44 */ 45public final class ZipPlugin implements Plugin { 46 47 public static final String NAME = "zip"; 48 private Predicate<String> predicate; 49 50 public ZipPlugin() { 51 52 } 53 54 ZipPlugin(String[] patterns) { 55 this(ResourceFilter.includeFilter(Arrays.asList(patterns))); 56 } 57 58 ZipPlugin(Predicate<String> predicate) { 59 this.predicate = predicate; 60 } 61 62 @Override 63 public String getName() { 64 return NAME; 65 } 66 67 @Override 68 public Category getType() { 69 return Category.COMPRESSOR; 70 } 71 72 @Override 73 public String getDescription() { 74 return PluginsResourceBundle.getDescription(NAME); 75 } 76 77 @Override 78 public boolean hasArguments() { 79 return false; 80 } 81 82 @Override 83 public String getArgumentsDescription() { 84 return PluginsResourceBundle.getArgument(NAME); 85 } 86 87 @Override 88 public void configure(Map<String, String> config) { 89 predicate = ResourceFilter.includeFilter(config.get(NAME)); 90 } 91 92 static byte[] compress(byte[] bytesIn) { 93 Deflater deflater = new Deflater(); 94 deflater.setInput(bytesIn); 95 ByteArrayOutputStream stream = new ByteArrayOutputStream(bytesIn.length); 96 byte[] buffer = new byte[1024]; 97 98 deflater.finish(); 99 while (!deflater.finished()) { 100 int count = deflater.deflate(buffer); 101 stream.write(buffer, 0, count); 102 } 103 104 try { 105 stream.close(); 106 } catch (IOException ex) { 107 return bytesIn; 108 } 109 110 byte[] bytesOut = stream.toByteArray(); 111 deflater.end(); 112 113 return bytesOut; 114 } 115 116 @Override 117 public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) { 118 in.transformAndCopy((resource) -> { 119 ResourcePoolEntry res = resource; 120 if (resource.type().equals(ResourcePoolEntry.Type.CLASS_OR_RESOURCE) 121 && predicate.test(resource.path())) { 122 byte[] compressed; 123 compressed = compress(resource.contentBytes()); 124 res = ResourcePoolManager.newCompressedResource(resource, 125 ByteBuffer.wrap(compressed), getName(), null, 126 ((ResourcePoolImpl)in).getStringTable(), in.byteOrder()); 127 } 128 return res; 129 }, out); 130 131 return out.build(); 132 } 133} 134