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

  • Home
  • SEARCH
  • 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 3870972
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:46:44+00:00 2026-05-19T21:46:44+00:00

OK, so I have a socket InputStream over which the server is sending a

  • 0

OK, so I have a socket InputStream over which the server is sending a data stream containing a number of lines of header text followed by a binary stream of bytes making up the pdf file it is sending (of a length specified in the header section). The server, which I can’t control, does not close the data stream after it sends its data so i must read the exact amount of bytes from the stream and then close it myself from the client end.

So, my question is, how do you or are there any utilities which will allow me to easily read the headers (as text) and then read an exact amount of bytes from the same input sream?

I’ve tried various Reader classes which work great for the headers, but as I’ve learned not so great for the binary content of the data (Readers work with characters not bytes). Utilities such as apache commons IOUtils don’t work for me because the stream remains open/unterminated and attempts at IOUtils.toBytes(inputStream) hang indefinitely.

The solution seems to be to work with Stream classes rather than Reader classes, but it seems so low level that there must be utilities out there to help me with this. Reading the binary data using a DataInputStream seems easy enough, but I’m stumped as to how to read the headers. Any advice?

EDIT: Here is a sample message:

http/1.0 200 OK
content-type: application/doc_request
content-length: 18813
session-id: slukdcy71292645678312
remote-addr: slukdcy7

<pdf binary data...>

The new line between the headers and binary data determine the end of the headers and the start of the binary data.

  • 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-05-19T21:46:45+00:00Added an answer on May 19, 2026 at 9:46 pm

    You can convert binary bytes to text. I suggest you read all the data as binary and convert the header to text from the binary for the header.

    EDIT: here is a sample solution. It assumes that all headers are as you suggested and the files are small enough to fit into memory. You may want to buffer your input stream.

    public class HttpFile {
        public final String status;
        public final Map<String, String> properties;
        public final byte[] data;
    
        public HttpFile(String status, Map<String, String> properties, byte[] data) {
            this.status = status;
            this.properties = properties;
            this.data = data;
        }
    
        public static HttpFile readFrom(DataInputStream dis, Charset charset) throws IOException {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int ch;
            while((ch = dis.read()) != -1) {
                baos.write(ch);
                if (ch == '\n') {
                    ch = dis.read();
                    // the second newline??
                    if (ch == '\n')
                        break;
                    baos.write(ch);
                }
            }
            String header = new String(baos.toByteArray(), charset);
            String[] lines = header.split("\\n");
            String status = lines[0];
            Map<String, String> properties = new LinkedHashMap<String, String>();
            for(int i=1;i<lines.length;i++) {
                String[] keyValue = lines[i].split(": ",2);
                properties.put(keyValue[0], keyValue[1]);
            }
            byte[] data = null;
            String content_length = properties.get("context-length");
            if (content_length != null) {
                int length = Integer.parseInt(content_length);
                dis.readFully(data = new byte[length]);
            }
            return new HttpFile(status, properties, data);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function for sending data over Socket class in Java. When I
I have a server that sends data via a socket, the data is a
I have a class which I'm serialising to send over a unix socket and
I have a socket server which I am writing in C++ and a client
I have a Java-app with an InputStream, which is copying data to an OutputStream.
I have a socket server that I am trying to move over to SSL
I have a class that encapsulates tcp socket communications with a server. For each
We have a simple project where we read data from a socket and we
I have a server that listens for a connection on a socket: public class
I have a Java program that opens a socket connection to a server that

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.