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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:12:47+00:00 2026-06-08T07:12:47+00:00

I am trying to first read 4 bytes(int) specifying the size of the message

  • 0

I am trying to first read 4 bytes(int) specifying the size of the message and then read the remaining bytes based on the byte count. I am using the following code to accomplish this:

DataInputStream dis = new DataInputStream(
                        mClientSocket.getInputStream());

// read the message length
int len = dis.readInt();

Log.i(TAG, "Reading bytes of length:" + len);

// read the message data
byte[] data = new byte[len];
if (len > 0) {
    dis.readFully(data);
} else {
    return "";
}
return new String(data);

Is there a better/efficient way of doing this?

  • 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-08T07:12:48+00:00Added an answer on June 8, 2026 at 7:12 am

    From JavaDocs of readUTF:

    First, two bytes are read and used to construct an unsigned 16-bit
    *integer* in exactly the manner of the readUnsignedShort method . This
    integer value is called the UTF length and specifies the number of
    additional bytes to be read
    . These bytes are then converted to
    characters by considering them in groups. The length of each group is
    computed from the value of the first byte of the group. The byte
    following a group, if any, is the first byte of the next group.

    The only problem with this is that your protocol seems to only send 4 bytes for the payload length. Perhaps you can do a similar method but increase the size of length sentinel read to 4 bytes/32-bits.

    Also, I see that you are just doing new String(bytes) which works fine as long as the encoding of the data is the same as “the platform’s default charset.” See javadoc So it would be much safer to just ensure that you are encoding it correctly(e.g. if you know that the sender sends it as UTF-8 then do new String(bytes,”UTF-8″) instead).

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

Sidebar

Related Questions

Ok, after failing to read a polynomial, I'm trying first a basic approach to
I was trying for the first time to read data from a file, a
I'm trying to read POST data in a PHP script. My first instincts led
I'm trying to read a BMP file in Python. I know the first two
I am trying to load a *.wav file to a byte array using C#
I'm trying to read a binary file that is in the following format: number
I'm trying to serialize/deserialize string. Using the code: private byte[] StrToBytes(string str) { BinaryFormatter
I'm trying to read a binary file which is a series of bytes and
I need to read a file in blocks of 200 bytes. So i'm using
I am an absolutely newb to GAE and am trying my first sample application.

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.