1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class BinaryFileHelper {
  5. /**
  6. * Reads a binary file and returns its content as a byte array.
  7. * @param filePath The path to the binary file.
  8. * @return The byte array representing the file content, or null if an error occurs.
  9. */
  10. public static byte[] readBinaryFile(String filePath) {
  11. try (FileInputStream fis = new FileInputStream(filePath)) {
  12. return fis.readAllBytes();
  13. } catch (IOException e) {
  14. System.err.println("Error reading binary file: " + e.getMessage());
  15. return null;
  16. }
  17. }
  18. /**
  19. * Writes a byte array to a binary file.
  20. * @param filePath The path to the file.
  21. * @param data The byte array to write.
  22. * @return True if the write was successful, false otherwise.
  23. */
  24. public static boolean writeBinaryFile(String filePath, byte[] data) {
  25. try (FileOutputStream fos = new FileOutputStream(filePath)) {
  26. fos.write(data);
  27. return true;
  28. } catch (IOException e) {
  29. System.err.println("Error writing to binary file: " + e.getMessage());
  30. return false;
  31. }
  32. }
  33. /**
  34. * Appends a byte array to the end of a binary file.
  35. * @param filePath The path to the file.
  36. * @param data The byte array to append.
  37. * @return True if the append was successful, false otherwise.
  38. */
  39. public static boolean appendToBinaryFile(String filePath, byte[] data) {
  40. try (FileOutputStream fos = new FileOutputStream(filePath, true)) { //true for append mode
  41. fos.write(data);
  42. return true;
  43. } catch (IOException e) {
  44. System.err.println("Error appending to binary file: " + e.getMessage());
  45. return false;
  46. }
  47. }
  48. /**
  49. * Calculates the MD5 hash of a binary file.
  50. * @param filePath The path to the file.
  51. * @return The MD5 hash as a hexadecimal string, or null if an error occurs.
  52. */
  53. public static String calculateMD5(String filePath) {
  54. try (FileInputStream fis = new FileInputStream(filePath)) {
  55. MessageDigest md = MessageDigest.getInstance("MD5");
  56. byte[] digest = md.digest();
  57. return java.util.Base64.getEncoder().encodeToString(digest);
  58. } catch (IOException e) {
  59. System.err.println("Error calculating MD5: " + e.getMessage());
  60. return null;
  61. } catch (NoSuchAlgorithmException e) {
  62. System.err.println("MD5 algorithm not available: " + e.getMessage());
  63. return null;
  64. }
  65. }
  66. /**
  67. * Reads a specified number of bytes from a binary file.
  68. * @param filePath Path to the file
  69. * @param count Number of bytes to read
  70. * @return Byte array containing the read data, or null on error.
  71. */
  72. public static byte[] readBytes(String filePath, int count) {
  73. try (FileInputStream fis = new FileInputStream(filePath)) {
  74. byte[] buffer = new byte[count];
  75. int bytesRead = fis.read(buffer);
  76. if (bytesRead == -1) {
  77. return null; //EOF
  78. }
  79. return buffer;
  80. } catch (IOException e) {
  81. System.err.println("Error reading bytes: " + e.getMessage());
  82. return null;
  83. }
  84. }
  85. /**
  86. * Writes a specified number of bytes to a binary file.
  87. * @param filePath Path to the file
  88. * @param data Byte array to write
  89. * @param count Number of bytes to write
  90. * @return True if write was successful, false otherwise.
  91. */
  92. public static boolean writeBytes(String filePath, byte[] data, int count) {
  93. try (FileOutputStream fos = new FileOutputStream(filePath)) {
  94. fos.write(data, 0, count);
  95. return true;
  96. } catch (IOException e) {
  97. System.err.println("Error writing bytes: " + e.getMessage());
  98. return false;
  99. }
  100. }
  101. public static void main(String[] args) {
  102. //Example Usage
  103. String

Add your comment