-
-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathSender.java
More file actions
108 lines (98 loc) · 2.31 KB
/
Sender.java
File metadata and controls
108 lines (98 loc) · 2.31 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
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.jspecify.annotations.Nullable;
import io.jooby.output.Output;
/**
* Non-blocking sender. Reactive responses use this class to send partial data in a non-blocking
* manner.
*
* <p>RxJava example:
*
* <pre>{@code
* Sender sender = ctx.getSender();
*
* Flux.fromCallable(...)
* .subscribe(new Subscriber () {
*
* onSubscribe(Subscription s) {
* this.subscription = s;
* this.subscription.request(1);
* }
*
* onNext(Object next) {
* sender.write(next, (ctx, cause) -> {
* subscription.request(1);
* });
* }
*
* onError(Throwable error) {
* subscription.cancel();
* }
*
* onComplete() {
* sender.close();
* }
* })
*
* }</pre>
*
* @since 2.0.0
* @author edgar
*/
public interface Sender {
/** Write callback. */
interface Callback {
/**
* Callback after for <code>write</code> operation.
*
* @param ctx Web context.
* @param cause Cause in case of error or <code>null</code> for success.
*/
void onComplete(Context ctx, @Nullable Throwable cause);
}
/**
* Write a string chunk. Chunk is flushed immediately.
*
* @param data String chunk.
* @param callback Callback.
* @return This sender.
*/
default Sender write(String data, Callback callback) {
return write(data, StandardCharsets.UTF_8, callback);
}
/**
* Write a string chunk. Chunk is flushed immediately.
*
* @param data String chunk.
* @param charset Charset.
* @param callback Callback.
* @return This sender.
*/
default Sender write(String data, Charset charset, Callback callback) {
return write(data.getBytes(charset), callback);
}
/**
* Write a byte chunk. Chunk is flushed immediately.
*
* @param data Bytes chunk.
* @param callback Callback.
* @return This sender.
*/
Sender write(byte[] data, Callback callback);
/**
* Write an output.
*
* @param output Data.
* @param callback Callback.
* @return This sender.
*/
Sender write(Output output, Callback callback);
/** Close the sender. */
void close();
}