forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSender.java
More file actions
103 lines (96 loc) · 2.29 KB
/
Sender.java
File metadata and controls
103 lines (96 loc) · 2.29 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
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
* Non-blocking sender. Reactive responses uses this class to send partial data in non-blocking
* manner.
*
* 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(@Nonnull Context ctx, @Nullable Throwable cause);
}
/**
* Write a string chunk. Chunk is flushed immediately.
*
* @param data String chunk.
* @param callback Callback.
* @return This sender.
*/
@Nonnull default Sender write(@Nonnull String data, @Nonnull 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.
*/
@Nonnull default Sender write(@Nonnull String data, @Nonnull Charset charset,
@Nonnull Callback callback) {
return write(data.getBytes(charset), callback);
}
/**
* Write a bytes chunk. Chunk is flushed immediately.
*
* @param data Bytes chunk.
* @param callback Callback.
* @return This sender.
*/
@Nonnull Sender write(@Nonnull byte[] data, @Nonnull Callback callback);
/**
* Close the sender.
*/
void close();
}