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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:04:50+00:00 2026-06-08T08:04:50+00:00

Sorry if I’m missing something obvious here…but please take a look at this code

  • 0

Sorry if I’m missing something obvious here…but please take a look at this code snippet:

String readString;
String writeString = "O hai world.";
BufferedReader br = new BufferedReader(
    new InputStreamReader( 
        new ByteArrayInputStream(writeString.getBytes()),
        "UTF-8"),
    1024);
readString = br.readLine();
System.out.println("readString: " + readString);

I’d expect this to print "readString: null" since I thought the BufferedReader would encounter an EOF before detecting a valid EOL, but instead this prints "readString: O hai world". This seems contrary to what the Javadocs for BufferedReader say readLine() will do:

Reads a line of text. A line is considered to be terminated by any one of a line feed (‘\n’), a carriage return (‘\r’), or a carriage return followed immediately by a linefeed.

Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

I can’t see any reason why my string would be re-interpreted to terminate with ‘\n’ and/or ‘\r’…can someone please illuminate me? Thanks!

EDIT:
To provide some context, I’m trying to write JUnit tests to validate a Reader class that I wrote that’s designed to read on System.in. Using ByteArrayInputStreams seemed like a reasonable way to simulate System.in (see this relevant SO post).

When my Reader captures a line, it currently relies on BufferedReader.readLine(). For my purposes, my Reader’s lines MUST all have been terminated with ‘\n’ or ‘\r’; encountering EOF without an EOL should not resolve into a valid line. So I guess my question(s) at this point are really as follows (I’ll try to test these myself in greater detail when I have time, but hoping you smart folks can help me out):

  • Is BufferedReader.readLine() broken/misdocumented? Or is ByteArrayInputStream returning something erroneous when its byte array is exhausted?
  • Is this method of testing my Reader erroneous, and should I expect readLine() to function properly when used against System.in? I’m inclined to believe the answer to this is yes.
  • Are there better ways to simulate System.in for unit testing?
  • If I need to strictly discriminate against ‘\n’ and ‘\r’ when reading from an InputStream, am I better off writing my own readLine() method? I’d be very surprised if this is the case.

Thanks again!

  • 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-08T08:04:53+00:00Added an answer on June 8, 2026 at 8:04 am

    The ByteArrayInputStream doesn’t return EOL when it’s exhausted. It only returns -1 which might be considered EOF.

    The thing is that the BufferedReader buffers all it reads from the input stream and if the EOF(-1) is encountered before any EOL character showed up, it returns the string buffered up to that point.

    So, if you want to be very strict, you can say that readLine() is either broken according to the current documentation or that it should be documented differently if this was the intended behavior.

    In my opinion, considering that the last line in a stream doesn’t have to end with an EOL character (EOF being enough) the current behavior of readLine is correct, i.e a line was read because EOF was encountered. So, the documentation should be changed.

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

Sidebar

Related Questions

Sorry if this is a noob question, but All my code is error-free and
Sorry if this is too obvious but I’ve been too long on it and
sorry if this is an obvious question, but I am relatively new to python
Sorry for this simple question, but I can't solve it... There is an example:
Sorry, I know this question is easy, but I don't know how to get
Sorry but I am not sure how to ask this question but I am
Sorry if this question seems stupid, but it's stumped me for a couple days,
Sorry if this is a stupid question, but is there an easy way to
Sorry to bother you all here but I am still trying to understand more
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.