jdk/src/java.httpclient/share/classes/java/net/http/PingFrame.java
author vinnie
Mon, 15 Aug 2016 14:25:51 +0100
changeset 40276 2217f830ca7b
parent 39730 196f4e25d9f5
permissions -rw-r--r--
8163503: PKCS12 keystore cannot store non-X.509 certificates Reviewed-by: weijun, xuelei, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     1
/*
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     4
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    10
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    15
 * accompanied this code).
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    16
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    20
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
39730
196f4e25d9f5 8160993: Fix headers in the java/net/http package
chegar
parents: 37720
diff changeset
    23
 * questions.
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    24
 */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    25
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    26
package java.net.http;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    27
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    28
import java.io.IOException;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    29
import java.nio.ByteBuffer;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    30
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    31
class PingFrame extends Http2Frame {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    32
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    33
    PingFrame() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    34
        type = TYPE;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    35
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    36
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    37
    byte[] data;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    38
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    39
    public final static int TYPE = 0x6;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    40
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    41
    // Flags
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    42
    public static final int ACK = 0x1;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    43
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    44
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    45
    String flagAsString(int flag) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    46
        switch (flag) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    47
        case ACK:
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    48
            return "ACK";
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    49
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    50
        return super.flagAsString(flag);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    51
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    52
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    53
    public void setData(byte[] data) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    54
        if (data.length != 8) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    55
            throw new IllegalArgumentException("Ping data not 8 bytes");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    56
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    57
        this.data = data;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    58
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    59
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    60
    public byte[] getData() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    61
        return data;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    62
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    63
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    64
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    65
    void readIncomingImpl(ByteBufferConsumer bc) throws IOException {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    66
        if (length != 8) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    67
            throw new IOException("Invalid Ping frame");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    68
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    69
        data = bc.getBytes(8);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    70
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    71
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    72
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    73
    void writeOutgoing(ByteBufferGenerator bg) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    74
        if (data == null) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    75
            data = new byte[] {0, 0, 0, 0, 0 ,0, 0, 0};
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    76
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    77
        super.writeOutgoing(bg);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    78
        ByteBuffer buf = bg.getBuffer(8);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    79
        buf.put(data);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    80
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    81
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    82
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    83
    void computeLength() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    84
        length = 8;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    85
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    86
}