1 /*
   2  * Copyright (c) 2015, 2017, 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.incubator.http.internal.frame;
  27 
  28 import java.nio.ByteBuffer;
  29 import java.util.List;
  30 
  31 public class HeadersFrame extends HeaderFrame {
  32 
  33     public static final int TYPE = 0x1;
  34 
  35     // Flags
  36     public static final int END_STREAM = 0x1;
  37     public static final int PADDED = 0x8;
  38     public static final int PRIORITY = 0x20;
  39 
  40 
  41     private int padLength;
  42     private int streamDependency;
  43     private int weight;
  44     private boolean exclusive;
  45 
  46     public HeadersFrame(int streamid, int flags, List<ByteBuffer> headerBlocks, int padLength) {
  47         super(streamid, flags, headerBlocks);
  48         if (padLength > 0) {
  49             setPadLength(padLength);
  50         }
  51     }
  52 
  53     public HeadersFrame(int streamid, int flags, List<ByteBuffer> headerBlocks) {
  54         super(streamid, flags, headerBlocks);
  55     }
  56 
  57     public HeadersFrame(int streamid, int flags, ByteBuffer headerBlock) {
  58         this(streamid, flags, List.of(headerBlock));
  59     }
  60 
  61     @Override
  62     public int type() {
  63         return TYPE;
  64     }
  65 
  66     @Override
  67     int length() {
  68         return headerLength
  69                 + ((flags & PADDED) != 0 ? (1 + padLength) : 0)
  70                 + ((flags & PRIORITY) != 0 ? 5 : 0);
  71     }
  72 
  73     @Override
  74     public String flagAsString(int flag) {
  75         switch (flag) {
  76             case END_STREAM:
  77                 return "END_STREAM";
  78             case PADDED:
  79                 return "PADDED";
  80             case PRIORITY:
  81                 return "PRIORITY";
  82         }
  83         return super.flagAsString(flag);
  84     }
  85 
  86     public void setPadLength(int padLength) {
  87         this.padLength = padLength;
  88         flags |= PADDED;
  89     }
  90 
  91     int getPadLength() {
  92         return padLength;
  93     }
  94 
  95     public void setPriority(int streamDependency, boolean exclusive, int weight) {
  96         this.streamDependency = streamDependency;
  97         this.exclusive = exclusive;
  98         this.weight = weight;
  99         this.flags |= PRIORITY;
 100     }
 101 
 102     public int getStreamDependency() {
 103         return streamDependency;
 104     }
 105 
 106     public int getWeight() {
 107         return weight;
 108     }
 109 
 110     public boolean getExclusive() {
 111         return exclusive;
 112     }
 113 
 114 }