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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:09:56+00:00 2026-05-14T22:09:56+00:00

i was wondering if the solution for this documented here is still the solution

  • 0

i was wondering if the solution for this documented here is still the solution or is there any other way getting an int from 4 bytes?

thank you.

EDIT: im getting the byte[] from sockets .read

EDIT: int recvMsgSize = in.read(Data, 0, BufferSize); if recvMsgSize is -1 i know the connection has been dropped.

how do i detect this when im using DataInputStream instead of InputStream?

thanks.

EDIT: apologies for being a yoyo regarding accepting the right answer. but after mihi’s updated final response, it would appear that the method is solid and cuts down extended coding and in my opinion best practice.

  • 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-05-14T22:09:57+00:00Added an answer on May 14, 2026 at 10:09 pm

    Depending on where you get those 4 bytes from:

    http://docs.oracle.com/javase/7/docs/api/java/io/DataInput.html#readInt()

    http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html#getInt(int)

    You can of course still do it manually, but in most cases using one of those (if you have to convert a byte array with lots of bytes, you might want to use a DataInputStream around a ByteArrayInputStream for example) is easier.

    Edit: If you need to change the endianness, you will have to use a ByteBuffer, or reverse the bytes yourself, or do the conversion yourself, as DataInput does not support changing the endianness.

    Edit2: When you get them from the socket input stream, I’d wrap that one into a DataInputStream and use it for reading all kinds of data. Especially since InputStream.read(byte[]) will not guarantee to fill the whole byte array… DataInputStream.readFully does.

    DataInputStream in = new DataInputStream(socket.getInputStream());
    byte aByte = in.readByte();
    int anInt = in.readInt();
    int anotherInt = in.readInt();
    short andAShort = in.readShort(); // 11 bytes read :-)
    byte[] lotOfBytes = new byte[anInt];
    in.readFully(lotOfBytes);
    

    Edit3: When reading multiple times from a stream, they will continue reading where you stopped, i. e. aByte will be byte 0, anInt will be bytes 1 to 4, anotherInt will be bytes 5 to 8, etc. readFully will read on after all that and will block until it has read lotOfbytes.

    When the stream stops (the connection drops) you will get EOFException instead of -1, so if you get -1, the int really was -1.

    If you do not want to parse any bytes at all, you can skip() them. Parsing one byte in 2 different ways is not possible with DataInputStream (i. e. read first an int from byte 0 to 3, then one from byte 2 to 5), but usually not needed either.

    Example:

    // read messages (length + data) until the stream ends:
    while (true) {
    int messageLength;
    try {
        messageLength = in.readInt(); // bytes 0 to 3
    } catch (EOFException ex) {
        // connection dropped, so handle it, for example
        return;
    }
    byte[] message = new byte[messageLength];
    in.readFully(message);
    // do something with the message.
    }
    // all messages handled.
    

    Hope this answers your additional questions.

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

Sidebar

Related Questions

I'm just wondering if it exists better solution for this. BitConverter.ToInt32(sample_guid.ToByteArray(), 0)
I'm just wondering what the optimal solution is here. Say I have a normalized
I've been wondering whether there is a good "git export" solution that creates a
Wondering if there is any Text to Speech software available as a plug in
Wondering if anybody out there has any success in using the JDEdwards XMLInterop functionality.
I am wondering if anyone knows how to pull this off. Here goes. I
I'm wondering if anybody's found a good solution to this: In our unit tests;
i was just wondering if there was a way to change the fontSize of
Wondering if anyone has a solution to this 2010 bug. I have a project
I was wondering if anyone has a good solution to a problem I've encountered

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.