1 /*
  2  * Copyright (c) 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.
  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 
 24 import java.io.BufferedReader;
 25 import java.io.ByteArrayInputStream;
 26 import java.io.File;
 27 import java.io.FileInputStream;
 28 import java.io.InputStream;
 29 import java.io.InputStreamReader;
 30 import java.util.ArrayList;
 31 import java.util.List;
 32 
 33 /**
 34  * HexDumpReader provides utility methods to read a hex dump text file
 35  * and convert to an InputStream.  The format supported by the methods
 36  * can be generated by the following command.
 37  *
 38  * $ od -vw -t x1 foo | sed -r -e 's/^[0-9]+ ?//' -e 's/ //g' -e '/^$/d'
 39  */
 40 public class HexDumpReader {
 41     public static InputStream getStreamFromHexDump(String fileName) {
 42         return getStreamFromHexDump(new File(System.getProperty("test.src", "."),
 43                                              fileName));
 44     }
 45 
 46     public static InputStream getStreamFromHexDump(File hexFile) {
 47         ByteArrayBuilder bab = new ByteArrayBuilder();
 48         int lineNo = 0;
 49         try (BufferedReader reader
 50                  = new BufferedReader(new InputStreamReader(new FileInputStream(hexFile),
 51                                                             "us-ascii"))) {
 52             String line;
 53             while ((line = reader.readLine()) != null) {
 54                 lineNo++;
 55                 line = line.trim();
 56                 // Skip blank and comment lines.
 57                 if (line.length() == 0) {
 58                     continue;
 59                 }
 60                 int x = line.indexOf('#');
 61                 if (x == 0) {
 62                     continue;
 63                 }
 64                 if (x > 0) {
 65                     line = line.substring(0, x).trim();
 66                 }
 67                 int len = line.length();
 68                 for (int i = 0; i < len; i += 2) {
 69                     bab.put((byte)Integer.parseInt(line, i, i + 2, 16));
 70                 }
 71             }
 72         } catch (Exception e) {
 73             throw new RuntimeException(hexFile.getName() + ":error:" + lineNo + ": " + e, e);
 74         }
 75         return new ByteArrayInputStream(bab.toArray());
 76     }
 77 
 78 
 79     private static class ByteArrayBuilder {
 80         private static final int BUFFER_SIZE = 4096;
 81 
 82         private int size;
 83         private List<byte[]> bytes;
 84         private byte[] current;
 85         private int offset;
 86 
 87         ByteArrayBuilder() {
 88             bytes = new ArrayList<>();
 89             current = new byte[BUFFER_SIZE];
 90         }
 91 
 92         void put(byte b) {
 93             if (offset == BUFFER_SIZE) {
 94                 bytes.add(current);
 95                 current = new byte[BUFFER_SIZE];
 96                 offset = 0;
 97             }
 98             current[offset++] = b;
 99             size++;
100         }
101 
102         byte[] toArray() {
103             byte[] buf = new byte[size];
104             int ptr = 0;
105             for (byte[] ba : bytes) {
106                 System.arraycopy(ba, 0, buf, ptr, ba.length);
107                 ptr += ba.length;
108             }
109             System.arraycopy(current, 0, buf, ptr, offset);
110             assert ptr + offset == size;
111             return buf;
112         }
113     }
114 
115 }