1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * Copyright 2013 The Netty Project 19 * 20 * The Netty Project licenses this file to you under the Apache License, 21 * version 2.0 (the "License"); you may not use this file except in compliance 22 * with the License. You may obtain a copy of the License at: 23 * 24 * http://www.apache.org/licenses/LICENSE-2.0 25 * 26 * Unless required by applicable law or agreed to in writing, software 27 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 28 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 29 * License for the specific language governing permissions and limitations 30 * under the License. 31 */ 32 package org.conscrypt; 33 34 import static org.conscrypt.Preconditions.checkNotNull; 35 36 import java.nio.ByteBuffer; 37 38 /** 39 * A buffer that was allocated by a {@link BufferAllocator}. 40 */ 41 @ExperimentalApi 42 public abstract class AllocatedBuffer { 43 /** 44 * Returns the {@link ByteBuffer} that backs this buffer. 45 */ nioBuffer()46 public abstract ByteBuffer nioBuffer(); 47 48 /** 49 * Returns the current instance for backward compatibility. 50 * 51 * @deprecated this method is not used 52 */ 53 @Deprecated retain()54 public AllocatedBuffer retain() { 55 // Do nothing. 56 return this; 57 } 58 59 /** 60 * Returns the delegate {@link ByteBuffer} for reuse or garbage collection. 61 * 62 * @return this {@link AllocatedBuffer} instance 63 */ release()64 public abstract AllocatedBuffer release(); 65 66 /** 67 * Creates a new {@link AllocatedBuffer} that is backed by the given {@link ByteBuffer}. 68 */ wrap(final ByteBuffer buffer)69 public static AllocatedBuffer wrap(final ByteBuffer buffer) { 70 checkNotNull(buffer, "buffer"); 71 72 return new AllocatedBuffer() { 73 74 @Override 75 public ByteBuffer nioBuffer() { 76 return buffer; 77 } 78 79 @Override 80 public AllocatedBuffer release() { 81 // Do nothing. 82 return this; 83 } 84 }; 85 } 86 } 87