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 127437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:28:35+00:00 2026-05-11T05:28:35+00:00

I am writing a simple proxy in Java. I am having trouble reading the

  • 0

I am writing a simple proxy in Java. I am having trouble reading the entirety of a given request into a byte array. Specifically, in the following loop, the call to ‘read’ blocks even though the client has sent all the data that it will (that is, the end of stream is never reached). As I can’t be sure that it is time to start writing output until I’ve read the entirety of the input, this is causing a bit of trouble. If I kill the connection to the server, the end of stream is finally reached, and everything goes off without a hitch (all of the data from the client, in this case Firefox requesting http://www.google.com, has been read by the server, and it is able to process it as required, though obviously it can’t send anything back to the client).

public static void copyStream(InputStream is, OutputStream os) throws IOException {     int read = 0;     byte[] buffer = new byte[BUFFER_SIZE];     while((read = is.read(buffer, 0, BUFFER_SIZE)) != -1)     {       os.write(buffer, 0, read);     }     return; } 

The InputStream comes from the client socket (getInputStream(), then buffered) directly; the OutputStream is a ByteArrayOutputStream.

What am I doing wrong?

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T05:28:36+00:00Added an answer on May 11, 2026 at 5:28 am

    Typically in HTTP the Content-Length header indicates how much data you’re supposed to read from the stream. Basically it tells you how many bytes follow the double-newline (actually double-\r\n) that indicates the end of the HTTP headers. See W3C for more info…

    If there is no Content-Length header sent, you could try interrupting the read after a certain amount of time passes with no data sent over the connection, although that’s definitely not preferable.

    (I’m assuming that you’re going to be processing the data you’re reading somehow, otherwise you could just write out each byte as you read it)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 191k
  • Answers 191k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I actually figured out a "quick" way to do this.… May 12, 2026 at 6:08 pm
  • Editorial Team
    Editorial Team added an answer Why not just use the WebClient class and its DownloadFile… May 12, 2026 at 6:08 pm
  • Editorial Team
    Editorial Team added an answer This is not encoding, it's html entities hexadecimal codes. try… May 12, 2026 at 6:08 pm

Related Questions

We have a simple Webapp running on two Tomcat instances behind Apache (a Tomcat
I am writing a command-line tool for Windows that uses libcurl to download files
I am writing a simple checkers game in Java. When I mouse over the
I am writing a simple database with web access. I have previous experience with

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.