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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:16:02+00:00 2026-06-01T23:16:02+00:00

I am using a queue service that only takes messages as byte so I

  • 0

I am using a queue service that only takes messages as byte so I need to convert my data quickly to the format and then make it back to its original when I receive work from the queue. My data format is a INT, DOUBLE, and INT[] and here’s how I did it at first

//to convert to string
String[] message = { Integer.toString(number), String.valueOf(double), Arrays.toString(my_list) };
message.asString;
//to convert back
String message_without_brackets = message.replace("[", "" ).replace("]", "");
String[] temp_message = message_without_brackets.split(","); 

int interger = Integer.valueOf(temp_message[0]);
double double = Double.valueOf(temp_message[1]);
int[] my_list = new int[temp_message.length-2]; //-2 because the first two entries are other data

for (int i = 2; i < temp_message.length; i++) {
    my_list[i-2] = Integer.parseInt(temp_message[i].replace(" ",""));
}

This is super ugly and it annoyed me that after a few weeks(or a single night of heavy drinking) I would probably not be able to figure out this quickly. Performance wise the code wasn’t too bad, I think replace was the heaviest part of the code(if I remember it was like 15% of overall execution).

I asked around and found Gson to be able to do this cleaner but the performance is now over 40% of my loop now(its Gson itself thats doing it):

    Gson gson = new Gson();
    int[] sub = { 0, 59, 16 };
    Object[] values = { 0, 43.0, sub };
    String output = gson.toJson(values); // => [0, 43.0,[0,59,16]]

    Object[] deserialized = gson.fromJson(output, Object[].class);
    System.out.println(deserialized[0]);
    System.out.println(deserialized[1]);
    System.out.println(deserialized[2]);

So I’m wondering if there’s faster way to get the same result?I am trying out a few of the suggestions in this question but is there a faster way to do this without depending on any external libraries as my needs are quite simple(if not, then is there a fast one)? Because someone suggested Gson, I looked at Json parsers, but is that what I should be looking for or is are there other types of libraries that do this?

EDIT: I am converting it to string because I thought I needed to do that to send it as getBytes(), is there any other format that would be faster that I can use getBytes() on?

  • 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-01T23:16:04+00:00Added an answer on June 1, 2026 at 11:16 pm

    You can use a DataOutputStream like

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    
    int i = 0;
    double d = 43.0;
    int[] sub = { 0, 59, 16 };
    
    dos.writeInt(i);
    dos.writeDouble(d);
    dos.write(sub.length);
    for (int j : sub)
        dos.writeInt(j);
    
    byte[] bytes = baos.toByteArray();
    

    If you want to get more extreme you can use recycled ByteBuffers or even direct ByteBuffers and use native byte ordering. etc.

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

Sidebar

Related Questions

I'm using a queue service that seems to only allow me to send a
I have a Windows Service that retrieves messages from a RabbitMQ queue. The service
I am using a System.Threading.ThreadPool to manage a queue of jobs from a service.
I launch a method that is, essentially, an endless loop using dispatch_queue_create and then
I am consuming a rate-limited web service, which only allows me to make 5
I have a .NET 4 C# service that is using the TPL libraries for
I have a table that I am using to queue information before it is
I have a Windows service that uses the producer/consumer queue model with multiple worker
I have a number of commands that make calls to a soap web service
I'm using Service Broker as a messaging queue for a job engine. When the

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.