src/java.net.http/share/classes/jdk/internal/net/http/frame/Http2Frame.java
branchhttp-client-branch
changeset 56092 fd85b2bf2b0d
parent 56089 42208b2f224e
child 56451 9585061fdb04
equal deleted inserted replaced
56091:aedd6133e7a0 56092:fd85b2bf2b0d
       
     1 /*
       
     2  * Copyright (c) 2015, 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 
       
    26 package jdk.internal.net.http.frame;
       
    27 
       
    28 /**
       
    29  * When sending a frame, the length field must be set in sub-class
       
    30  * by calling computeLength()
       
    31  */
       
    32 public abstract class Http2Frame {
       
    33 
       
    34     public static final int FRAME_HEADER_SIZE = 9;
       
    35 
       
    36     protected int streamid;
       
    37     protected int flags;
       
    38 
       
    39     public Http2Frame(int streamid, int flags) {
       
    40         this.streamid = streamid;
       
    41         this.flags = flags;
       
    42     }
       
    43 
       
    44     public int streamid() {
       
    45         return streamid;
       
    46     }
       
    47 
       
    48     public void setFlag(int flag) {
       
    49         flags |= flag;
       
    50     }
       
    51 
       
    52     public int getFlags() {
       
    53         return flags;
       
    54     }
       
    55 
       
    56     public boolean getFlag(int flag) {
       
    57         return (flags & flag) != 0;
       
    58     }
       
    59 
       
    60 //    public void clearFlag(int flag) {
       
    61 //        flags &= 0xffffffff ^ flag;
       
    62 //    }
       
    63 
       
    64     public void streamid(int streamid) {
       
    65         this.streamid = streamid;
       
    66     }
       
    67 
       
    68 
       
    69     private String typeAsString() {
       
    70         return asString(type());
       
    71     }
       
    72 
       
    73     public int type() {
       
    74         return -1; // Unknown type
       
    75     }
       
    76 
       
    77     int length() {
       
    78         return -1; // Unknown length
       
    79     }
       
    80 
       
    81 
       
    82     public static String asString(int type) {
       
    83         switch (type) {
       
    84           case DataFrame.TYPE:
       
    85             return "DATA";
       
    86           case HeadersFrame.TYPE:
       
    87             return "HEADERS";
       
    88           case ContinuationFrame.TYPE:
       
    89             return "CONTINUATION";
       
    90           case ResetFrame.TYPE:
       
    91             return "RESET";
       
    92           case PriorityFrame.TYPE:
       
    93             return "PRIORITY";
       
    94           case SettingsFrame.TYPE:
       
    95             return "SETTINGS";
       
    96           case GoAwayFrame.TYPE:
       
    97             return "GOAWAY";
       
    98           case PingFrame.TYPE:
       
    99             return "PING";
       
   100           case PushPromiseFrame.TYPE:
       
   101             return "PUSH_PROMISE";
       
   102           case WindowUpdateFrame.TYPE:
       
   103             return "WINDOW_UPDATE";
       
   104           default:
       
   105             return "UNKNOWN";
       
   106         }
       
   107     }
       
   108 
       
   109     @Override
       
   110     public String toString() {
       
   111         StringBuilder sb = new StringBuilder();
       
   112         sb.append(typeAsString())
       
   113                 .append(": length=")
       
   114                 .append(Integer.toString(length()))
       
   115                 .append(", streamid=")
       
   116                 .append(streamid)
       
   117                 .append(", flags=");
       
   118 
       
   119         int f = flags;
       
   120         int i = 0;
       
   121         if (f == 0) {
       
   122             sb.append("0 ");
       
   123         } else {
       
   124             while (f != 0) {
       
   125                 if ((f & 1) == 1) {
       
   126                     sb.append(flagAsString(1 << i))
       
   127                       .append(' ');
       
   128                 }
       
   129                 f = f >> 1;
       
   130                 i++;
       
   131             }
       
   132         }
       
   133         return sb.toString();
       
   134     }
       
   135 
       
   136     // Override
       
   137     public String flagAsString(int f) {
       
   138         return "unknown";
       
   139     }
       
   140 
       
   141 }