How can I wrap a GZipInputStream with a BufferedInputStream?

I have FileReadClient

one that reads a file like this:

     public synchronized int read (byte buf[], int off, int len) throws IOException {
        if (br == null){
          if (useGZIP) {
            br = new BufferedInputStream(new GZIPInputStream(blockStream));
          } else {
           br = new BufferedInputStream(blockStream);
          }
        }
    int result = br.read(buf, off, len);
    return result;
    }

      

Here is mine FileDownloadServlet

, which reads the contents of a file and writes it toServletOutputStream

  InputStream fileStream; //This is an instance of `FileReadClient`
  int c = 0;
  int BUF_LEN = 4096;
  byte bufr[] = new byte[BUF_LEN];
  while ((c = fileStream.read(bufr, 0, BUF_LEN)) != -1) {
    servletOutStream.write(bufr, 0, c);
  }

      

I am saving the file as compressed, so when reading the original file, my file is decompressed using GZIPInputStream

which is inFileReadClient

Case 1:

All I could see was my call read()

FileReadClient

reads 8192

bytes completely , and mine FileDownloadServlet

reads 4096

bytes twice . I couldn't figure out how it reads the bytes first 8192

, although I gave the buffer length as 4096

when calling read ().

Case 2:

Now I am reading a file in compressed format from FileReadClient

and passing it to my input stream

public class MyInputStream extends java.io.InputStream {

String filepath = "";
boolean decompressByFileReadClient = true;
InputStream in;
public MyInputStream(Sting filepath, boolean decompressByFileReadClient) {
  this.filepath = filepath;
  initInputStream();
}
public void initInputStream() {
  this.in = new FileReadClient();   //This reads data as compressed from file read client
  if(!decompressByFileReadClient) {
     this.in = new GZIPInputStream(this.in); //Pass "FileReadClient" input stream to GZIPInputStream to decompress the file content here 
     //This reads only 512 bytes at a time - since the default buffer size for GZIPInputStream is 512 bytes
  }
 }
}

      

Now that I read the content of my file from FileDownloadServlet

with MyInputStream

, I could only read 512 bytes of data, although I wrap it up BufferInputStream

, which makes it BufferInputStream

pointless here.

Now, although the buffer size is 4K while reading, this one MyInputStream

only reads 512 bytes at a time from behind GZIPInputStream

. Thus, the number of read calls increases, which increases the latency of file downloads.

The number of system calls while reading from InputStream we could use BufferedInputStream (which reads the available number of bytes in the input stream until the buffer can hold - reference ). In the same way, how could I reduce the number of calls read in a similar BufferedInputStream

way when I use GZIPInputStream

?

Any suggestions are greatly appreciated! Thank you in advance: -)

+3
java inputstream buffer bufferedinputstream gzipinputstream


source to share


No one has answered this question yet

See similar questions:

49
Why use BufferedInputStream to read a file byte by byte faster than using FileInputStream?

or similar:

3799
How do I read / convert an InputStream to a string in Java?
3324
How to generate random integers in a specific range in Java?
3073
How to efficiently iterate over each entry in a Java map?
2853
How can I convert String to int in Java?
ten
Is this a bug in the Java GZipInputStream class?
3
Reading HttpURLConnection InputStream - manual buffer or BufferedInputStream?
2
BufferedInputStream Mark / Unset Invalid Label
1
Default buffer size (8KB) ​​for "BufferedInputStream"? (JAVA)
0
Difference between the following snippets. How were my initial stream read cycles reduced?
0
Behind the scenes Java BufferedInputStream



All Articles
Loading...
X
Show
Funny
Dev
Pics