forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileBody.java
More file actions
98 lines (82 loc) · 2.21 KB
/
FileBody.java
File metadata and controls
98 lines (82 loc) · 2.21 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
/**
* 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.Body;
import io.jooby.Context;
import io.jooby.MediaType;
import io.jooby.SneakyThrows;
import io.jooby.ValueNode;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public class FileBody implements Body {
private Context ctx;
private Path file;
public FileBody(Context ctx, Path file) {
this.ctx = ctx;
this.file = file;
}
@Override public long getSize() {
try {
return Files.size(file);
} catch (IOException x) {
throw SneakyThrows.propagate(x);
}
}
@Override public boolean isInMemory() {
return false;
}
@Override public ReadableByteChannel channel() {
try {
return Files.newByteChannel(file);
} catch (IOException x) {
throw SneakyThrows.propagate(x);
}
}
@Override public InputStream stream() {
try {
return Files.newInputStream(file);
} catch (IOException x) {
throw SneakyThrows.propagate(x);
}
}
@Override public byte[] bytes() {
try {
return Files.readAllBytes(file);
} catch (IOException x) {
throw SneakyThrows.propagate(x);
}
}
@Nonnull @Override public String value() {
return value(StandardCharsets.UTF_8);
}
@Nonnull @Override public List<String> toList() {
return Collections.singletonList(value());
}
@Nonnull @Override public ValueNode get(@Nonnull int index) {
return index == 0 ? this : get(Integer.toString(index));
}
@Nonnull @Override public ValueNode get(@Nonnull String name) {
return new MissingValue(name);
}
@Override public String name() {
return "body";
}
@Nonnull @Override public <T> T to(@Nonnull Type type) {
return ctx.decode(type, ctx.getRequestType(MediaType.text));
}
@Override public Map<String, List<String>> toMultimap() {
return Collections.emptyMap();
}
}