< prev index next >

test/jdk/java/text/testlib/HexDumpReader.java

Print this page

  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             }

  1 /*
  2  * Copyright (c) 2016, 2023, 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.nio.charset.StandardCharsets;
 31 import java.util.ArrayList;
 32 import java.util.List;
 33 
 34 /**
 35  * HexDumpReader provides utility methods to read a hex dump text file
 36  * and convert to an InputStream.  The format supported by the methods
 37  * can be generated by the following command.
 38  *
 39  * $ od -vw -t x1 foo | sed -r -e 's/^[0-9]+ ?//' -e 's/ //g' -e '/^$/d'
 40  */
 41 public final class HexDumpReader {
 42 
 43     // Utility class should not be instantiated
 44     private HexDumpReader() {}
 45 
 46     public static InputStream getStreamFromHexDump(String fileName) {
 47         return getStreamFromHexDump(new File(System.getProperty("test.src", "."),
 48                                              fileName));
 49     }
 50 
 51     public static InputStream getStreamFromHexDump(File hexFile) {
 52         ByteArrayBuilder bab = new ByteArrayBuilder();
 53         int lineNo = 0;
 54         try (BufferedReader reader
 55                  = new BufferedReader(new InputStreamReader(new FileInputStream(hexFile),
 56                 StandardCharsets.US_ASCII))) {
 57             String line;
 58             while ((line = reader.readLine()) != null) {
 59                 lineNo++;
 60                 line = line.trim();
 61                 // Skip blank and comment lines.
 62                 if (line.length() == 0) {
 63                     continue;
 64                 }
 65                 int x = line.indexOf('#');
 66                 if (x == 0) {
 67                     continue;
 68                 }
 69                 if (x > 0) {
 70                     line = line.substring(0, x).trim();
 71                 }
 72                 int len = line.length();
 73                 for (int i = 0; i < len; i += 2) {
 74                     bab.put((byte)Integer.parseInt(line, i, i + 2, 16));
 75                 }
 76             }
< prev index next >