test/jdk/java/net/httpclient/http2/jdk.incubator.httpclient/jdk/incubator/http/internal/hpack/SpecHelper.java
branchhttp-client-branch
changeset 56089 42208b2f224e
parent 56088 38fac6d0521d
child 56090 5c7fb702948a
equal deleted inserted replaced
56088:38fac6d0521d 56089:42208b2f224e
     1 /*
       
     2  * Copyright (c) 2014, 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.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 package jdk.incubator.http.internal.hpack;
       
    24 
       
    25 import java.nio.ByteBuffer;
       
    26 import java.util.ArrayList;
       
    27 import java.util.List;
       
    28 import java.util.regex.Matcher;
       
    29 import java.util.regex.Pattern;
       
    30 import java.util.stream.Collectors;
       
    31 
       
    32 //
       
    33 // THIS IS NOT A TEST
       
    34 //
       
    35 public final class SpecHelper {
       
    36 
       
    37     private SpecHelper() {
       
    38         throw new AssertionError();
       
    39     }
       
    40 
       
    41     public static ByteBuffer toBytes(String hexdump) {
       
    42         Pattern hexByte = Pattern.compile("[0-9a-fA-F]{2}");
       
    43         List<String> bytes = new ArrayList<>();
       
    44         Matcher matcher = hexByte.matcher(hexdump);
       
    45         while (matcher.find()) {
       
    46             bytes.add(matcher.group(0));
       
    47         }
       
    48         ByteBuffer result = ByteBuffer.allocate(bytes.size());
       
    49         for (String f : bytes) {
       
    50             result.put((byte) Integer.parseInt(f, 16));
       
    51         }
       
    52         result.flip();
       
    53         return result;
       
    54     }
       
    55 
       
    56     public static String toHexdump(ByteBuffer bb) {
       
    57         List<String> words = new ArrayList<>();
       
    58         int i = 0;
       
    59         while (bb.hasRemaining()) {
       
    60             if (i % 2 == 0) {
       
    61                 words.add("");
       
    62             }
       
    63             byte b = bb.get();
       
    64             String hex = Integer.toHexString(256 + Byte.toUnsignedInt(b)).substring(1);
       
    65             words.set(i / 2, words.get(i / 2) + hex);
       
    66             i++;
       
    67         }
       
    68         return words.stream().collect(Collectors.joining(" "));
       
    69     }
       
    70 }