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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:21:35+00:00 2026-06-07T11:21:35+00:00

private void readIncomingMessage() { try { StringBuilder builder = new StringBuilder(); InputStream is =

  • 0
private void readIncomingMessage() {
    try {
        StringBuilder builder = new StringBuilder();
        InputStream is = socket.getInputStream();
        int length = 1024;
        byte[] array = new byte[length];
        int n = 0;

        while ((n = is.read(array, n, 100)) != -1) {
            builder.append(new String(array));

            if (checkIfComplete(builder.toString())) {
                buildListItems(builder.toString(), null);
                builder = new StringBuilder();
            }
        }

    } catch (IOException e) {
        Log.e("TCPclient", "Something went wrong while reading the socket");
    }
}

Hi,

I want to read the stream per block of 100 bytes, convert those bytes into a string and than see if that strings fits certain conditions.

But when I debug I see that builder has a count of 3072.
And I see a string like (text, , , , , , , , , , text , , , , , , , , , text)
How can I just add the text to the stringbuilder?

thx 🙂

 private void readIncomingMessage() {
    try {
        StringBuilder builder = new StringBuilder();
        InputStream is = socket.getInputStream();
        int length = 100;
        byte[] array = new byte[length];
        int n = 0;

        while ((n = is.read(array, 0, length)) != -1) {
            builder.append(new String(array, 0, n));

            if (checkIfComplete(builder.toString())) {
                buildListItems(builder.toString(), null);
                builder = new StringBuilder();
            }
        }

    } catch (IOException e) {
        Log.e("TCPclient", "Something went wrong while reading the socket");
    }
}

this solution did the trick for me.
any drawbacks with this solution?

  • 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-07T11:21:37+00:00Added an answer on June 7, 2026 at 11:21 am

    2 problems:

    1. you need to use the 'n' value when converting the bytes to a String. Specifically, use this String constructor String(byte[] bytes, int offset, int length)
    2. when converting bytes to strings on arbitrary boundaries, like you are doing, you have the potential to corrupt multi-byte characters. You’d be better off putting an InputStreamReader on top if the 'is' and reading characters from that.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

private void btnMiles_Click(object sender, EventArgs e) { try { int milesless200 = int.Parse(txtMiles.Text); int
private void findLDS() { Integer[] array = Arrays.copyOf(elephants.iq, elephants.iq.length); Hashtable<Integer, Integer> eq = elephants.elephantiqs;
private void btnStart_Click(object sender, EventArgs e) { Random random = new Random(); int randomNumber
private void sessionText() { try { System.IO.TextReader r = new System.IO.StreamReader(saved.txt); this.textBox1.Text = r.ReadLine();
private void btnGo_Click(object sender, EventArgs e) { Array IDlist = txtUserID.Text.Split(new char[] { });
private void DoSomeWork() { ManualResetEvent[] waitEvents = new ManualResetEvent[rowCount]; int i = 0; foreach
private void drawGrid(){ for(int i = 0; i<3; i++){ GLine line = new GLine(0,0,21*i,211*i);
private void test() { int index = 0; int length = 0; while (true)
private void button1_Click(object sender, EventArgs e) { int[] ml = new int[10] ( 1,
private void pushButtonActionPerformed(java.awt.event.ActionEvent evt) { final int c=0; final JDialog d=new JDialog(); JLabel l=new

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.