diff --git a/common/src/main/java/com/hedera/block/common/utils/FileUtilities.java b/common/src/main/java/com/hedera/block/common/utils/FileUtilities.java index f5e7c3cb..4111f3a1 100644 --- a/common/src/main/java/com/hedera/block/common/utils/FileUtilities.java +++ b/common/src/main/java/com/hedera/block/common/utils/FileUtilities.java @@ -29,9 +29,7 @@ import java.util.Set; import java.util.zip.GZIPInputStream; -/** - * A utility class that deals with logic related to dealing with files. - */ +/** A utility class that deals with logic related to dealing with files. */ public final class FileUtilities { private static final Logger LOGGER = System.getLogger(FileUtilities.class.getName()); @@ -65,22 +63,21 @@ public final class FileUtilities { PosixFilePermission.OTHERS_EXECUTE)); /** - * Log message template used when a path is not created because a file or folder already exists - * at the requested path. + * Log message template used when a path is not created because a file + * or folder already exists at the requested path. */ private static final String PRE_EXISTING_FOLDER_MESSAGE = "Requested %s [%s] not created because %s already exists at %s"; /** - * Create a new path (folder or file) if it does not exist. Any folders or files created will - * use default permissions. + * Create a new path (folder or file) if it does not exist. + * Any folders or files created will use default permissions. * - * @param toCreate valid, non-null instance of {@link Path} to be created - * @param logLevel valid, non-null instance of {@link System.Logger.Level} to use + * @param toCreate valid, non-null instance of {@link Path} to be created + * @param logLevel valid, non-null instance of {@link System.Logger.Level} to use * @param semanticPathName valid, non-blank {@link String} used for logging that represents the - * desired path semantically - * @param createDir {@link Boolean} value if we should create a directory or a file - * + * desired path semantically + * @param createDir {@link Boolean} value if we should create a directory or a file * @throws IOException if the path cannot be created */ public static void createPathIfNotExists( @@ -101,15 +98,16 @@ public static void createPathIfNotExists( /** * Create a new path (folder or file) if it does not exist. * - * @param toCreate The path to be created. - * @param logLevel The logging level to use when logging this event. - * @param filePermissions Permissions to use when creating a new file. + * @param toCreate The path to be created. + * @param logLevel The logging level to use when logging this event. + * @param filePermissions Permissions to use when creating a new file. * @param folderPermissions Permissions to use when creating a new folder. - * @param semanticPathName A name to represent the path in a logging statement. - * @param createDir A flag indicating we should create a directory (true) or a file - * (false) - * - * @throws IOException if the path cannot be created due to a filesystem error. + * @param semanticPathName A name to represent the path in a logging + * statement. + * @param createDir A flag indicating we should create a directory + * (true) or a file (false) + * @throws IOException if the path cannot be created due to a filesystem + * error. */ public static void createPathIfNotExists( @NonNull final Path toCreate, @@ -146,22 +144,20 @@ public static void createPathIfNotExists( /** * Read a GZIP file and return the content as a byte array. *

- * This method is _unsafe_ because it reads the entire file content into a single byte array, - * which can cause memory issues, and may fail if the file contains a large amount of data. + * This method is _unsafe_ because it reads the entire file content into + * a single byte array, which can cause memory issues, and may fail if the + * file contains a large amount of data. * * @param filePath Path to the GZIP file. - * * @return byte array containing the _uncompressed_ content of the GZIP file. - * - * @throws IOException if unable to read the file. - * @throws OutOfMemoryError if a byte array large enough to contain the file contents cannot be - * allocated (either because it exceeds MAX_INT bytes or exceeds - * available heap memory). + * @throws IOException if unable to read the file. + * @throws OutOfMemoryError if a byte array large enough to contain the + * file contents cannot be allocated (either because it exceeds MAX_INT + * bytes or exceeds available heap memory). */ public static byte[] readGzipFileUnsafe(@NonNull final Path filePath) throws IOException { Objects.requireNonNull(filePath); - try (final GZIPInputStream gzipInputStream = - new GZIPInputStream(Files.newInputStream(filePath))) { + try (final var gzipInputStream = new GZIPInputStream(Files.newInputStream(filePath))) { return gzipInputStream.readAllBytes(); } } @@ -171,17 +167,17 @@ public static byte[] readGzipFileUnsafe(@NonNull final Path filePath) throws IOE *

* This method uses default extensions for gzip and block files. *

- * This method is _unsafe_ because it reads the entire file content into a single byte array, - * which can cause memory issues, and may fail if the file contains a large amount of data. + * This method is _unsafe_ because it reads the entire file content into + * a single byte array, which can cause memory issues, and may fail if the + * file contains a large amount of data. * * @param file to read bytes from - * - * @return byte array of the content of the file or null if the file extension is not supported - * - * @throws IOException if unable to read the file. - * @throws OutOfMemoryError if a byte array large enough to contain the file contents cannot be - * allocated (either because it exceeds MAX_INT bytes or exceeds - * available heap memory). + * @return byte array of the content of the file or null if the file extension is not + * supported + * @throws IOException if unable to read the file. + * @throws OutOfMemoryError if a byte array large enough to contain the + * file contents cannot be allocated (either because it exceeds MAX_INT + * bytes or exceeds available heap memory). */ public static byte[] readFileBytesUnsafe(@NonNull final File file) throws IOException { return readFileBytesUnsafe(file.toPath()); @@ -192,17 +188,17 @@ public static byte[] readFileBytesUnsafe(@NonNull final File file) throws IOExce *

* This method uses default extensions for gzip and block files. *

- * This method is _unsafe_ because it reads the entire file content into a single byte array, - * which can cause memory issues, and may fail if the file contains a large amount of data. + * This method is _unsafe_ because it reads the entire file content into + * a single byte array, which can cause memory issues, and may fail if the + * file contains a large amount of data. * * @param filePath Path to the file - * - * @return byte array of the content of the file or null if the file extension is not supported - * - * @throws IOException if unable to read the file. - * @throws OutOfMemoryError if a byte array large enough to contain the file contents cannot be - * allocated (either because it exceeds MAX_INT bytes or exceeds - * available heap memory). + * @return byte array of the content of the file or null if the file extension is not + * supported + * @throws IOException if unable to read the file. + * @throws OutOfMemoryError if a byte array large enough to contain the + * file contents cannot be allocated (either because it exceeds MAX_INT + * bytes or exceeds available heap memory). */ public static byte[] readFileBytesUnsafe(@NonNull final Path filePath) throws IOException { return readFileBytesUnsafe(filePath, ".blk", ".gz"); @@ -211,20 +207,20 @@ public static byte[] readFileBytesUnsafe(@NonNull final Path filePath) throws IO /** * Read a file and return the content as a byte array. *

- * This method is _unsafe_ because it reads the entire file content into a single byte array, - * which can cause memory issues, and may fail if the file contains a large amount of data. + * This method is _unsafe_ because it reads the entire file content into + * a single byte array, which can cause memory issues, and may fail if the + * file contains a large amount of data. * - * @param filePath Path to the file to read. + * @param filePath Path to the file to read. * @param blockFileExtension A file extension for block files. - * @param gzipFileExtension A file extension for gzip files. - * - * @return A byte array with the full contents of the file, or null if the file extension - * requested does not match at least one of the extensions provided (GZip or Block). - * - * @throws IOException if unable to read the file. - * @throws OutOfMemoryError if a byte array large enough to contain the file contents cannot be - * allocated (either because it exceeds MAX_INT bytes or exceeds - * available heap memory). + * @param gzipFileExtension A file extension for gzip files. + * @return A byte array with the full contents of the file, or null if the + * file extension requested does not match at least one of the + * extensions provided (GZip or Block). + * @throws IOException if unable to read the file. + * @throws OutOfMemoryError if a byte array large enough to contain the + * file contents cannot be allocated (either because it exceeds MAX_INT + * bytes or exceeds available heap memory). */ public static byte[] readFileBytesUnsafe( @NonNull final Path filePath, diff --git a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java index c4d5f2c1..6527a6a6 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java @@ -29,24 +29,19 @@ import java.lang.System.Logger; import java.nio.file.Path; -/** - * The BlockStreamSimulator class defines the simulator for the block stream. - */ +/** The BlockStreamSimulator class defines the simulator for the block stream. */ public class BlockStreamSimulator { private static final Logger LOGGER = System.getLogger(BlockStreamSimulator.class.getName()); - /** - * This constructor should not be instantiated. - */ + /** This constructor should not be instantiated. */ private BlockStreamSimulator() {} /** * The main entry point for the block stream simulator. * * @param args the arguments to be passed to the block stream simulator - * - * @throws IOException if an I/O error occurs - * @throws InterruptedException if the thread is interrupted + * @throws IOException if an I/O error occurs + * @throws InterruptedException if the thread is interrupted * @throws BlockSimulatorParsingException if a parse error occurs */ public static void main(final String[] args) diff --git a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsDirBlockStreamManager.java b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsDirBlockStreamManager.java index 673a1bd5..c7e1dd76 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsDirBlockStreamManager.java +++ b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsDirBlockStreamManager.java @@ -16,11 +16,11 @@ package com.hedera.block.simulator.generator; -import static com.hedera.block.common.utils.FileUtilities.readFileBytesUnsafe; import static java.lang.System.Logger.Level.DEBUG; import static java.lang.System.Logger.Level.ERROR; import static java.lang.System.Logger.Level.INFO; +import com.hedera.block.common.utils.FileUtilities; import com.hedera.block.simulator.config.data.BlockGeneratorConfig; import com.hedera.block.simulator.config.types.GenerationMode; import com.hedera.hapi.block.stream.Block; @@ -117,16 +117,14 @@ private void loadBlocks() throws IOException, ParseException { try (final Stream blockItems = Files.list(blockDirPath).filter(Files::isRegularFile)) { - final List sortedBlockItems = - blockItems - .sorted( - Comparator.comparing( - BlockAsDirBlockStreamManager - ::extractNumberFromPath)) - .toList(); + final Comparator comparator = + Comparator.comparing( + BlockAsDirBlockStreamManager::extractNumberFromPath); + final List sortedBlockItems = blockItems.sorted(comparator).toList(); for (final Path pathBlockItem : sortedBlockItems) { - final byte[] blockItemBytes = readFileBytesUnsafe(pathBlockItem); + final byte[] blockItemBytes = + FileUtilities.readFileBytesUnsafe(pathBlockItem); // if null means the file is not a block item and we can skip the file. if (blockItemBytes == null) { continue; diff --git a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsFileBlockStreamManager.java b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsFileBlockStreamManager.java index 41c828f0..9945f2cc 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsFileBlockStreamManager.java +++ b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsFileBlockStreamManager.java @@ -16,11 +16,11 @@ package com.hedera.block.simulator.generator; -import static com.hedera.block.common.utils.FileUtilities.readFileBytesUnsafe; import static java.lang.System.Logger.Level.DEBUG; import static java.lang.System.Logger.Level.ERROR; import static java.lang.System.Logger.Level.INFO; +import com.hedera.block.common.utils.FileUtilities; import com.hedera.block.simulator.config.data.BlockGeneratorConfig; import com.hedera.block.simulator.config.types.GenerationMode; import com.hedera.hapi.block.stream.Block; @@ -109,7 +109,7 @@ private void loadBlocks() throws IOException, ParseException { for (final Path blockPath : sortedBlockFiles) { - final byte[] blockBytes = readFileBytesUnsafe(blockPath); + final byte[] blockBytes = FileUtilities.readFileBytesUnsafe(blockPath); // skip if block is null, usually due to SO files like .DS_STORE if (blockBytes == null) { continue; diff --git a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsFileLargeDataSets.java b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsFileLargeDataSets.java index 8b853a82..9d9ae9bf 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsFileLargeDataSets.java +++ b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsFileLargeDataSets.java @@ -16,9 +16,9 @@ package com.hedera.block.simulator.generator; -import static com.hedera.block.common.utils.FileUtilities.readFileBytesUnsafe; import static java.lang.System.Logger.Level.INFO; +import com.hedera.block.common.utils.FileUtilities; import com.hedera.block.simulator.config.data.BlockGeneratorConfig; import com.hedera.block.simulator.config.types.GenerationMode; import com.hedera.block.simulator.exception.BlockSimulatorParsingException; @@ -86,7 +86,7 @@ public Block getNextBlock() throws IOException, BlockSimulatorParsingException { } try { - final byte[] blockBytes = readFileBytesUnsafe(blockFile); + final byte[] blockBytes = FileUtilities.readFileBytesUnsafe(blockFile); LOGGER.log(INFO, "Loading block: " + blockFile.getName());