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 package org.apache.hc.core5.http.nio.support.classic;
28
29 import java.util.concurrent.locks.Condition;
30 import java.util.concurrent.locks.ReentrantLock;
31
32 import org.apache.hc.core5.annotation.Contract;
33 import org.apache.hc.core5.annotation.ThreadingBehavior;
34 import org.apache.hc.core5.http.impl.nio.ExpandableBuffer;
35 import org.apache.hc.core5.util.Args;
36
37
38
39
40 @Contract(threading = ThreadingBehavior.SAFE)
41 abstract class AbstractSharedBuffer extends ExpandableBuffer {
42
43 final ReentrantLock lock;
44 final Condition condition;
45
46 volatile boolean endStream;
47 volatile boolean aborted;
48
49 public AbstractSharedBuffer(final ReentrantLock lock, final int initialBufferSize) {
50 super(initialBufferSize);
51 this.lock = Args.notNull(lock, "Lock");
52 this.condition = lock.newCondition();
53 }
54
55 @Override
56 public boolean hasData() {
57 lock.lock();
58 try {
59 return super.hasData();
60 } finally {
61 lock.unlock();
62 }
63 }
64
65 @Override
66 public int capacity() {
67 lock.lock();
68 try {
69 return super.capacity();
70 } finally {
71 lock.unlock();
72 }
73 }
74
75 @Override
76 public int length() {
77 lock.lock();
78 try {
79 return super.length();
80 } finally {
81 lock.unlock();
82 }
83 }
84
85 public void abort() {
86 lock.lock();
87 try {
88 endStream = true;
89 aborted = true;
90 condition.signalAll();
91 } finally {
92 lock.unlock();
93 }
94 }
95
96 public void reset() {
97 if (aborted) {
98 return;
99 }
100 lock.lock();
101 try {
102 setInputMode();
103 buffer().clear();
104 endStream = false;
105 } finally {
106 lock.unlock();
107 }
108 }
109
110 public boolean isEndStream() {
111 lock.lock();
112 try {
113 return endStream && !super.hasData();
114 } finally {
115 lock.unlock();
116 }
117 }
118
119 }