You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HexRead.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. import java.io.*;
  17. import java.util.List;
  18. import java.util.ArrayList;
  19. /**
  20. * Utilities to read hex from files.
  21. * TODO - move to test packages
  22. */
  23. public class HexRead {
  24. /**
  25. * This method reads hex data from a filename and returns a byte array.
  26. * The file may contain line comments that are preceded with a # symbol.
  27. *
  28. * @param filename The filename to read
  29. * @return The bytes read from the file.
  30. * @throws IOException If there was a problem while reading the file.
  31. */
  32. public static byte[] readData( String filename ) throws IOException {
  33. File file = new File( filename );
  34. try (InputStream stream = new FileInputStream(file)) {
  35. return readData(stream, -1);
  36. }
  37. }
  38. /**
  39. * Same as readData(String) except that this method allows you to specify sections within
  40. * a file. Sections are referenced using section headers in the form:
  41. * <pre>
  42. * [sectioname]
  43. * </pre>
  44. *
  45. * @see #readData(String)
  46. */
  47. public static byte[] readData(InputStream stream, String section ) throws IOException {
  48. try {
  49. StringBuilder sectionText = new StringBuilder();
  50. boolean inSection = false;
  51. int c = stream.read();
  52. while ( c != -1 ) {
  53. switch ( c ) {
  54. case '[':
  55. inSection = true;
  56. break;
  57. case '\n':
  58. case '\r':
  59. inSection = false;
  60. sectionText = new StringBuilder();
  61. break;
  62. case ']':
  63. inSection = false;
  64. if ( sectionText.toString().equals( section ) ) return readData( stream, '[' );
  65. sectionText = new StringBuilder();
  66. break;
  67. default:
  68. if ( inSection ) sectionText.append( (char) c );
  69. }
  70. c = stream.read();
  71. }
  72. } finally {
  73. stream.close();
  74. }
  75. throw new IOException( "Section '" + section + "' not found" );
  76. }
  77. public static byte[] readData( String filename, String section ) throws IOException {
  78. return readData(new FileInputStream( filename ), section);
  79. }
  80. @SuppressWarnings("fallthrough")
  81. static public byte[] readData( InputStream stream, int eofChar )
  82. throws IOException
  83. {
  84. int characterCount = 0;
  85. byte b = (byte) 0;
  86. List<Byte> bytes = new ArrayList<>();
  87. final char a = 'a' - 10;
  88. final char A = 'A' - 10;
  89. while ( true ) {
  90. int count = stream.read();
  91. int digitValue = -1;
  92. if ( '0' <= count && count <= '9' ) {
  93. digitValue = count - '0';
  94. } else if ( 'A' <= count && count <= 'F' ) {
  95. digitValue = count - A;
  96. } else if ( 'a' <= count && count <= 'f' ) {
  97. digitValue = count - a;
  98. } else if ( '#' == count ) {
  99. readToEOL( stream );
  100. } else if ( -1 == count || eofChar == count ) {
  101. break;
  102. }
  103. // else: ignore the character
  104. if (digitValue != -1) {
  105. b <<= 4;
  106. b += (byte) digitValue;
  107. characterCount++;
  108. if ( characterCount == 2 ) {
  109. bytes.add( Byte.valueOf( b ) );
  110. characterCount = 0;
  111. b = (byte) 0;
  112. }
  113. }
  114. }
  115. Byte[] polished = bytes.toArray(new Byte[0]);
  116. byte[] rval = new byte[polished.length];
  117. for ( int j = 0; j < polished.length; j++ ) {
  118. rval[j] = polished[j].byteValue();
  119. }
  120. return rval;
  121. }
  122. static public byte[] readFromString(String data) {
  123. try {
  124. return readData(new ByteArrayInputStream( data.getBytes(StringUtil.UTF8) ), -1);
  125. } catch (IOException e) {
  126. throw new RuntimeException(e);
  127. }
  128. }
  129. static private void readToEOL( InputStream stream ) throws IOException {
  130. int c = stream.read();
  131. while ( c != -1 && c != '\n' && c != '\r' ) {
  132. c = stream.read();
  133. }
  134. }
  135. }