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