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.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   * @since 5.0
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 }