Path: blob/master/test/jdk/java/text/testlib/HexDumpReader.java
41149 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.BufferedReader;24import java.io.ByteArrayInputStream;25import java.io.File;26import java.io.FileInputStream;27import java.io.InputStream;28import java.io.InputStreamReader;29import java.util.ArrayList;30import java.util.List;3132/**33* HexDumpReader provides utility methods to read a hex dump text file34* and convert to an InputStream. The format supported by the methods35* can be generated by the following command.36*37* $ od -vw -t x1 foo | sed -r -e 's/^[0-9]+ ?//' -e 's/ //g' -e '/^$/d'38*/39public class HexDumpReader {40public static InputStream getStreamFromHexDump(String fileName) {41return getStreamFromHexDump(new File(System.getProperty("test.src", "."),42fileName));43}4445public static InputStream getStreamFromHexDump(File hexFile) {46ByteArrayBuilder bab = new ByteArrayBuilder();47int lineNo = 0;48try (BufferedReader reader49= new BufferedReader(new InputStreamReader(new FileInputStream(hexFile),50"us-ascii"))) {51String line;52while ((line = reader.readLine()) != null) {53lineNo++;54line = line.trim();55// Skip blank and comment lines.56if (line.length() == 0) {57continue;58}59int x = line.indexOf('#');60if (x == 0) {61continue;62}63if (x > 0) {64line = line.substring(0, x).trim();65}66int len = line.length();67for (int i = 0; i < len; i += 2) {68bab.put((byte)Integer.parseInt(line, i, i + 2, 16));69}70}71} catch (Exception e) {72throw new RuntimeException(hexFile.getName() + ":error:" + lineNo + ": " + e, e);73}74return new ByteArrayInputStream(bab.toArray());75}767778private static class ByteArrayBuilder {79private static final int BUFFER_SIZE = 4096;8081private int size;82private List<byte[]> bytes;83private byte[] current;84private int offset;8586ByteArrayBuilder() {87bytes = new ArrayList<>();88current = new byte[BUFFER_SIZE];89}9091void put(byte b) {92if (offset == BUFFER_SIZE) {93bytes.add(current);94current = new byte[BUFFER_SIZE];95offset = 0;96}97current[offset++] = b;98size++;99}100101byte[] toArray() {102byte[] buf = new byte[size];103int ptr = 0;104for (byte[] ba : bytes) {105System.arraycopy(ba, 0, buf, ptr, ba.length);106ptr += ba.length;107}108System.arraycopy(current, 0, buf, ptr, offset);109assert ptr + offset == size;110return buf;111}112}113114}115116117