src/java.net.http/share/classes/jdk/internal/net/http/common/BufferSupplier.java
branchhttp-client-branch
changeset 56474 fe2bf7b369b8
equal deleted inserted replaced
56463:b583caf69b39 56474:fe2bf7b369b8
       
     1 /*
       
     2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package jdk.internal.net.http.common;
       
    26 
       
    27 import java.nio.ByteBuffer;
       
    28 import java.util.concurrent.ConcurrentLinkedQueue;
       
    29 import java.util.function.Supplier;
       
    30 
       
    31 /**
       
    32  *  This interface allows to recycle buffers used for SSL decryption.
       
    33  *  Buffers that are used for reading SSL encrypted data are typically
       
    34  *  very short lived, as it is necessary to aggregate their content
       
    35  *  before calling SSLEngine::unwrap.
       
    36  *  Because both reading and copying happen in the SelectorManager
       
    37  *  thread, then it makes it possible to pool these buffers and
       
    38  *  recycle them for the next socket read, instead of simply
       
    39  *  letting them be GC'ed. That also makes it possible to use
       
    40  *  direct byte buffers, and avoid another layer of copying by
       
    41  *  the SocketChannel implementation.
       
    42  *
       
    43  *  The HttpClientImpl has an implementation of this interface
       
    44  *  that allows to reuse the same 3 direct buffers for reading
       
    45  *  off SSL encrypted data from the socket.
       
    46  *  The BufferSupplier::get method is called by SocketTube
       
    47  *  (see SocketTube.SSLDirectBufferSource) and BufferSupplier::recycle
       
    48  *  is called by SSLFlowDelegate.Reader.
       
    49  **/
       
    50 public interface BufferSupplier extends Supplier<ByteBuffer> {
       
    51     /**
       
    52      * Returns a buffer to read encrypted data off the socket.
       
    53      * @return a buffer to read encrypted data off the socket.
       
    54      */
       
    55     ByteBuffer get();
       
    56 
       
    57     /**
       
    58      * Returns a buffer to the pool.
       
    59      *
       
    60      * @param buffer This must be a buffer previously obtained
       
    61      *               by calling BufferSupplier::get. The caller must
       
    62      *               not touch the buffer after returning it to
       
    63      *               the pool.
       
    64      */
       
    65     void recycle(ByteBuffer buffer);
       
    66 }
       
    67