View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.impl.compat;
28  
29  import java.util.concurrent.locks.Condition;
30  import java.util.concurrent.locks.ReentrantLock;
31  
32  import org.apache.hc.core5.annotation.Internal;
33  import org.apache.hc.core5.http.impl.nio.ExpandableBuffer;
34  import org.apache.hc.core5.util.Args;
35  
36  /**
37   * TODO: to be replaced by core functionality
38   */
39  @Internal
40  abstract class AbstractSharedBuffer extends ExpandableBuffer {
41  
42      final ReentrantLock lock;
43      final Condition condition;
44  
45      volatile boolean endStream;
46      volatile boolean aborted;
47  
48      public AbstractSharedBuffer(final ReentrantLock lock, final int initialBufferSize) {
49          super(initialBufferSize);
50          this.lock = Args.notNull(lock, "Lock");
51          this.condition = lock.newCondition();
52      }
53  
54      @Override
55      public boolean hasData() {
56          lock.lock();
57          try {
58              return super.hasData();
59          } finally {
60              lock.unlock();
61          }
62      }
63  
64      @Override
65      public int capacity() {
66          lock.lock();
67          try {
68              return super.capacity();
69          } finally {
70              lock.unlock();
71          }
72      }
73  
74      @Override
75      public int length() {
76          lock.lock();
77          try {
78              return super.length();
79          } finally {
80              lock.unlock();
81          }
82      }
83  
84      public void abort() {
85          lock.lock();
86          try {
87              endStream = true;
88              aborted = true;
89              condition.signalAll();
90          } finally {
91              lock.unlock();
92          }
93      }
94  
95      public void reset() {
96          if (aborted) {
97              return;
98          }
99          lock.lock();
100         try {
101             setInputMode();
102             buffer().clear();
103             endStream = false;
104         } finally {
105             lock.unlock();
106         }
107     }
108 
109     public boolean isEndStream() {
110         lock.lock();
111         try {
112             return endStream && !super.hasData();
113         } finally {
114             lock.unlock();
115         }
116     }
117 
118 }