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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:11:24+00:00 2026-05-26T14:11:24+00:00

I need to aggregate 2 Strings and a List into a single byte[] in

  • 0

I need to aggregate 2 Strings and a List into a single byte[] in order to send it through the network (using a special library that has a function send(byte[]).

Then, on the other end, I need to get the 3 different objects back.

I’ve done an ugly implementation of it, but it is very slow. Basically, what I do is

        public byte[] myserializer(String dataA, String dataB, List<byte[]> info) {

        byte[] header = (dataA +";" + dataB + ";").getBytes();

        int numOfBytes = 0;
        for (byte[] bs : info) {
            numOfBytes += bs.length;
        }

        ByteArrayOutputStream b = new ByteArrayOutputStream();
        ObjectOutputStream o;
        try {
            o = new ObjectOutputStream(b);
            o.writeObject(info);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        byte[] data = b.toByteArray();

        int length = header.length + data.length;
        byte[] headerLength = (new Integer(header.length)).toString()
                .getBytes();
        byte[] pattern = ";".getBytes();
        int finalLength = headerLength.length + pattern.length + length;

        byte[] total = new byte[finalLength];enter code here
        total = // Copy headerLength, header and total into byte[] total

        return return;

In essence I’m creating kind of a frame that looks like this

      HEADER                    INFO

(———————————————–)(———————————-)
HEADER_LENGHT;DATA_A;DATA_B;SERIALIZED_LIST_OBJECT

Then, on the receiver side, I do the inverse process and that’s “all”. This works, but it is PRETTY inefficient and ugly.

Suggestions? Best practices? Ideas?

Oh…just one note more: this have to work for J2SE and Android too

Thanks so much in advanced!!

  • 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-26T14:11:26+00:00Added an answer on May 26, 2026 at 2:11 pm

    Here is a simplistic method of serializing an array of bytes and deserialize it on the other side. Note that the method only accept one argument of type List<byte[]> and since your argument dataA and dataB are of type String, you can simply assume that the two first byte[] elements in the list are those two argument. I believe that this is a lot faster than object serialization through an ObjectOutputStream and will be faster to deserialize on the other side as well.

    public class ByteListSerializer {
    static private final int INT_SIZE = Integer.SIZE / 8;
    
        static public void main(String...args) {
            ByteListSerializer bls = new ByteListSerializer();
    
            // ============== variable declaration =================
            String dataA = "hello";
            String dataB = "world";
            List<byte[]> info = new ArrayList<byte[]>();
            info.add(new byte[] {'s','o','m','e'});
            info.add(new byte[] {'d','a','t','a'});
            // ============= end variable declaration ==============
    
            // ======== serialization =========
            info.add(0, dataA.getBytes());
            info.add(1, dataB.getBytes());
            byte[] result = bls.dataSerializer(info);
    
            System.out.println(Arrays.toString(result));
    
            // ======== deserialization ========
            List<byte[]> back = bls.dataDeserializer(result);
    
            String backDataA = new String(back.get(0));
            String backDataB = new String(back.get(1));
            back.remove(0);
            back.remove(0);
    
            // ============ print end result ============
            System.out.println(backDataA);
            System.out.println(backDataB);
            for (byte[] b : back) {
                System.out.println(new String(b));
            }
        }
    
        public byte[] dataSerializer(List<byte[]> data) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ByteBuffer lenBuffer = ByteBuffer.allocate(4);
    
            try {
                for (byte[] d : data) {
                    lenBuffer.putInt(0, d.length);
                    out.write(lenBuffer.array());
                    out.write(d);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            // wrap this
            byte[] dataBuffer = new byte[out.size() + 4];
            lenBuffer.putInt(0, out.size());
            System.arraycopy(lenBuffer.array(), 0, dataBuffer, 0, 4);
            System.arraycopy(out.toByteArray(), 0, dataBuffer, 4, out.size());
    
            return dataBuffer;
        }
    
        public List<byte[]> dataDeserializer(byte[] data) {
            if (data.length < INT_SIZE) {
                throw new IllegalArgumentException("incomplete data");
            }
    
            ByteBuffer dataBuffer = ByteBuffer.wrap(data);
            int packetSize = dataBuffer.getInt();
    
            if (packetSize > data.length - INT_SIZE) {
                throw new IllegalArgumentException("incomplete data");
            }
    
            List<byte[]> dataList = new ArrayList<byte[]>();
            int len, pos = dataBuffer.position(), nextPos;
    
            while (dataBuffer.hasRemaining() && (packetSize > 0)) {
                len = dataBuffer.getInt();
                pos += INT_SIZE;
                nextPos = pos + len;
                dataList.add(Arrays.copyOfRange(data, pos, nextPos));
    
                dataBuffer.position(pos = nextPos);
                packetSize -= len;
            }
    
            return dataList;
        }
    }
    

    The frame is constructed as

         - 4 bytes: the total bytes to read (frame size = [nnnn] + 4 bytes header)
        |      - 4 bytes: the first chunk size in bytes
        |     |          - x bytes: the first chunk data
        |     |         |          
        |     |         |           - 4 bytes: the n chunk size in byte
        |     |         |          |         - x bytes: the n chunk data
        |     |         |          |        |
        |     |         |          |        |
    [nnnn][iiii][dddd....][...][iiii][dddd...]
    

    The example above will output

    [0, 0, 0, 34, 0, 0, 0, 5, 104, 101, 108, 108, 111, 0, 0, 0, 5, 119, 111, 114, 108, 100, 0, 0, 0, 4, 115, 111, 109, 101, 0, 0, 0, 4, 100, 97, 116, 97]
    hello
    world
    some
    data
    

    Note that the frame format is formed of byte[] chunks so as long as you know the order of the chunks, you can use these methods with virtually any set of data.

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

Sidebar

Related Questions

I have a database for which I need to aggregate records into another smaller
Often you need to show a list of database items and certain aggregate numbers
For a current project I am working I need to return an aggregate report
Need a function that takes a character as a parameter and returns true if
Need a way to allow sorting except for last item with in a list.
Need to an expression that returns only things with an I followed by either
Need a function like: function isGoogleURL(url) { ... } that returns true iff URL
I have looked into pivot but I think it requires an aggregate function which
I need to apply two successive aggregate functions to a dataset (the sum of
An aggregate type T is made up of 4 strings: t = c1 c2

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.