forked from jruby/warbler
/
WarblerJar.java
182 lines (162 loc) · 6.34 KB
/
WarblerJar.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Copyright (c) 2010-2011 Engine Yard, Inc.
* Copyright (c) 2007-2009 Sun Microsystems, Inc.
* This source code is available under the MIT license.
* See the file LICENSE.txt for details.
*/
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyHash;
import org.jruby.RubyModule;
import org.jruby.RubyString;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.Block;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
import org.jruby.util.JRubyFile;
public class WarblerJar {
public static void create(Ruby runtime) {
RubyModule task = runtime.getClassFromPath("Warbler::Jar");
task.defineAnnotatedMethods(WarblerJar.class);
}
@JRubyMethod
public static IRubyObject create_jar(ThreadContext context, IRubyObject recv, IRubyObject jar_path, IRubyObject entries) {
final Ruby runtime = recv.getRuntime();
if (!(entries instanceof RubyHash)) {
throw runtime.newArgumentError("expected a hash for the second argument");
}
RubyHash hash = (RubyHash) entries;
try {
FileOutputStream file = newFile(jar_path);
try {
ZipOutputStream zip = new ZipOutputStream(file);
addEntries(context, zip, hash);
zip.finish();
} finally {
close(file);
}
} catch (IOException e) {
if (runtime.isDebug()) {
e.printStackTrace();
}
throw runtime.newIOErrorFromException(e);
}
return runtime.getNil();
}
@JRubyMethod
public static IRubyObject entry_in_jar(ThreadContext context, IRubyObject recv, IRubyObject jar_path, IRubyObject entry) {
final Ruby runtime = recv.getRuntime();
try {
InputStream entryStream = getStream(jar_path.convertToString().getUnicodeValue(),
entry.convertToString().getUnicodeValue());
try {
byte[] buf = new byte[16384];
ByteList blist = new ByteList();
int bytesRead = -1;
while ((bytesRead = entryStream.read(buf)) != -1) {
blist.append(buf, 0, bytesRead);
}
IRubyObject stringio = runtime.getModule("StringIO");
return stringio.callMethod(context, "new", runtime.newString(blist));
} finally {
close(entryStream);
}
} catch (IOException e) {
if (runtime.isDebug()) {
e.printStackTrace();
}
throw runtime.newIOErrorFromException(e);
}
}
private static void addEntries(ThreadContext context, ZipOutputStream zip, RubyHash entries) throws IOException {
RubyArray keys = entries.keys().sort(context, Block.NULL_BLOCK);
for (int i = 0; i < keys.getLength(); i++) {
IRubyObject key = keys.entry(i);
IRubyObject value = entries.op_aref(context, key);
addEntry(context, zip, key.convertToString().getUnicodeValue(), value);
}
}
private static void addEntry(ThreadContext context, ZipOutputStream zip, String entryName, IRubyObject value) throws IOException {
if (value.respondsTo("read")) {
RubyString str = (RubyString) value.callMethod(context, "read").checkStringType();
byte[] contents = str.getByteList().getUnsafeBytes();
zip.putNextEntry(new ZipEntry(entryName));
zip.write(contents);
} else {
File f;
if (value.isNil() || (f = getFile(value)).isDirectory()) {
zip.putNextEntry(new ZipEntry(entryName + "/"));
} else {
String path = f.getPath();
if (!f.exists()) {
path = value.convertToString().getUnicodeValue();
}
InputStream inFile = getStream(path, null);
try {
zip.putNextEntry(new ZipEntry(entryName));
byte[] buf = new byte[16384];
int bytesRead = -1;
while ((bytesRead = inFile.read(buf)) != -1) {
zip.write(buf, 0, bytesRead);
}
} finally {
close(inFile);
}
}
}
}
private static FileOutputStream newFile(IRubyObject jar_path) throws IOException {
return new FileOutputStream(getFile(jar_path));
}
private static File getFile(IRubyObject path) {
return JRubyFile.create(path.getRuntime().getCurrentDirectory(),
path.convertToString().getUnicodeValue());
}
private static void close(Closeable c) {
try {
c.close();
} catch (Exception e) {
}
}
private static Pattern PROTOCOL = Pattern.compile("^[a-z][a-z0-9]+:");
private static InputStream getStream(String jar, String entry) throws IOException {
Matcher m = PROTOCOL.matcher(jar);
while (m.find()) {
jar = jar.substring(m.end());
m = PROTOCOL.matcher(jar);
}
String[] path = jar.split("!/");
InputStream stream = new FileInputStream(path[0]);
for (int i = 1; i < path.length; i++) {
stream = entryInJar(stream, path[i]);
}
if (entry == null) {
return stream;
}
return entryInJar(stream, entry);
}
private static InputStream entryInJar(InputStream jar, String entry) throws IOException {
ZipInputStream jstream = new ZipInputStream(jar);
ZipEntry zentry = null;
while ((zentry = jstream.getNextEntry()) != null) {
if (zentry.getName().equals(entry)) {
return jstream;
}
jstream.closeEntry();
}
throw new FileNotFoundException("entry '" + entry + "' not found in " + jar);
}
}