1 /*
   2  * Copyright (c) 2015, 2016, 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 java.net.http;
  27 
  28 import java.nio.ByteBuffer;
  29 
  30 /**
  31  * Either a HeadersFrame or a ContinuationFrame
  32  */
  33 abstract class HeaderFrame extends Http2Frame {
  34 
  35     int offset;
  36     int number;
  37     int headerLength;
  38     ByteBuffer[] headerBlocks;
  39 
  40     public static final int END_HEADERS = 0x4;
  41 
  42     @Override
  43     String flagAsString(int flag) {
  44         switch (flag) {
  45         case END_HEADERS:
  46             return "END_HEADERS";
  47         }
  48         return super.flagAsString(flag);
  49     }
  50 
  51     /**
  52      * Sets the array of hpack encoded ByteBuffers
  53      */
  54     public void setHeaderBlock(ByteBuffer bufs[], int offset, int number) {
  55         this.headerBlocks = bufs;
  56         this.offset = offset;
  57         this.number = number;
  58         int length = 0;
  59         for (int i=offset; i<offset+number; i++) {
  60             length += headerBlocks[i].remaining();
  61         }
  62         this.headerLength = length;
  63     }
  64 
  65     public void setHeaderBlock(ByteBuffer bufs[]) {
  66         setHeaderBlock(bufs, 0, bufs.length);
  67     }
  68 
  69     public ByteBuffer[] getHeaderBlock() {
  70         return headerBlocks;
  71     }
  72 
  73     /**
  74      * Returns true if this block is the final block of headers
  75      */
  76     public abstract boolean endHeaders();
  77 
  78 }