-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResultStream.java
More file actions
117 lines (103 loc) · 3.95 KB
/
ResultStream.java
File metadata and controls
117 lines (103 loc) · 3.95 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
106
107
108
109
110
111
112
113
114
115
116
117
package dev.arcp.client;
import dev.arcp.core.events.ResultChunkEvent;
import dev.arcp.core.ids.ResultId;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.TreeMap;
import org.jspecify.annotations.Nullable;
/**
* §8.4 result_chunk reassembler. Chunks may arrive out of order (the spec mandates emission order,
* but processing order at the subscriber is not guaranteed); we hold pending chunks in a {@link
* TreeMap} and flush them when their predecessor arrives.
*/
public final class ResultStream {
public static final class OutOfOrderChunkException extends RuntimeException {
public OutOfOrderChunkException(String message) {
super(message);
}
}
public static final class DuplicateChunkException extends RuntimeException {
public DuplicateChunkException(long chunkSeq) {
super("duplicate chunk_seq " + chunkSeq);
}
}
public static final class EncodingMismatchException extends RuntimeException {
public EncodingMismatchException(String first, String later) {
super("encoding switched mid-stream: " + first + " -> " + later);
}
}
private final @Nullable ResultId resultId;
private final OutputStream sink;
private final TreeMap<Long, ResultChunkEvent> pending = new TreeMap<>();
private long nextExpected;
private long bytesWritten;
private boolean closed;
private @Nullable String encoding;
private ResultStream(@Nullable ResultId resultId, OutputStream sink) {
this.resultId = resultId;
this.sink = sink;
}
/** In-memory sink; {@link #bytes()} returns the assembled output. */
public static ResultStream toMemory(@Nullable ResultId resultId) {
return new ResultStream(resultId, new ByteArrayOutputStream());
}
/** Stream chunks directly to an arbitrary {@link OutputStream}. */
public static ResultStream toSink(@Nullable ResultId resultId, OutputStream sink) {
return new ResultStream(resultId, sink);
}
public synchronized void accept(ResultChunkEvent chunk) throws IOException {
if (closed) {
throw new IllegalStateException("ResultStream already closed");
}
if (resultId != null && !resultId.equals(chunk.resultId())) {
throw new IllegalArgumentException(
"chunk for wrong result_id: " + chunk.resultId() + " != " + resultId);
}
if (encoding == null) {
encoding = chunk.encoding();
} else if (!encoding.equals(chunk.encoding())) {
throw new EncodingMismatchException(encoding, chunk.encoding());
}
if (chunk.chunkSeq() < nextExpected) {
throw new DuplicateChunkException(chunk.chunkSeq());
}
pending.put(chunk.chunkSeq(), chunk);
while (pending.containsKey(nextExpected)) {
ResultChunkEvent ready = pending.remove(nextExpected);
byte[] decoded = decode(ready);
sink.write(decoded);
bytesWritten += decoded.length;
nextExpected++;
if (!ready.more()) {
if (!pending.isEmpty()) {
throw new OutOfOrderChunkException("chunks beyond terminal: " + pending.keySet());
}
closed = true;
sink.flush();
}
}
}
private static byte[] decode(ResultChunkEvent chunk) {
return switch (chunk.encoding()) {
case ResultChunkEvent.UTF8 -> chunk.data().getBytes(StandardCharsets.UTF_8);
case ResultChunkEvent.BASE64 -> Base64.getDecoder().decode(chunk.data());
default -> throw new IllegalStateException("unsupported encoding: " + chunk.encoding());
};
}
public synchronized boolean isComplete() {
return closed;
}
public synchronized long bytesWritten() {
return bytesWritten;
}
/** Assembled bytes from an in-memory stream; throws if a non-memory sink is in use. */
public synchronized byte[] bytes() {
if (!(sink instanceof ByteArrayOutputStream baos)) {
throw new IllegalStateException("ResultStream sink is not in-memory");
}
return baos.toByteArray();
}
}