I have a process that writes to a gz file continuously. However, I want to read from the file and print on screen. If there's no activity, the program will wait. Once the file gets updated by another process, my process will start reading and print the updated contents on console
Have tried implementing this via the following
First thread that keeps putting data from source ( a really big gz file) into an intermediate file
Second thread that continuously reads data from intermediate file and prints output to console
Here's the file
import java.io.*;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class Main {
final String sourceFile = "D:/test/sample.json.gz";
final String intermediateFile = "D:/test/continuous.gz";
//First thread that runs from large source file
Runnable r1 = ()-> {
try {
GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(sourceFile));
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(new FileOutputStream(intermediateFile));
byte[] buf = new byte[1024];
int length = 0;
while ((length = gzipInputStream.read(buf)) > 0) {
gzipOutputStream.write(buf, 0, length);
}
gzipInputStream.close();
}
catch (Exception e){
e.printStackTrace();
System.out.println("Exception while reading json");
}
};
//Second thread that reads intermediate file continuously and prints contents to screen
Runnable r2 = ()-> {
try {
GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(intermediateFile));
byte[] buf = new byte[1024];
int length = 0;
while ((length = gzipInputStream.read(buf)) > 0) {
System.out.print(String.valueOf(buf));
}
gzipInputStream.close();
}
catch (Exception e){
System.err.println("Error while writing to console");
e.printStackTrace();
}
};
public static void main(String[] args) throws Exception {
try{
Main main = new Main();
new Thread(main.r1).start();
Thread.sleep(10);
new Thread(main.r2).start();
}
catch (Exception e){
System.out.println("Main ended");
e.printStackTrace();
}
}
}
However, on running this , I am getting the following issue :
Error while writing to console
java.io.EOFException: Unexpected end of ZLIB input stream
at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:117)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at Main.lambda$new$1(Main.java:35)
at java.lang.Thread.run(Thread.java:748)
Any assistance would be of great help