-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathEmptyPublisher.java
More file actions
45 lines (38 loc) · 1.22 KB
/
EmptyPublisher.java
File metadata and controls
45 lines (38 loc) · 1.22 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
package com.canary;
import java.nio.ByteBuffer;
import java.util.Optional;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import software.amazon.awssdk.http.async.SdkHttpContentPublisher;
public class EmptyPublisher implements SdkHttpContentPublisher {
@Override
public void subscribe(Subscriber<? super ByteBuffer> subscriber) {
subscriber.onSubscribe(new EmptySubscription(subscriber));
}
@Override
public Optional<Long> contentLength() {
return Optional.of(0L);
}
private static class EmptySubscription implements Subscription {
private final Subscriber subscriber;
private volatile boolean done;
EmptySubscription(Subscriber subscriber) {
this.subscriber = subscriber;
}
@Override
public void request(long l) {
if (!done) {
done = true;
if (l <= 0) {
this.subscriber.onError(new IllegalArgumentException("Demand must be positive"));
} else {
this.subscriber.onComplete();
}
}
}
@Override
public void cancel() {
done = true;
}
}
}