src/java.net.http/share/classes/jdk/internal/net/http/frame/Http2Frame.java
author chegar
Wed, 18 Apr 2018 12:03:06 +0100
branchhttp-client-branch
changeset 56451 9585061fdb04
parent 56092 fd85b2bf2b0d
parent 49765 ee6f7a61f3a5
permissions -rw-r--r--
http-client-branch: Merge with default

/*
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package jdk.internal.net.http.frame;

/**
 * When sending a frame, the length field must be set in sub-class
 * by calling computeLength()
 */
public abstract class Http2Frame {

    public static final int FRAME_HEADER_SIZE = 9;

    protected int streamid;
    protected int flags;

    public Http2Frame(int streamid, int flags) {
        this.streamid = streamid;
        this.flags = flags;
    }

    public int streamid() {
        return streamid;
    }

    public void setFlag(int flag) {
        flags |= flag;
    }

    public int getFlags() {
        return flags;
    }

    public boolean getFlag(int flag) {
        return (flags & flag) != 0;
    }

//    public void clearFlag(int flag) {
//        flags &= 0xffffffff ^ flag;
//    }

    public void streamid(int streamid) {
        this.streamid = streamid;
    }


    private String typeAsString() {
        return asString(type());
    }

    public int type() {
        return -1; // Unknown type
    }

    int length() {
        return -1; // Unknown length
    }


    public static String asString(int type) {
        switch (type) {
          case DataFrame.TYPE:
            return "DATA";
          case HeadersFrame.TYPE:
            return "HEADERS";
          case ContinuationFrame.TYPE:
            return "CONTINUATION";
          case ResetFrame.TYPE:
            return "RESET";
          case PriorityFrame.TYPE:
            return "PRIORITY";
          case SettingsFrame.TYPE:
            return "SETTINGS";
          case GoAwayFrame.TYPE:
            return "GOAWAY";
          case PingFrame.TYPE:
            return "PING";
          case PushPromiseFrame.TYPE:
            return "PUSH_PROMISE";
          case WindowUpdateFrame.TYPE:
            return "WINDOW_UPDATE";
          default:
            return "UNKNOWN";
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(typeAsString())
                .append(": length=")
                .append(Integer.toString(length()))
                .append(", streamid=")
                .append(streamid)
                .append(", flags=");

        int f = flags;
        int i = 0;
        if (f == 0) {
            sb.append("0 ");
        } else {
            while (f != 0) {
                if ((f & 1) == 1) {
                    sb.append(flagAsString(1 << i))
                      .append(' ');
                }
                f = f >> 1;
                i++;
            }
        }
        return sb.toString();
    }

    // Override
    public String flagAsString(int f) {
        return "unknown";
    }

}