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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:12:04+00:00 2026-06-17T17:12:04+00:00

I want to read the first x bytes from a java.net.URLConnection (although I’m not

  • 0

I want to read the first x bytes from a java.net.URLConnection (although I’m not forced to use this class – other suggestions welcome).

My code looks like this:

val head = new Array[Byte](2000)  
new BufferedInputStream(connection.getInputStream).read(head)
IOUtils.toString(new ByteArrayInputStream(head), charset)

It works, but does this code load only the first 2000 bytes from the network?

Next trial

As ‘JB Nizet’ said it is not useful to use a buffered input stream, so I tried it with an InputStreamReader:

val head = new Array[Char](2000)  
new InputStreamReader(connection.getInputStream, charset).read(head)
new String(head)

This code may be better, but the load times are about the same. So does this procedure limit the transferred bytes ?

  • 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-17T17:12:05+00:00Added an answer on June 17, 2026 at 5:12 pm

    You can use read(Reader, char[]) from Apache Commons IO. Just pass a 2000-character buffer to it and it will fill it with as many characters as possible, up to 2000.

    Be sure you understand the objections in the other answers/comments, in particular:

    • Don’t use Buffered... wrappers, it goes against your intentions.
    • If you read textual data, then use a Reader to read 2000 characters instead of InputStream reading 2000 bytes. The proper procedure would be to determine the character encoding from the headers of a response (Content-Type) and set that encoding into InputStreamReader.
    • Calling plain read(char[]) on a Reader will not fully fill the array you give to it. It can read as little as one character no matter how big the array is!
    • Don’t forget to close the reader afterwards.

    Other than that, I’d strongly recommend you to use Apache HttpClient in favor of java.net.URLConnection. It’s much more flexible.


    Edit: To understand the difference between Reader.read and IOUtils.read, it’s worth examining the source of the latter:

    public static int read(Reader input, char[] buffer,
                           int offset, int length)
        throws IOException
    {
        if (length < 0) {
            throw new IllegalArgumentException("Length must not be negative: " + length);
        }
        int remaining = length;
        while (remaining > 0) {
            int location = length - remaining;
            int count = input.read(buffer, offset + location, remaining);
            if (EOF == count) { // EOF
                break;
            }
            remaining -= count;
        }
        return length - remaining;
    }
    

    Since Reader.read can read less characters than a given length (we only know it’s at least 1 and at most the length), we need to iterate calling it until we get the amount we want.

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

Sidebar

Related Questions

I have one binary file and I want to read this file like first
Possible Duplicate: File to byte[] in Java I want to read data from file
this is my first question, although I've already used so many tips from Stack
I want to read N bytes of data from a file stream and append
I want to override or switch some bytes (say the first 2048 bytes from
H, How to Read First 512 Bytes of data from a .dat file in
I want to read a sequence of bytes from my accelerometer. I can't get
Hi I can't understand this error I want read this XML file: <?xml version=1.0
I want to read a INI file from python, and I came up with
I want to read the pdf file from raw folder if devices have any

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.