forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJarAsset.java
More file actions
61 lines (50 loc) · 1.31 KB
/
JarAsset.java
File metadata and controls
61 lines (50 loc) · 1.31 KB
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
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.internal;
import io.jooby.Asset;
import io.jooby.MediaType;
import io.jooby.SneakyThrows;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
public class JarAsset implements Asset {
private final ZipEntry entry;
private final JarFile jar;
public JarAsset(JarURLConnection connection) throws IOException {
connection.setUseCaches(false);
jar = connection.getJarFile();
entry = jar.getEntry(connection.getEntryName());
}
@Override public boolean isDirectory() {
return entry.isDirectory();
}
@Override public long getSize() {
return entry.getSize();
}
@Override public long getLastModified() {
return entry.getTime();
}
@Nonnull @Override public MediaType getContentType() {
return MediaType.byFile(entry.getName());
}
@Override public InputStream stream() {
try {
return jar.getInputStream(entry);
} catch (IOException x) {
throw SneakyThrows.propagate(x);
}
}
@Override public void close() {
try {
jar.close();
} catch (Exception x) {
// silence
}
}
}