• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      * Increases the reference count by {@code 1}.
50      */
retain()51     public abstract AllocatedBuffer retain();
52 
53     /**
54      * Decreases the reference count by {@code 1} and deallocates this object if the reference count
55      * reaches at {@code 0}.
56      *
57      * @return {@code true} if and only if the reference count became {@code 0} and this object has
58      * been deallocated
59      */
release()60     public abstract AllocatedBuffer release();
61 
62     /**
63      * Creates a new {@link AllocatedBuffer} that is backed by the given {@link ByteBuffer}.
64      */
wrap(final ByteBuffer buffer)65     public static AllocatedBuffer wrap(final ByteBuffer buffer) {
66         checkNotNull(buffer, "buffer");
67 
68         return new AllocatedBuffer() {
69 
70             @Override
71             public ByteBuffer nioBuffer() {
72                 return buffer;
73             }
74 
75             @Override
76             public AllocatedBuffer retain() {
77                 // Do nothing.
78                 return this;
79             }
80 
81             @Override
82             public AllocatedBuffer release() {
83                 // Do nothing.
84                 return this;
85             }
86         };
87     }
88 }
89