From f8a4549269d9df145a4f27c5368a2578f18128a7 Mon Sep 17 00:00:00 2001 From: jonmv Date: Sat, 5 Nov 2022 09:24:21 +0100 Subject: Revert "Merge pull request #24763 from vespa-engine/jonmv/revert-streams" This reverts commit 6d8bca79a1f600501290593ecd920eca0b237c78, reversing changes made to 36374eb2d3cc94c3792dd0a70963244abb6284b4. --- .../src/main/java/ai/vespa/hosted/api/MultiPartStreamer.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'hosted-api/src/main') 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 f1cbc027e17..c47fc60e58b 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,6 +15,7 @@ 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; @@ -89,10 +90,12 @@ 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(Collections.enumeration(Stream.concat(streams.stream().map(Supplier::get), - Stream.of(end())) - .collect(Collectors.toList()))); - + 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() : end(); } + }); try { if (aggregate.skip(2) != 2)// This should never happen, as the first stream is a ByteArrayInputStream. throw new IllegalStateException("Failed skipping extraneous bytes."); -- cgit v1.2.3 From a48b3b9e2cd688459ddf5143b303f9f929d667c2 Mon Sep 17 00:00:00 2001 From: jonmv Date: Mon, 7 Nov 2022 11:44:13 +0100 Subject: Clean up some more --- .../main/java/ai/vespa/hosted/api/MultiPartStreamer.java | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) (limited to 'hosted-api/src/main') 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 c47fc60e58b..b3862b76296 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 @@ -94,7 +94,7 @@ public class MultiPartStreamer { 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() : end(); } + @Override public InputStream nextElement() { return ++i < j ? streams.get(i).get() : i == j ? end() : null; } }); try { if (aggregate.skip(2) != 2)// This should never happen, as the first stream is a ByteArrayInputStream. @@ -116,17 +116,6 @@ 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 + "--"); @@ -143,7 +132,7 @@ public class MultiPartStreamer { return "\r\nContent-Type: " + contentType + "\r\n\r\n"; } - /** Returns the a ByteArrayInputStream over the given string, UTF-8 encoded. */ + /** Returns a ByteArrayInputStream over the given string, UTF-8 encoded. */ private static InputStream asStream(String string) { return new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8)); } -- cgit v1.2.3