aboutsummaryrefslogtreecommitdiffstats
path: root/hosted-api
diff options
context:
space:
mode:
authorJon Marius Venstad <jonmv@users.noreply.github.com>2022-11-07 15:08:46 +0100
committerGitHub <noreply@github.com>2022-11-07 15:08:46 +0100
commita0a5b2d3c7cb9319590821bb1beb824dc25d1b89 (patch)
tree4e1d28d33c02a86c179c94fda29158b4ada9d2c9 /hosted-api
parenta0e9b51aaf01a425dbd6cdd1fbc8fd5ce066a9bb (diff)
Revert "Jonmv/reapply app streams"
Diffstat (limited to 'hosted-api')
-rw-r--r--hosted-api/src/main/java/ai/vespa/hosted/api/MultiPartStreamer.java24
1 files changed, 16 insertions, 8 deletions
diff --git a/hosted-api/src/main/java/ai/vespa/hosted/api/MultiPartStreamer.java b/hosted-api/src/main/java/ai/vespa/hosted/api/MultiPartStreamer.java
index b3862b76296..f1cbc027e17 100644
--- a/hosted-api/src/main/java/ai/vespa/hosted/api/MultiPartStreamer.java
+++ b/hosted-api/src/main/java/ai/vespa/hosted/api/MultiPartStreamer.java
@@ -15,7 +15,6 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
-import java.util.Enumeration;
import java.util.List;
import java.util.UUID;
import java.util.function.Supplier;
@@ -90,12 +89,10 @@ public class MultiPartStreamer {
/** Returns an input stream which is an aggregate of all current parts in this, plus an end marker. */
public InputStream data() {
- InputStream aggregate = new SequenceInputStream(new Enumeration<>() {
- final int j = streams.size();
- int i = -1;
- @Override public boolean hasMoreElements() { return i < j; }
- @Override public InputStream nextElement() { return ++i < j ? streams.get(i).get() : i == j ? end() : null; }
- });
+ InputStream aggregate = new SequenceInputStream(Collections.enumeration(Stream.concat(streams.stream().map(Supplier::get),
+ Stream.of(end()))
+ .collect(Collectors.toList())));
+
try {
if (aggregate.skip(2) != 2)// This should never happen, as the first stream is a ByteArrayInputStream.
throw new IllegalStateException("Failed skipping extraneous bytes.");
@@ -116,6 +113,17 @@ public class MultiPartStreamer {
return asStream(disposition(name) + (filename == null ? "" : "; filename=\"" + filename + "\"") + type(contentType));
}
+ /** Returns the separator to put between one part and the next, when this is a file. */
+ private InputStream separator(String name, Path path) {
+ try {
+ String contentType = Files.probeContentType(path);
+ return separator(name, path.getFileName().toString(), contentType != null ? contentType : "application/octet-stream");
+ }
+ catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+
/** Returns the end delimiter of the request, with line breaks prepended. */
private InputStream end() {
return asStream("\r\n--" + boundary + "--");
@@ -132,7 +140,7 @@ public class MultiPartStreamer {
return "\r\nContent-Type: " + contentType + "\r\n\r\n";
}
- /** Returns a ByteArrayInputStream over the given string, UTF-8 encoded. */
+ /** Returns the a ByteArrayInputStream over the given string, UTF-8 encoded. */
private static InputStream asStream(String string) {
return new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8));
}