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 abstract class SSLManagedBuffer { 35 36 /** 37 * Allocates the resources required for this buffer, or returns the resources already allocated for this buffer. 38 * Unless {@link #release() } is called, multiple invocations to this method must return the same 39 * {@link java.nio.ByteBuffer}. 40 * @return buffer 41 */ 42 abstract ByteBuffer acquire(); 43 /** 44 * Releases the resources for this buffer. If the buffer has already been released, this method does nothing. 45 */ 46 abstract void release(); 47 /** 48 * Tests to see if this buffer has been acquired. 49 * @return {@code true} if the buffer is acquired, otherwise {@code false} 50 */ 51 abstract boolean isAcquired(); 52 /** 53 * Tests to make sure that the buffer has been acquired and the underlying buffer has a position larger than 54 * {@code 0}. Essentially the same as {@code isAquired() && acquire().position > 0}. 55 * @return {@code true} if the buffer has been acquired and the underlying buffer's position is {@code > 0}, 56 * otherwise {@code false} 57 */ 58 abstract boolean hasData(); 59 60 static SSLManagedBuffer create(final SSLBufferMode mode, final int size) { 61 return mode == SSLBufferMode.DYNAMIC ? new DynamicBuffer(size) : new StaticBuffer(size); 62 } 63 64 static final class StaticBuffer extends SSLManagedBuffer { 65 66 private final ByteBuffer buffer; 67 68 public StaticBuffer(final int size) { 69 Args.positive(size, "size"); 70 buffer = ByteBuffer.allocate(size); 71 } 72 73 @Override 74 public ByteBuffer acquire() { 75 return buffer; 76 } 77 78 @Override 79 public void release() { 80 // do nothing 81 } 82 83 @Override 84 public boolean isAcquired() { 85 return true; 86 } 87 88 @Override 89 public boolean hasData() { 90 return buffer.position() > 0; 91 } 92 93 } 94 95 static final class DynamicBuffer extends SSLManagedBuffer { 96 97 private ByteBuffer wrapped; 98 private final int length; 99 100 public DynamicBuffer(final int size) { 101 Args.positive(size, "size"); 102 this.length = size; 103 } 104 105 @Override 106 public ByteBuffer acquire() { 107 if (wrapped != null) { 108 return wrapped; 109 } 110 wrapped = ByteBuffer.allocate(length); 111 return wrapped; 112 } 113 114 @Override 115 public void release() { 116 wrapped = null; 117 } 118 119 @Override 120 public boolean isAcquired() { 121 return wrapped != null; 122 } 123 124 @Override 125 public boolean hasData() { 126 return wrapped != null && wrapped.position() > 0; 127 } 128 129 } 130 131 }