import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class BinaryFileHelper {
/**
* Reads a binary file and returns its content as a byte array.
* @param filePath The path to the binary file.
* @return The byte array representing the file content, or null if an error occurs.
*/
public static byte[] readBinaryFile(String filePath) {
try (FileInputStream fis = new FileInputStream(filePath)) {
return fis.readAllBytes();
} catch (IOException e) {
System.err.println("Error reading binary file: " + e.getMessage());
return null;
}
}
/**
* Writes a byte array to a binary file.
* @param filePath The path to the file.
* @param data The byte array to write.
* @return True if the write was successful, false otherwise.
*/
public static boolean writeBinaryFile(String filePath, byte[] data) {
try (FileOutputStream fos = new FileOutputStream(filePath)) {
fos.write(data);
return true;
} catch (IOException e) {
System.err.println("Error writing to binary file: " + e.getMessage());
return false;
}
}
/**
* Appends a byte array to the end of a binary file.
* @param filePath The path to the file.
* @param data The byte array to append.
* @return True if the append was successful, false otherwise.
*/
public static boolean appendToBinaryFile(String filePath, byte[] data) {
try (FileOutputStream fos = new FileOutputStream(filePath, true)) { //true for append mode
fos.write(data);
return true;
} catch (IOException e) {
System.err.println("Error appending to binary file: " + e.getMessage());
return false;
}
}
/**
* Calculates the MD5 hash of a binary file.
* @param filePath The path to the file.
* @return The MD5 hash as a hexadecimal string, or null if an error occurs.
*/
public static String calculateMD5(String filePath) {
try (FileInputStream fis = new FileInputStream(filePath)) {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] digest = md.digest();
return java.util.Base64.getEncoder().encodeToString(digest);
} catch (IOException e) {
System.err.println("Error calculating MD5: " + e.getMessage());
return null;
} catch (NoSuchAlgorithmException e) {
System.err.println("MD5 algorithm not available: " + e.getMessage());
return null;
}
}
/**
* Reads a specified number of bytes from a binary file.
* @param filePath Path to the file
* @param count Number of bytes to read
* @return Byte array containing the read data, or null on error.
*/
public static byte[] readBytes(String filePath, int count) {
try (FileInputStream fis = new FileInputStream(filePath)) {
byte[] buffer = new byte[count];
int bytesRead = fis.read(buffer);
if (bytesRead == -1) {
return null; //EOF
}
return buffer;
} catch (IOException e) {
System.err.println("Error reading bytes: " + e.getMessage());
return null;
}
}
/**
* Writes a specified number of bytes to a binary file.
* @param filePath Path to the file
* @param data Byte array to write
* @param count Number of bytes to write
* @return True if write was successful, false otherwise.
*/
public static boolean writeBytes(String filePath, byte[] data, int count) {
try (FileOutputStream fos = new FileOutputStream(filePath)) {
fos.write(data, 0, count);
return true;
} catch (IOException e) {
System.err.println("Error writing bytes: " + e.getMessage());
return false;
}
}
public static void main(String[] args) {
//Example Usage
String
Add your comment