@@ -21,6 +21,12 @@ public class HuffmanCompressor implements Compressor {
2121 */
2222 private final String inputFilePath ;
2323
24+ /**
25+ * Path to the output file that will be compressed
26+ * Example: "/home/user/document.txt.huffz"
27+ */
28+ private final String outputFilePath ;
29+
2430 /**
2531 * Array storing the frequency of each byte value (0-255) in the input file
2632 * Index represents the byte value, value represents the count
@@ -51,6 +57,7 @@ public class HuffmanCompressor implements Compressor {
5157 public HuffmanCompressor (String inputFilePath ) {
5258 logger .debug ("Initializing HuffmanCompressor for file: {}" , inputFilePath );
5359 this .inputFilePath = inputFilePath ;
60+ this .outputFilePath = inputFilePath + Constants .HUFFMAN_FILE_EXTENSION ;
5461 this .frequency = HuffmanUtils .calculateFrequencyOfBytesInFile (inputFilePath );
5562
5663 if (HuffmanUtils .isEmptyFile (frequency )) {
@@ -68,31 +75,31 @@ public HuffmanCompressor(String inputFilePath) {
6875 /**
6976 * Step 1: Write the table size
7077 */
71- private void writeTableSize (ByteWriter writer , int [] frequency ) throws IOException {
72- writer .writeInt (HuffmanUtils .calculateUniqueByteCount (frequency ));
78+ private void writeTableSize (ByteWriter writer ) throws IOException {
79+ writer .writeInt (HuffmanUtils .calculateUniqueByteCount (this . frequency ));
7380 }
7481
7582 /**
7683 * Step 2: Write the frequency table
7784 */
78- private void writeFrequencyTable (ByteWriter writer , int [] frequency ) throws IOException {
85+ private void writeFrequencyTable (ByteWriter writer ) throws IOException {
7986 for (int i = 0 ; i < Constants .BYTE_VALUES_COUNT ; i ++) {
80- if (frequency [i ] != 0 ) {
87+ if (this . frequency [i ] != 0 ) {
8188 byte currentByte = (byte ) i ;
8289 writer .writeByte (currentByte );
83- writer .writeInt (frequency [i ]);
90+ writer .writeInt (this . frequency [i ]);
8491 }
8592 }
8693 }
8794
8895 /**
8996 * Step 3: Calculate and write extra bits needed for padding
9097 */
91- private void writeExtraBits (ByteWriter writer , int [] frequency , String [] huffmanCodes ) throws IOException {
98+ private void writeExtraBits (ByteWriter writer ) throws IOException {
9299 int totalBinaryDigitsMod8 = 0 ;
93100 for (int i = 0 ; i < Constants .BYTE_VALUES_COUNT ; i ++) {
94- if (huffmanCodes [i ] != null ) {
95- totalBinaryDigitsMod8 += huffmanCodes [i ].length () * frequency [i ];
101+ if (this . huffmanCodes [i ] != null ) {
102+ totalBinaryDigitsMod8 += this . huffmanCodes [i ].length () * this . frequency [i ];
96103 totalBinaryDigitsMod8 %= Constants .BITS_PER_BYTE ;
97104 }
98105 }
@@ -104,12 +111,12 @@ private void writeExtraBits(ByteWriter writer, int[] frequency, String[] huffman
104111 * Step 4: Encode and write the compressed content using Huffman codes
105112 * Reads each byte from input, converts to its Huffman code, and writes compressed output
106113 */
107- private void encodeAndWriteContent (ByteReader reader , ByteWriter writer , String [] huffmanCodes ) throws IOException {
114+ private void encodeAndWriteContent (ByteReader reader , ByteWriter writer ) throws IOException {
108115 StringBuilder bitBuffer = new StringBuilder ();
109116 Byte currentByte ;
110117
111118 while ((currentByte = reader .readNextByte ()) != null ) {
112- String huffmanCodeOfCurrentByte = huffmanCodes [CommonUtil .byteToUnsignedInt (currentByte )];
119+ String huffmanCodeOfCurrentByte = this . huffmanCodes [CommonUtil .byteToUnsignedInt (currentByte )];
113120 bitBuffer .append (huffmanCodeOfCurrentByte );
114121
115122 while (bitBuffer .length () >= Constants .BITS_PER_BYTE ) {
@@ -123,25 +130,25 @@ private void encodeAndWriteContent(ByteReader reader, ByteWriter writer, String[
123130 }
124131 }
125132
126- private void compressFile (String inputFilePath , String outputFilePath , int [] frequency , String [] huffmanCodes ) {
133+ private void compressFile () {
127134 logger .info ("Compressing file: {} -> {}" , inputFilePath , outputFilePath );
128135 try (ByteReader reader = new ByteReader (inputFilePath );
129136 ByteWriter writer = new ByteWriter (outputFilePath )) {
130137
131138 // Step1: Write the table size
132139 logger .debug ("Writing frequency table" );
133- writeTableSize (writer , frequency );
140+ writeTableSize (writer );
134141
135142 // Step2: Write the table
136- writeFrequencyTable (writer , frequency );
143+ writeFrequencyTable (writer );
137144
138145 // Step3: Write extra bits needed for padding
139146 logger .debug ("Writing padding information" );
140- writeExtraBits (writer , frequency , huffmanCodes );
147+ writeExtraBits (writer );
141148
142149 // Step4: Encode and write the compressed content
143150 logger .debug ("Encoding and writing compressed content" );
144- encodeAndWriteContent (reader , writer , huffmanCodes );
151+ encodeAndWriteContent (reader , writer );
145152
146153 logger .info ("Compression completed successfully" );
147154 } catch (IOException e ) {
@@ -158,21 +165,7 @@ private void compressFile(String inputFilePath, String outputFilePath, int[] fre
158165 */
159166 @ Override
160167 public void compress () {
161- String outputFilePath = inputFilePath + Constants .HUFFMAN_FILE_EXTENSION ;
162- compressFile (inputFilePath , outputFilePath , frequency , huffmanCodes );
163- }
164-
165- /**
166- * Compresses the file to a specific output path
167- *
168- * @param outputFilePath The path where the compressed file will be saved
169- * @throws RuntimeException if compression fails
170- * @throws IllegalArgumentException if outputFilePath is null or invalid
171- */
172- @ Override
173- public void compress (String outputFilePath ) {
174- CommonUtil .validateOutputFilePath (outputFilePath );
175- compressFile (inputFilePath , outputFilePath , frequency , huffmanCodes );
168+ compressFile ();
176169 }
177170
178171 /**
0 commit comments