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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:20:04+00:00 2026-05-31T16:20:04+00:00

I have a client and server communicating via Spring remoting (using Java Serialization) over

  • 0

I have a client and server communicating via Spring remoting (using Java Serialization) over a proprietary messaging system. My server returns large objects so my Spring remoting implementation splits the serialized object byte array into blocks, and sends multiple messages. The client waits for all the response messages for a given request and eventually calls the method below to the deserialize the byte arrays into the resultant object.

protected Object deserialize(List<byte[]> blocks) {
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream(blocks.size() * blockSize);
        for (byte[] b : blocks) {
            os.write(b, 0, b.length);
        }
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        ObjectInputStream objInputStream = new ObjectInputStream(is);
        return objInputStream.readObject();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

This works perfectly. However, its very memory heavy. Assuming an object in memory is very roughly the same size as its serialized byte array in memory, I end up with something like 3 times the size of my object in memory:

  1. the List<byte[]> containing the blocks
  2. the ByteArrayOutputStream containing concatenated byte array (and possibly another because ByteArrayOutputStream.toByteArray() copies the array).
  3. the resulting Object

Once this method returns all the arrays can be GC’d, but during this method call there’s a big spike in memory usage.

So, to my question: Is there a way that I can create a blocking byte input stream that I can append the byte arrays to as I receive them? ObjectOutputStream would (in a separate thread) read available bytes, then block until more bytes were written, and continue until the object was fully deserialized. In this way, I don’t ever have to have the full concatenated byte array in memory. None of the standard stream implementations seem to fit, I can’t see how I’d use NIO to do this, and I’d rather not write my own stream implementation if there’s one out there that would suffice.

Many thanks,
Ian

  • 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-31T16:20:05+00:00Added an answer on May 31, 2026 at 4:20 pm

    implement your own input stream to lessen the array overhead

    protected Object deserialize(final List<byte[]> blocks) {
        try {
           ObjectInputStream objInputStream = new ObjectInputStream(InputStream(){
                Iterator<byte[]> it=blocks.iterator();
                byte[] curr;
                int ind;
                public int read(){
                    if(curr==null||curr.length==ind){
                        if(!it.hasNext())return -1;//or use a blocking queue and pop
                        curr=it.next();
                        ind=0;
                    }
                    return curr[ind++];
                }
            });
            return objInputStream.readObject();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    

    ofcourse you’ll should also override read(byte[],int,int) as well for efficiency but this will work if a tad slowly

    Or you can use the PipedInputStream and PipedOutputStream combo for what you really want. The input stream will block until it has something to read

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

Sidebar

Related Questions

I have a client-server application written in Java using CORBA for the communication. The
I have setup a basic WCF client/server which are communicating via Named pipes. It
I have a client and a server communicating to one another over a standard
I have an Android client and a C# server. They are communicating over sockets
I have a client and a server communicating with datagrams (UDP) in C. The
I have a Silverlight application on the client communicating with the server side through
I have client-server app. Client on C++, server on Java. I am sending byte-stream
Here's my problem: I have server A, running node.js and using socket.io for communicating
I want a Server Client communication via socket programming in java. But problem arises
I have a pretty simple client-server ASP.NET app; communication is via WCF service. All

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.