author | redestad |
Fri, 17 Feb 2017 18:12:55 +0100 | |
changeset 43837 | 51a9f82cff03 |
parent 39834 | 53a6fb443c20 |
permissions | -rw-r--r-- |
36511 | 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 |
*/ |
|
25 |
package jdk.tools.jlink.internal.plugins; |
|
26 |
||
27 |
import java.io.ByteArrayOutputStream; |
|
28 |
import java.io.IOException; |
|
29 |
import java.nio.ByteBuffer; |
|
39042 | 30 |
import java.util.Arrays; |
36511 | 31 |
import java.util.Map; |
32 |
import java.util.function.Predicate; |
|
33 |
import java.util.zip.Deflater; |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
34 |
import jdk.tools.jlink.internal.ResourcePoolManager; |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
35 |
import jdk.tools.jlink.internal.ResourcePoolManager.ResourcePoolImpl; |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
36 |
import jdk.tools.jlink.plugin.ResourcePool; |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
37 |
import jdk.tools.jlink.plugin.ResourcePoolBuilder; |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
38 |
import jdk.tools.jlink.plugin.ResourcePoolEntry; |
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39308
diff
changeset
|
39 |
import jdk.tools.jlink.plugin.Plugin; |
36511 | 40 |
|
41 |
/** |
|
42 |
* |
|
43 |
* ZIP Compression plugin |
|
44 |
*/ |
|
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39308
diff
changeset
|
45 |
public final class ZipPlugin implements Plugin { |
36511 | 46 |
|
47 |
public static final String NAME = "zip"; |
|
48 |
private Predicate<String> predicate; |
|
49 |
||
50 |
public ZipPlugin() { |
|
51 |
||
52 |
} |
|
53 |
||
39042 | 54 |
ZipPlugin(String[] patterns) { |
55 |
this(ResourceFilter.includeFilter(Arrays.asList(patterns))); |
|
36511 | 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 |
|
39129
4b2086305b68
8159593: Plugin Set<Category> getType() should return a Category
sundar
parents:
39042
diff
changeset
|
68 |
public Category getType() { |
4b2086305b68
8159593: Plugin Set<Category> getType() should return a Category
sundar
parents:
39042
diff
changeset
|
69 |
return Category.COMPRESSOR; |
36511 | 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) { |
|
39042 | 89 |
predicate = ResourceFilter.includeFilter(config.get(NAME)); |
36511 | 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 |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
117 |
public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) { |
38320 | 118 |
in.transformAndCopy((resource) -> { |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
119 |
ResourcePoolEntry res = resource; |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
120 |
if (resource.type().equals(ResourcePoolEntry.Type.CLASS_OR_RESOURCE) |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
121 |
&& predicate.test(resource.path())) { |
36511 | 122 |
byte[] compressed; |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
123 |
compressed = compress(resource.contentBytes()); |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
124 |
res = ResourcePoolManager.newCompressedResource(resource, |
36511 | 125 |
ByteBuffer.wrap(compressed), getName(), null, |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
126 |
((ResourcePoolImpl)in).getStringTable(), in.byteOrder()); |
36511 | 127 |
} |
128 |
return res; |
|
129 |
}, out); |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
130 |
|
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
131 |
return out.build(); |
36511 | 132 |
} |
133 |
} |