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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:30:17+00:00 2026-06-09T05:30:17+00:00

I need to convert the content of an InputStream into a String. The difficulty

  • 0

I need to convert the content of an InputStream into a String. The difficulty here is the input encoding, namely Latin-1. I tried several approaches and code snippets with String, getBytes, char[], etc. in order to get the encoding straight, but nothing seemed to work.

Finally, I came up with the working solution below. However, this code seems a little verbose to me, even for Java. So the question here is:

Is there a simpler and more elegant approach to achieve what is done here?

private String convertStreamToStringLatin1(java.io.InputStream is)
        throws IOException {

    String text = "";

    // setup readers with Latin-1 (ISO 8859-1) encoding
    BufferedReader i = new BufferedReader(new InputStreamReader(is, "8859_1"));

    int numBytes;
    CharBuffer buf = CharBuffer.allocate(512);
    while ((numBytes = i.read(buf)) != -1) {
        text += String.copyValueOf(buf.array(), 0, numBytes);
        buf.clear();
    }

    return text;
}
  • 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-09T05:30:19+00:00Added an answer on June 9, 2026 at 5:30 am

    Firstly, a few criticisms of the approach you’ve taken already. You shouldn’t unnecessarily use an NIO CharBuffer when you merely want a char[512]. You don’t need to clear the buffer each iteration, either.

    int numBytes;
    final char[] buf = new char[512];
    while ((numBytes = i.read(buf)) != -1) {
        text += String.copyValueOf(buf, 0, numBytes);
    }
    

    You should also know that just constructing a String with those arguments will have the same effect, as the constructor too copies the data.

    The contents of the subarray are copied; subsequent modification of the character array does not affect the newly created string.


    You can use a dynamic ByteArrayOutputStream which grows an internal buffer to accommodate all the data. You can then use the entire byte[] from toByteArray to decode into a String.

    The advantage is that deferring decoding until the end avoids decoding fragments individually; while that may work for simple charsets like ASCII or ISO-8859-1, it will not work on multi-byte schemes like UTF-8 and UTF-16. This means it is easier to change the character encoding in the future, since the code requires no modification.

    private static final String DEFAULT_ENCODING = "ISO-8859-1";
    
    public static final String convert(final InputStream in) throws IOException {
      return convert(in, DEFAULT_ENCODING);
    }
    
    public static final String convert(final InputStream in, final String encoding) throws IOException {
      final ByteArrayOutputStream out = new ByteArrayOutputStream();
      final byte[] buf = new byte[2048];
      int rd;
      while ((rd = in.read(buf, 0, 2048) >= 0) {
        out.write(buf, 0, rd);
      }
      return new String(out.toByteArray(), 0, encoding);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to convert a Joda-Time DateTime into a String, in the following format:
I have an ArrayList of Path values. I need to convert it into String
I need to convert a image to base64binary content. Can we do it in
I need to convert a String to enum. I followed the idea in String
I need to convert a string vector in a simple string. I do not
I need to convert a time difference (diff) between two actions into a human
I need to convert a column data into single row with multiple columns Example
I need to convert hierarchical data (AVRO data, which boils down to JSON) into
I need to convert keys from type string to hash. The name of all
I have a list of Phone Numbers which I need to convert into a

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.