Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9144099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:11:11+00:00 2026-06-17T10:11:11+00:00

I am having a strange program with a GzipInputStream zero filling part of the

  • 0

I am having a strange program with a GzipInputStream zero filling part of the buffer. I have the fortune of knowing what the bytes are supposed to look like in the stream and I can see the buffer is being filled with 8 correct bytes and 12 zeros (shouldn’t be zero)

BYTES SHOULD LOOK LIKE THIS—->
0
20
82
22
-91
27
-96
65
66
65
88
32
32
32
32
81
32
0
0
0
100
78

BYTES ACTUALLY LOOK LIKE THIS—>
0
20
82
22
-91
27
-96
65
66
65
0
0
0
0
0
0
0
0
0
0
0
0

The first two bytes represent an integer that determine the size of the variable length (in bytes) payload after the first two. So in this example, the first bytes are 0 20 and in BIG_ENDIAN this gives us a subsequent payload size of 20 bytes.

Here is my code for reading

gzipInputStream = new GZIPInputStream(url.openStream());        

byte[] payload = new byte[2];
gzipInputStream.read(payload);
for(int i=0;i<payload.length;i++){
    System.out.println(payload[i]);
}
int payloadSize = ((payload[0] & 0xFF) << 8) | ((payload[1]) & 0xFF);
//read the next payloadSize bytes
byte[] messageBytes = new byte[payloadSize];

gzipInputStream.read(messageBytes);

So the first two bytes are those in the payload array and the second 20 bytes are those in messageBytes. Can’t figure it out

Modified code thanks to NPE

            byte[] payloadSizeBytes = new byte[2];
            int payloadSizeBytesRead = 0;
            while(payloadSizeBytesRead < 2){
                int r = gzipInputStream.read(buffer);
                if(r>0){
                    payloadSizeBytes[payloadSizeBytesRead] = buffer[0];
                    payloadSizeBytesRead++;
                }
            }
            int payloadSize = ((payloadSizeBytes[0] & 0xFF) << 8) | ((payloadSizeBytes[1]) & 0xFF);
            //read the next payloadSize bytes
            byte[] messageBytes = new byte[payloadSize];
            int messageBytesRead = 0;
            while(messageBytesRead < payloadSize){
                int r = gzipInputStream.read(buffer);
                if(r>0){
                    messageBytes[messageBytesRead] = buffer[0];
                    messageBytesRead++;
                }
            }
            for(int i=0;i<messageBytes.length;i++){
                System.out.println(messageBytes[i]);
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:11:13+00:00Added an answer on June 17, 2026 at 10:11 am

    The contract on read(byte[]) is that it reads some data, and returns how many bytes have been read. As things stand, you are ignoring the return value. Instead, you should examine the return value of read() and keep calling read() until you’ve read payloadSize bytes.

    An easy way to do this is by using read(b, off, len) in a loop:

    int payloadSize = ((payload[0] & 0xFF) << 8) | ((payload[1]) & 0xFF);
    byte[] messageBytes = new byte[payloadSize];
    
    int bytesRead = 0;
    while (bytesRead < payloadSize) {
       bytesRead += gzipInputStream.read(messageBytes, bytesRead, payloadSize - bytesRead);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a very strange problem in a simple program and have been
I am kinda having a strange requirement. I have a google talk bot &
I am having a strange error in the program. However, I do not find
I am having some pretty strange symptoms with JPanels and CardLayout. Essentially, I have
I'm having some strange things happen with my program, and I'm not sure what
I have a strange issue going on as I try to learn and program
I’m having some strange problem with my JS program. I had this working properly
It may sound strange that knowing a lot about iOS and having some experience
I am having a strange problem in my code. I have many asserts scattered
sorry for the cryptic question title. I'm having a strange problem and i have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.