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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:57:19+00:00 2026-05-24T06:57:19+00:00

We are using Java ByteBuffer for socket communication with a C++ server. We know

  • 0

We are using Java ByteBuffer for socket communication with a C++ server. We know Java is Big-endian and Socket communication is also Big-endian. So whenever the byte stream received and put into a ByteBuffer by Java, we call getInt() to get the value. No problem, no conversion.

But if somehow we specifically set the ByteBuffer byte order to Little-endian (my co-worker actually did this),

  1. will the Java automatically convert the Big-endian into the Little-endian when the data is put into the ByteBuffer?

  2. Then the getInt() of the Little-endian version will return a right value to you?

I guess the answer to above two questions are yes. But when I try to verify my guessing and try to find how the getInt() works in ByteBuffer, I found it is an abstract method. The only subclass of ByteBuffer is the MappedByteBuffer class which didn’t implement the abstract getInt(). So where is the implementation of the getInt() method?

For the sending, because we are using Little-endian ByteBuffer, we need to convert them into a Big-endian bytes before we put onto the socket.

  • 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-24T06:57:19+00:00Added an answer on May 24, 2026 at 6:57 am

    So where is the implementation of the getInt() method?

    ByteBuffer is indeed an abstract class. There are several way in which byte buffers can be created:

    • allocate;
    • wrap;
    • allocateDirect.

    In my JDK, these create instances of internal classes HeapByteBuffer and DirectByteBuffer. Their respective getInt functions are as follows:

    // HeapByteBuffer
    
    public int getInt() {
        return Bits.getInt(this, ix(nextGetIndex(4)), bigEndian);
    }
    
    public int getInt(int i) {
        return Bits.getInt(this, ix(checkIndex(i, 4)), bigEndian);
    }
    

    and

    // DirectByteBuffer
    
    private int getInt(long a) {
        if (unaligned) {
            int x = unsafe.getInt(a);
            return (nativeByteOrder ? x : Bits.swap(x));
        }
        return Bits.getInt(a, bigEndian);
    }
    
    public int getInt() {
        return getInt(ix(nextGetIndex((1 << 2))));
    }
    
    public int getInt(int i) {
        return getInt(ix(checkIndex(i, (1 << 2))));
    }
    

    In the above, nativeByteOrder and bigEndian are two boolean members indicating respectively — and somewhat redundantly — whether the configured byte order: (a) matches the native byte order; (b) is big endian.

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

Sidebar

Related Questions

By using java reflection, we can easily know if an object is an array.
I am using IBM Websphere Application Server v6 and Java 1.4 and am trying
I've a big problem. I using this C# function to encode my message: byte[]
I have a Java client that connects to a C++ server using TCP Sockets
I'm writing a program using Java non-blocking socket and TCP. I understand that TCP
I'm creating a small server using java.nio , but when trying to stress test
Java Socket Server I have a Java process that is creating a listener on
I am using Java NIO for my socket connections, and my protocol is text
I need to put the contents of a java.nio.ByteBuffer into an java.io.OutputStream . (wish
Using Java, how can I test that a URL is contactable, and returns 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.