src/java.net.http/share/classes/jdk/internal/net/http/hpack/HPACK.java
author rehn
Tue, 21 May 2019 10:34:57 +0200
changeset 54955 46409371a691
parent 50681 4254bed3c09d
child 56795 03ece2518428
permissions -rw-r--r--
8223306: Remove threads linked list (use ThreadsList's array in SA) Reviewed-by: coleenp, dholmes, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     1
/*
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
     2
 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     4
 *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    10
 *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    15
 * accompanied this code).
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    16
 *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    20
 *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    23
 * questions.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    24
 */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    25
package jdk.internal.net.http.hpack;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    26
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    27
import jdk.internal.net.http.common.Utils;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    28
import jdk.internal.net.http.hpack.HPACK.Logger.Level;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    29
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
    30
import java.nio.ByteBuffer;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    31
import java.security.AccessController;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    32
import java.security.PrivilegedAction;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    33
import java.util.Map;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    34
import java.util.ResourceBundle;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    35
import java.util.function.Supplier;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    36
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    37
import static java.lang.String.format;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    38
import static java.util.stream.Collectors.joining;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    39
import static jdk.internal.net.http.hpack.HPACK.Logger.Level.EXTRA;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    40
import static jdk.internal.net.http.hpack.HPACK.Logger.Level.NONE;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    41
import static jdk.internal.net.http.hpack.HPACK.Logger.Level.NORMAL;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    42
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    43
/**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    44
 * Internal utilities and stuff.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    45
 */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    46
public final class HPACK {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    47
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    48
    private static final RootLogger LOGGER;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    49
    private static final Map<String, Level> logLevels =
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    50
            Map.of("NORMAL", NORMAL, "EXTRA", EXTRA);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    51
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    52
    static {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    53
        String PROPERTY = "jdk.internal.httpclient.hpack.log.level";
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    54
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    55
        String value = AccessController.doPrivileged(
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    56
                (PrivilegedAction<String>) () -> System.getProperty(PROPERTY));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    57
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    58
        if (value == null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    59
            LOGGER = new RootLogger(NONE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    60
        } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    61
            String upperCasedValue = value.toUpperCase();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    62
            Level l = logLevels.get(upperCasedValue);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    63
            if (l == null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    64
                LOGGER = new RootLogger(NONE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    65
                LOGGER.log(System.Logger.Level.INFO,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    66
                        () -> format("%s value '%s' not recognized (use %s); logging disabled",
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    67
                                     PROPERTY, value, logLevels.keySet().stream().collect(joining(", "))));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    68
            } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    69
                LOGGER = new RootLogger(l);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    70
                LOGGER.log(System.Logger.Level.DEBUG,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    71
                        () -> format("logging level %s", l));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    72
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    73
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    74
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    75
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    76
    public static Logger getLogger() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    77
        return LOGGER;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    78
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    79
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    80
    private HPACK() { }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    81
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    82
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    83
     * The purpose of this logger is to provide means of diagnosing issues _in
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    84
     * the HPACK implementation_. It's not a general purpose logger.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    85
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    86
    // implements System.Logger to make it possible to skip this class
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    87
    // when looking for the Caller.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    88
    public static class Logger implements System.Logger {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    89
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    90
        /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    91
         * Log detail level.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    92
         */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    93
        public enum Level {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    94
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    95
            NONE(0, System.Logger.Level.OFF),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    96
            NORMAL(1, System.Logger.Level.DEBUG),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    97
            EXTRA(2, System.Logger.Level.TRACE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    98
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    99
            private final int level;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   100
            final System.Logger.Level systemLevel;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   101
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   102
            Level(int i, System.Logger.Level system) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   103
                level = i;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   104
                systemLevel = system;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   105
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   106
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   107
            public final boolean implies(Level other) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   108
                return this.level >= other.level;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   109
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   110
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   111
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   112
        private final String name;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   113
        private final Level level;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   114
        private final String path;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   115
        private final System.Logger logger;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   116
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   117
        private Logger(String path, String name, Level level) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   118
            this(path, name, level, null);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   119
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   120
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   121
        private Logger(String p, String name, Level level, System.Logger logger) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   122
            this.path = p;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   123
            this.name = name;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   124
            this.level = level;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   125
            this.logger = Utils.getHpackLogger(path::toString, level.systemLevel);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   126
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   127
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   128
        public final String getName() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   129
            return name;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   130
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   131
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   132
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   133
        public boolean isLoggable(System.Logger.Level level) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   134
            return logger.isLoggable(level);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   135
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   136
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   137
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   138
        public void log(System.Logger.Level level, ResourceBundle bundle, String msg, Throwable thrown) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   139
            logger.log(level, bundle, msg,thrown);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   140
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   141
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   142
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   143
        public void log(System.Logger.Level level, ResourceBundle bundle, String format, Object... params) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   144
            logger.log(level, bundle, format, params);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   145
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   146
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   147
        /*
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   148
         * Usual performance trick for logging, reducing performance overhead in
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   149
         * the case where logging with the specified level is a NOP.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   150
         */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   151
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   152
        public boolean isLoggable(Level level) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   153
            return this.level.implies(level);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   154
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   155
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   156
        public void log(Level level, Supplier<String> s) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   157
            if (this.level.implies(level)) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   158
                logger.log(level.systemLevel, s);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   159
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   160
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   161
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   162
        public Logger subLogger(String name) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   163
            return new Logger(path + "/" + name, name, level);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   164
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   165
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   166
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   167
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   168
    private static final class RootLogger extends Logger {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   169
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   170
        protected RootLogger(Level level) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   171
            super("hpack", "hpack", level);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   172
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   173
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   174
    }
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   175
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   176
    // -- low-level utilities --
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   177
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   178
    @FunctionalInterface
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   179
    interface BufferUpdateConsumer {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   180
        void accept(long data, int len);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   181
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   182
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   183
    @SuppressWarnings("fallthrough")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   184
    public static int read(ByteBuffer source,
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   185
                           long buffer,
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   186
                           int bufferLen,
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   187
                           BufferUpdateConsumer consumer)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   188
    {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   189
        // read as much as possible (up to 8 bytes)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   190
        int nBytes = Math.min((64 - bufferLen) >> 3, source.remaining());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   191
        switch (nBytes) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   192
            case 0:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   193
                break;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   194
            case 3:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   195
                buffer |= ((source.get() & 0x00000000000000ffL) << (56 - bufferLen));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   196
                bufferLen += 8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   197
            case 2:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   198
                buffer |= ((source.get() & 0x00000000000000ffL) << (56 - bufferLen));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   199
                bufferLen += 8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   200
            case 1:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   201
                buffer |= ((source.get() & 0x00000000000000ffL) << (56 - bufferLen));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   202
                bufferLen += 8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   203
                consumer.accept(buffer, bufferLen);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   204
                break;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   205
            case 7:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   206
                buffer |= ((source.get() & 0x00000000000000ffL) << (56 - bufferLen));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   207
                bufferLen += 8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   208
            case 6:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   209
                buffer |= ((source.get() & 0x00000000000000ffL) << (56 - bufferLen));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   210
                bufferLen += 8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   211
            case 5:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   212
                buffer |= ((source.get() & 0x00000000000000ffL) << (56 - bufferLen));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   213
                bufferLen += 8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   214
            case 4:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   215
                buffer |= ((source.getInt() & 0x00000000ffffffffL) << (32 - bufferLen));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   216
                bufferLen += 32;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   217
                consumer.accept(buffer, bufferLen);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   218
                break;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   219
            case 8:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   220
                buffer = source.getLong();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   221
                bufferLen = 64;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   222
                consumer.accept(buffer, bufferLen);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   223
                break;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   224
            default:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   225
                throw new InternalError(String.valueOf(nBytes));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   226
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   227
        return nBytes;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   228
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   229
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   230
    // The number of bytes that can be written at once
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   231
    // (calculating in bytes, not bits, since
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   232
    //  destination.remaining() * 8 might overflow)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   233
    @SuppressWarnings("fallthrough")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   234
    public static int write(long buffer,
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   235
                            int bufferLen,
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   236
                            BufferUpdateConsumer consumer,
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   237
                            ByteBuffer destination)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   238
    {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   239
        int nBytes = Math.min(bufferLen >> 3, destination.remaining());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   240
        switch (nBytes) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   241
            case 0:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   242
                break;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   243
            case 3:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   244
                destination.put((byte) (buffer >>> 56));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   245
                buffer <<= 8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   246
                bufferLen -= 8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   247
            case 2:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   248
                destination.put((byte) (buffer >>> 56));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   249
                buffer <<= 8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   250
                bufferLen -= 8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   251
            case 1:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   252
                destination.put((byte) (buffer >>> 56));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   253
                buffer <<= 8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   254
                bufferLen -= 8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   255
                consumer.accept(buffer, bufferLen);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   256
                break;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   257
            case 7:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   258
                destination.put((byte) (buffer >>> 56));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   259
                buffer <<= 8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   260
                bufferLen -= 8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   261
            case 6:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   262
                destination.put((byte) (buffer >>> 56));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   263
                buffer <<= 8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   264
                bufferLen -= 8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   265
            case 5:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   266
                destination.put((byte) (buffer >>> 56));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   267
                buffer <<= 8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   268
                bufferLen -= 8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   269
            case 4:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   270
                destination.putInt((int) (buffer >>> 32));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   271
                buffer <<= 32;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   272
                bufferLen -= 32;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   273
                consumer.accept(buffer, bufferLen);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   274
                break;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   275
            case 8:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   276
                destination.putLong(buffer);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   277
                buffer = 0;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   278
                bufferLen = 0;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   279
                consumer.accept(buffer, bufferLen);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   280
                break;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   281
            default:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   282
                throw new InternalError(String.valueOf(nBytes));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   283
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   284
        return nBytes;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   285
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   286
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   287
    /*
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   288
     * Returns the number of bytes the given number of bits constitute.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   289
     */
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   290
    static int bytesForBits(int n) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   291
        assert (n / 8 + (n % 8 != 0 ? 1 : 0)) == (n + 7) / 8
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   292
                && (n + 7) / 8 == ((n + 7) >> 3) : n;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   293
        return (n + 7) >> 3;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   294
    }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   295
}