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  
28  package org.apache.hc.core5.reactor.ssl;
29  
30  import java.nio.ByteBuffer;
31  
32  import org.apache.hc.core5.util.Args;
33  
34  /**
35   * @since 5.0
36   */
37  public abstract class SSLBufferManagement {
38  
39      public static final SSLBufferManagement STATIC = new StaticSSLBufferManagement();
40      public static final SSLBufferManagement DYNAMIC = new DynamicSSLBufferManagement();
41  
42      public abstract SSLBuffer create(int size);
43  
44      static SSLBuffer create(final SSLBufferManagement mode, final int size) {
45          if (mode == null) {
46              return STATIC.create(size);
47          }
48          return mode.create(size);
49      }
50  
51      private static final class StaticSSLBufferManagement extends SSLBufferManagement {
52  
53          @Override
54          public SSLBuffer create(final int size) {
55              return new StaticBuffer(size);
56          }
57  
58      }
59  
60      private static final class StaticBuffer implements SSLBuffer {
61  
62          private final ByteBuffer buffer;
63  
64          public StaticBuffer(final int size) {
65              Args.positive(size, "size");
66              buffer = ByteBuffer.allocate(size);
67          }
68  
69          @Override
70          public ByteBuffer acquire() {
71              return buffer;
72          }
73  
74          @Override
75          public void release() {
76              // do nothing
77          }
78  
79          @Override
80          public boolean isAcquired() {
81              return true;
82          }
83  
84          @Override
85          public boolean hasData() {
86              return buffer.position() > 0;
87          }
88  
89      }
90  
91      private static final class DynamicSSLBufferManagement extends SSLBufferManagement {
92  
93          @Override
94          public SSLBuffer create(final int size) {
95              return new DynamicBuffer(size);
96          }
97  
98      }
99  
100     private static final class DynamicBuffer implements SSLBuffer {
101 
102         private ByteBuffer wrapped;
103         private final int length;
104 
105         public DynamicBuffer(final int size) {
106             Args.positive(size, "size");
107             this.length = size;
108         }
109 
110         @Override
111         public ByteBuffer acquire() {
112             if (wrapped != null) {
113                 return wrapped;
114             }
115             wrapped = ByteBuffer.allocate(length);
116             return wrapped;
117         }
118 
119         @Override
120         public void release() {
121             wrapped = null;
122         }
123 
124         @Override
125         public boolean isAcquired() {
126             return wrapped != null;
127         }
128 
129         @Override
130         public boolean hasData() {
131             return wrapped != null && wrapped.position() > 0;
132         }
133 
134     }
135 
136 }