-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceSource.java
More file actions
105 lines (92 loc) · 3.14 KB
/
Copy pathResourceSource.java
File metadata and controls
105 lines (92 loc) · 3.14 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
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
package com.retailsvc.http.internal;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;
import java.util.Objects;
/**
* A streamable, fail-fast handle to a resource on the classpath or filesystem. Existence and length
* are resolved at construction; the underlying bytes are not buffered. Each {@link #open()} call
* returns a fresh {@link InputStream} that the caller must close.
*/
public sealed interface ResourceSource {
long length();
String contentType();
String describe();
InputStream open() throws IOException;
static ResourceSource ofClasspath(String classpathResource) {
Objects.requireNonNull(classpathResource, "classpathResource");
long length;
try (InputStream in = ResourceSource.class.getResourceAsStream(classpathResource)) {
if (in == null) {
throw new IllegalArgumentException("classpath resource not found: " + classpathResource);
}
length = in.transferTo(OutputStream.nullOutputStream());
} catch (IOException io) {
throw new IllegalArgumentException(
"failed reading classpath resource: " + classpathResource, io);
}
return new Classpath(classpathResource, length, contentTypeFor(classpathResource));
}
static ResourceSource ofFile(Path file) {
Objects.requireNonNull(file, "file");
if (!Files.isRegularFile(file)) {
throw new IllegalArgumentException("file not found or not a regular file: " + file);
}
long length;
try {
length = Files.size(file);
} catch (IOException io) {
throw new IllegalArgumentException("failed reading file: " + file, io);
}
return new File(file, length, contentTypeFor(file.getFileName().toString()));
}
static String contentTypeFor(String path) {
String lower = path.toLowerCase(Locale.ROOT);
if (lower.endsWith(".json")) {
return "application/json";
}
if (lower.endsWith(".yaml") || lower.endsWith(".yml")) {
return "application/yaml";
}
if (lower.endsWith(".html") || lower.endsWith(".htm")) {
return "text/html; charset=utf-8";
}
if (lower.endsWith(".css")) {
return "text/css; charset=utf-8";
}
if (lower.endsWith(".js")) {
return "text/javascript; charset=utf-8";
}
if (lower.endsWith(".txt")) {
return "text/plain; charset=utf-8";
}
return "application/octet-stream";
}
record Classpath(String path, long length, String contentType) implements ResourceSource {
@Override
public InputStream open() throws IOException {
InputStream in = ResourceSource.class.getResourceAsStream(path);
if (in == null) {
throw new IOException("classpath resource disappeared: " + path);
}
return in;
}
@Override
public String describe() {
return "classpath:" + path;
}
}
record File(Path path, long length, String contentType) implements ResourceSource {
@Override
public InputStream open() throws IOException {
return Files.newInputStream(path);
}
@Override
public String describe() {
return path.toString();
}
}
}