/projects/poi-3.6/src/java/org/apache/poi/util/HexRead.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 191 lines · 143 code · 8 blank · 40 comment · 20 complexity · 2e8201ca51e3eaba2f91d5f3840beaf4 MD5 · raw file

  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. * @author Marc Johnson
  24. * @author Glen Stampoultzis (glens at apache.org)
  25. */
  26. public class HexRead
  27. {
  28. /**
  29. * This method reads hex data from a filename and returns a byte array.
  30. * The file may contain line comments that are preceeded with a # symbol.
  31. *
  32. * @param filename The filename to read
  33. * @return The bytes read from the file.
  34. * @throws IOException If there was a problem while reading the file.
  35. */
  36. public static byte[] readData( String filename ) throws IOException
  37. {
  38. File file = new File( filename );
  39. FileInputStream stream = new FileInputStream( file );
  40. try
  41. {
  42. return readData( stream, -1 );
  43. }
  44. finally
  45. {
  46. stream.close();
  47. }
  48. }
  49. /**
  50. * Same as readData(String) except that this method allows you to specify sections within
  51. * a file. Sections are referenced using section headers in the form:
  52. * <pre>
  53. * [sectioname]
  54. * </pre>
  55. *
  56. * @see #readData(String)
  57. */
  58. public static byte[] readData(InputStream stream, String section ) throws IOException {
  59. try
  60. {
  61. StringBuffer sectionText = new StringBuffer();
  62. boolean inSection = false;
  63. int c = stream.read();
  64. while ( c != -1 )
  65. {
  66. switch ( c )
  67. {
  68. case '[':
  69. inSection = true;
  70. break;
  71. case '\n':
  72. case '\r':
  73. inSection = false;
  74. sectionText = new StringBuffer();
  75. break;
  76. case ']':
  77. inSection = false;
  78. if ( sectionText.toString().equals( section ) ) return readData( stream, '[' );
  79. sectionText = new StringBuffer();
  80. break;
  81. default:
  82. if ( inSection ) sectionText.append( (char) c );
  83. }
  84. c = stream.read();
  85. }
  86. }
  87. finally
  88. {
  89. stream.close();
  90. }
  91. throw new IOException( "Section '" + section + "' not found" );
  92. }
  93. public static byte[] readData( String filename, String section ) throws IOException
  94. {
  95. File file = new File( filename );
  96. FileInputStream stream = new FileInputStream( file );
  97. return readData(stream, section);
  98. }
  99. static public byte[] readData( InputStream stream, int eofChar )
  100. throws IOException
  101. {
  102. int characterCount = 0;
  103. byte b = (byte) 0;
  104. List bytes = new ArrayList();
  105. boolean done = false;
  106. while ( !done )
  107. {
  108. int count = stream.read();
  109. char baseChar = 'a';
  110. if ( count == eofChar ) break;
  111. switch ( count )
  112. {
  113. case '#':
  114. readToEOL( stream );
  115. break;
  116. case '0': case '1': case '2': case '3': case '4': case '5':
  117. case '6': case '7': case '8': case '9':
  118. b <<= 4;
  119. b += (byte) ( count - '0' );
  120. characterCount++;
  121. if ( characterCount == 2 )
  122. {
  123. bytes.add( Byte.valueOf( b ) );
  124. characterCount = 0;
  125. b = (byte) 0;
  126. }
  127. break;
  128. case 'A':
  129. case 'B':
  130. case 'C':
  131. case 'D':
  132. case 'E':
  133. case 'F':
  134. baseChar = 'A';
  135. case 'a':
  136. case 'b':
  137. case 'c':
  138. case 'd':
  139. case 'e':
  140. case 'f':
  141. b <<= 4;
  142. b += (byte) ( count + 10 - baseChar );
  143. characterCount++;
  144. if ( characterCount == 2 )
  145. {
  146. bytes.add( Byte.valueOf( b ) );
  147. characterCount = 0;
  148. b = (byte) 0;
  149. }
  150. break;
  151. case -1:
  152. done = true;
  153. break;
  154. default :
  155. break;
  156. }
  157. }
  158. Byte[] polished = (Byte[]) bytes.toArray( new Byte[0] );
  159. byte[] rval = new byte[polished.length];
  160. for ( int j = 0; j < polished.length; j++ )
  161. {
  162. rval[j] = polished[j].byteValue();
  163. }
  164. return rval;
  165. }
  166. static public byte[] readFromString(String data) {
  167. try {
  168. return readData(new ByteArrayInputStream( data.getBytes() ), -1);
  169. } catch (IOException e) {
  170. throw new RuntimeException(e);
  171. }
  172. }
  173. static private void readToEOL( InputStream stream ) throws IOException
  174. {
  175. int c = stream.read();
  176. while ( c != -1 && c != '\n' && c != '\r' )
  177. {
  178. c = stream.read();
  179. }
  180. }
  181. }