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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:21:08+00:00 2026-05-23T16:21:08+00:00

Working with the Android Profiler tool, I’ve discovered that I’m using lots of ByteBuffers

  • 0

Working with the Android Profiler tool, I’ve discovered that I’m using lots of ByteBuffers in a performance critical area of my code. I’ve worked on optimizing as much as possible of the largest performance hits (haven’t been forced to go to stuff like loop optimization yet) and the ByteBuffer code looks like it could easily be replaced with another/a better solution.

The Problem: Getting an int from 4 consecutive bytes located in the ByteBuffer stream, which could be of any length, currently done (seen below, specifically //<<< parts) with a temp ByteBuffer and grabbing needed bytes to form an int. Is there any simpler process?

private int peekStreamData(ByteBuffer stream, int count) {
    int data = 0;
    int stream_field = streamField;
    int stream_field_bit_index = streamFieldBitIndex;
    byte[] streamArr = stream.array();
    ByteBuffer bb = ByteBuffer.allocate(4);
    while (count > (32 - stream_field_bit_index) && streamIndex < (imageStream.capacity() >> 2)) {
        data = (data << (32 - stream_field_bit_index)) | ( stream_field >>> stream_field_bit_index);
        count -= 32 - stream_field_bit_index;

        bb.put(0,streamArr[streamIndex * 4 + 3]);  //<<<
        bb.put(1,streamArr[streamIndex * 4 + 2]);  //<<<
        bb.put(2,streamArr[streamIndex * 4 + 1]);  //<<<
        bb.put(3,streamArr[streamIndex * 4 + 0]);  //<<<
        stream_field = bb.getInt();                //<<<        
        stream_field_bit_index = 0;
    }
    if (count > 0)
        data = (data << count) | (stream_field >>> (32 - count));
    return data;//data;
}

Please feel free to point out any mistakes. I’ve never worked with ByteBuffers until quite recently and would love to learn.

  • 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-23T16:21:09+00:00Added an answer on May 23, 2026 at 4:21 pm

    Have you tried:

    stream_field = (streamArr[streamIndex * 4 + 0] & 0xFF) | ((streamArr[streamIndex * 4 + 1] & 0xFF) << 8) | ((streamArr[streamIndex * 4 + 2] & 0xFF) << 16) | ((streamArr[streamIndex * 4 + 3] & 0xFF) << 24);
    

    or:

    stream_field = ((streamArr[streamIndex * 4 + 0] & 0xFF) << 24) | ((streamArr[streamIndex * 4 + 1] & 0xFF) << 16) | ((streamArr[streamIndex * 4 + 2] & 0xFF) << 8) | (streamArr[streamIndex * 4 + 3] & 0xFF);
    

    depending on endiannes.

    EDIT: In Java byte is signed, so they need masking. Corrected.

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

Sidebar

Related Questions

I am working on an Android app that utilizes the Google Maps API MapView,
I am working on a XMPP client on Android, using the Smack library. The
Hi I am working Android application development using titanium studio.I have developed small application.my
I am working on android geolocation application in that I have added marker for
I am a working on android application development. I am using eclipse IDE. I
I am working on performance benchmarking of a SDIO UART Linux/Android driver and used
I'm working on making an iPhone version of an Android app that I've written
I am working android application my problem is i am using handlers this handler
im working on android and on getting response from http my xml code contain
I am working in Android using Open GL ES. I have been trying to

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.