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 */
25package jdk.incubator.http.internal.hpack;
26
27import java.nio.ByteBuffer;
28import 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//
47final 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, int start, int end,
66                           boolean huffman) {
67        if (start < 0 || end < 0 || end > input.length() || start > end) {
68            throw new IndexOutOfBoundsException(
69                    String.format("input.length()=%s, start=%s, end=%s",
70                            input.length(), start, end));
71        }
72        if (!huffman) {
73            plainWriter.configure(input, start, end);
74            intWriter.configure(end - start, 7, 0b0000_0000);
75        } else {
76            huffmanWriter.from(input, start, end);
77            intWriter.configure(Huffman.INSTANCE.lengthOf(input, start, end),
78                    7, 0b1000_0000);
79        }
80
81        this.huffman = huffman;
82        state = CONFIGURED;
83        return this;
84    }
85
86    boolean write(ByteBuffer output) {
87        if (state == DONE) {
88            return true;
89        }
90        if (state == NEW) {
91            throw new IllegalStateException("Configure first");
92        }
93        if (!output.hasRemaining()) {
94            return false;
95        }
96        if (state == CONFIGURED) {
97            if (intWriter.write(output)) {
98                state = LENGTH_WRITTEN;
99            } else {
100                return false;
101            }
102        }
103        if (state == LENGTH_WRITTEN) {
104            boolean written = huffman
105                    ? huffmanWriter.write(output)
106                    : plainWriter.write(output);
107            if (written) {
108                state = DONE;
109                return true;
110            } else {
111                return false;
112            }
113        }
114        throw new InternalError(Arrays.toString(new Object[]{state, huffman}));
115    }
116
117    void reset() {
118        intWriter.reset();
119        if (huffman) {
120            huffmanWriter.reset();
121        } else {
122            plainWriter.reset();
123        }
124        state = NEW;
125    }
126}
127