src/java.net.http/share/classes/java/net/http/internal/hpack/StringWriter.java
branchhttp-client-branch
changeset 56089 42208b2f224e
parent 56088 38fac6d0521d
equal deleted inserted replaced
56088:38fac6d0521d 56089:42208b2f224e
       
     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 package java.net.http.internal.hpack;
       
    26 
       
    27 import java.nio.ByteBuffer;
       
    28 import java.util.Arrays;
       
    29 
       
    30 //
       
    31 //          0   1   2   3   4   5   6   7
       
    32 //        +---+---+---+---+---+---+---+---+
       
    33 //        | H |    String Length (7+)     |
       
    34 //        +---+---------------------------+
       
    35 //        |  String Data (Length octets)  |
       
    36 //        +-------------------------------+
       
    37 //
       
    38 // StringWriter does not require a notion of endOfInput (isLast) in 'write'
       
    39 // methods due to the nature of string representation in HPACK. Namely, the
       
    40 // length of the string is put before string's contents. Therefore the length is
       
    41 // always known beforehand.
       
    42 //
       
    43 // Expected use:
       
    44 //
       
    45 //     configure write* (reset configure write*)*
       
    46 //
       
    47 final class StringWriter {
       
    48 
       
    49     private static final int NEW            = 0;
       
    50     private static final int CONFIGURED     = 1;
       
    51     private static final int LENGTH_WRITTEN = 2;
       
    52     private static final int DONE           = 4;
       
    53 
       
    54     private final IntegerWriter intWriter = new IntegerWriter();
       
    55     private final Huffman.Writer huffmanWriter = new Huffman.Writer();
       
    56     private final ISO_8859_1.Writer plainWriter = new ISO_8859_1.Writer();
       
    57 
       
    58     private int state = NEW;
       
    59     private boolean huffman;
       
    60 
       
    61     StringWriter configure(CharSequence input, boolean huffman) {
       
    62         return configure(input, 0, input.length(), huffman);
       
    63     }
       
    64 
       
    65     StringWriter configure(CharSequence input,
       
    66                            int start,
       
    67                            int end,
       
    68                            boolean huffman) {
       
    69         if (start < 0 || end < 0 || end > input.length() || start > end) {
       
    70             throw new IndexOutOfBoundsException(
       
    71                     String.format("input.length()=%s, start=%s, end=%s",
       
    72                             input.length(), start, end));
       
    73         }
       
    74         if (!huffman) {
       
    75             plainWriter.configure(input, start, end);
       
    76             intWriter.configure(end - start, 7, 0b0000_0000);
       
    77         } else {
       
    78             huffmanWriter.from(input, start, end);
       
    79             intWriter.configure(Huffman.INSTANCE.lengthOf(input, start, end),
       
    80                     7, 0b1000_0000);
       
    81         }
       
    82 
       
    83         this.huffman = huffman;
       
    84         state = CONFIGURED;
       
    85         return this;
       
    86     }
       
    87 
       
    88     boolean write(ByteBuffer output) {
       
    89         if (state == DONE) {
       
    90             return true;
       
    91         }
       
    92         if (state == NEW) {
       
    93             throw new IllegalStateException("Configure first");
       
    94         }
       
    95         if (!output.hasRemaining()) {
       
    96             return false;
       
    97         }
       
    98         if (state == CONFIGURED) {
       
    99             if (intWriter.write(output)) {
       
   100                 state = LENGTH_WRITTEN;
       
   101             } else {
       
   102                 return false;
       
   103             }
       
   104         }
       
   105         if (state == LENGTH_WRITTEN) {
       
   106             boolean written = huffman
       
   107                     ? huffmanWriter.write(output)
       
   108                     : plainWriter.write(output);
       
   109             if (written) {
       
   110                 state = DONE;
       
   111                 return true;
       
   112             } else {
       
   113                 return false;
       
   114             }
       
   115         }
       
   116         throw new InternalError(Arrays.toString(new Object[]{state, huffman}));
       
   117     }
       
   118 
       
   119     void reset() {
       
   120         intWriter.reset();
       
   121         if (huffman) {
       
   122             huffmanWriter.reset();
       
   123         } else {
       
   124             plainWriter.reset();
       
   125         }
       
   126         state = NEW;
       
   127     }
       
   128 }