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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:59:42+00:00 2026-05-13T19:59:42+00:00

ObjectInputStream blocks when created until it recieves a serial input stream ans verifies it.

  • 0

ObjectInputStream blocks when created until it recieves a serial input stream ans verifies it. I was trying to make my first program using sockets through it and found this. I used a dummy object so that it doesn’t block. The code is here:

import java.io.*;                      
import java.net.*;                     
import java.util.*;                    

class Dummy implements Serializable {
}

class X_Int implements Serializable {
    int x;
}

class Server {
        public static void main(String args[]) throws Exception {
                ServerSocket ss = new ServerSocket(5879);
                Socket client = ss.accept();
                ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
                out.writeObject(new Dummy());
                ObjectInputStream in = new ObjectInputStream(client.getInputStream());
                in.readObject();
                out.flush();
                out.writeObject(new Date());
                out.flush();
                out.close();
        }
}

class Client {
    public static void main(String args[]) throws Exception {
        Socket server = new Socket("localhost", 5879);
        ObjectOutputStream out = new ObjectOutputStream(server.getOutputStream());
        out.writeObject(new Dummy());
        ObjectInputStream in  = new ObjectInputStream(server.getInputStream());
        in.readObject();
        out.flush();
        Date d = (Date)in.readObject();
        System.out.println(d);
    }
}

Is this the right way. Please comment.

  • 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-13T19:59:42+00:00Added an answer on May 13, 2026 at 7:59 pm

    A better way is to get rid of the cause of blocking in the first place. Use these classes instead on both ends, if you can:

    public class ObjInputStream extends ObjectInputStream {
        /**
         * @param in
         * @throws IOException
         */
        public ObjInputStream(InputStream in) throws IOException {
            super(in);
        }
    
        /* (non-Javadoc)
         * @see java.io.ObjectInputStream#readStreamHeader()
         */
        @Override
        protected void readStreamHeader() throws IOException, StreamCorruptedException {
        }
    }
    

    and

    public class ObjOutputStream extends ObjectOutputStream {
    
        /**
         * @param out
         * @throws IOException
         */
        public ObjOutputStream(OutputStream out) throws IOException {
            super(out);
        }
    
        /* (non-Javadoc)
         * @see java.io.ObjectOutputStream#writeStreamHeader()
         */
        @Override
        protected void writeStreamHeader() throws IOException {
        }
    }
    

    This removes the functions which are called to ascertain stream version info and such.

    Additionally, as you are using TCP packets, IP fragmentation will cause your objects not be received ‘whole’ on the other end — TCP is a stream socket. What you need is an additional framing input / output class. Luckily, I already coded this 🙂

    /**
     * 
     */
    package objtest;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.nio.BufferUnderflowException;
    import java.nio.ByteBuffer;
    import java.util.ArrayDeque;
    import java.util.Queue;
    
    import kokuks.KokuKS;
    
    /**
     * UnrealConceptTest - FramedInputStream
     * @version 1.0
     */
    public class FramedInputStream extends InputStream {
        public static final int INITIAL_BUFFER_SIZE = 8 << 1;
    
        public static final int FRAME_HEADER_1 = 0xBEEFFACE;
        public static final int FRAME_HEADER_2 = 0xFACEBEEF;
    
        public static final byte[] HEADER_BYTES = new byte[4 * 2];
        protected static final byte[] CURR_HEADER_BUFF = new byte[HEADER_BYTES.length];
    
        static {
            ByteBuffer b = ByteBuffer.allocateDirect(8);
    
            b.putInt(FRAME_HEADER_1);
            b.putInt(FRAME_HEADER_2);
    
            ByteBuffer b2 = (ByteBuffer) b.flip();
    
            b2.get(HEADER_BYTES, 0, 4);
            b2.get(HEADER_BYTES, 3, 4);
        }   
    
        protected int     size         = 0;
        protected int     chain        = 0;
        protected boolean inFrame      = false;
        protected boolean readingSize  = false;
        protected int     sizePos      = 0;
    
        protected int dbgput = 0;
    
    
        protected ByteBuffer        bb  = ByteBuffer.allocateDirect(INITIAL_BUFFER_SIZE);
        protected Queue<ByteBuffer> bbq = new ArrayDeque<ByteBuffer>();
        protected ByteBuffer        currBuff = null;
    
        protected final boolean     recoverFromError;
    
        /**
         * 
         */
        public FramedInputStream(boolean recoverFromError) {
            this.recoverFromError = recoverFromError;
        }
    
        public FramedInputStream() {
            this(true);
        }
    
        protected boolean ensureFramebufferCapacity(int min) {
            int mymin = 1 << min;
    
            if (mymin <= bb.capacity()) return false;
    
            int num = bb.capacity();
            while (num < mymin) num <<= 1;
    
            ByteBuffer bb2 = ByteBuffer.allocateDirect(num);
            // copy old data into new bytebuffer
            int bb_pos = bb.position();
            bb.rewind();
            bb2.put(bb);
            bb = bb2;
    
            if (KokuKS.DEBUG_MODE) System.out.println("modified buffer size to: " + num);
    
            return true;
        }
    
        /**
         * @return the recoverFromError
         */
        public boolean isRecoverFromError() {
            return recoverFromError;
        }
    
        /* (non-Javadoc)
         * @see java.io.InputStream#read()
         */
        @Override
        public int read() throws IOException {
            if (currBuff == null || !currBuff.hasRemaining()) return -1;
    
            byte b = currBuff.get();
            //System.out.println("data: " + b);
            return b;
        }
    
        public void putBuffer(ByteBuffer source) {
            ensureFramebufferCapacity(bb.capacity() + source.remaining());
    
            while (source.hasRemaining()) {
                putByte(source.get());
            }
        }
    
        public boolean checkCompleteFrame() {
            return !bbq.isEmpty();
        }
    
        /* (non-Javadoc)
         * @see java.io.InputStream#available()
         */
        @Override
        public int available() throws IOException {
            return currBuff != null ? currBuff.remaining() : 0;
        }
    
        public int read(byte[] data) {
            if (currBuff == null || !currBuff.hasRemaining()) {
                return -1;
            }
    
            if (data.length > currBuff.remaining()) {
                throw new BufferUnderflowException();
            }
    
            currBuff.get(data);
    
            //System.out.println("data: " + new String(data));
    
            return data.length;
        }
    
        public boolean nextFrame() {
            ByteBuffer bbf = bbq.poll();
    
            if (bbf != null) {
                /*
                System.out.println("bbf limit: " + bbf.limit());
                System.out.println("bbf pos: " + bbf.position());
                System.out.println("bbf data: " + new String(bbf.array()));
                */
    
                //byte[] data = bbf.array();
    
                //for (int i = 0; i < data.length; i++) {
                //  byte by = data[i];
                //  System.out.println("b: " + (by > 32 ? new String(new byte[] {by}) : "??") + ", " + by);
                //}         
    
                currBuff = ByteBuffer.allocateDirect(bbf.limit());
                currBuff.put(bbf).flip();
                bbf.rewind();
    
                /*
                System.out.println("currbuf limit: " + currBuff.limit());
                System.out.println("currbuf pos: " + currBuff.position());
                System.out.println("currbuf data: " + new String(currBuff.array()));
                */
    
                currBuff.rewind();
                currBuff.position(1);
    
                return true;
            }
    
            return false;
        }
    
    
        public void putByte(byte b) {
            //System.out.println("pb b: " + ObjTest.getByteStr(b));
    
            if (recoverFromError || !inFrame) {
                if (b == HEADER_BYTES[chain++]) {
    
                    if (chain >= (HEADER_BYTES.length)) {
                        if (KokuKS.DEBUG_MODE) System.out.println("got header!" + (inFrame ? " (recovered)" : ""));
    
                        // we have a header! hurrah.
                        inFrame = true;
                        sizePos = 0;
                        size = 0;
                        readingSize = true;
                        chain = 0;
    
                        bb.clear();
                    }
                } else {
                    chain = 0;
                }
            }
    
            if (inFrame) {
                if (readingSize) {
                    size += (b & 0xFF) << ((8 * 3) - (8 * sizePos));
                    //System.out.println("new size: " + size);
                    sizePos++;
    
                    if (sizePos >= 4) {
                        // we've read the size :)
                        readingSize = false;
                        sizePos = 0;
    
                        ensureFramebufferCapacity(size);
                        bb.clear();
                        bb.limit(size); // set buffer limit to size
                        //System.out.println("bb limit set to: " + bb.limit());
                    }
                } else {
                    //System.out.println("put: " + dbgput++ + ", " + ObjTest.getByteStr(b));
                    bb.put(b);
    
                    if (!bb.hasRemaining()) {
                        bb.flip();
    
                        //System.out.println("bb limit after flip(): " + bb.limit());
    
                        //System.out.println("bblimit: " + bb.limit());
    
                        ByteBuffer newbuf = ByteBuffer.allocateDirect(bb.limit());
                        newbuf.put(bb).flip(); //we have to flip this
                        bbq.offer(newbuf); 
    
                        //byte[] data = newbuf.array();
    
                        //for (int i = 0; i < newbuf.limit(); i++) {
                        //  byte by = data[i];
                        //  System.out.println("b: " + (by > 32 ? new String(new byte[] {by}) : "??") + ", " + by);
                        //}
    
                        inFrame = false;
                        readingSize = false;
                        size = 0;
                        sizePos = 0;
                        chain = 0;
    
                        bb.clear();
    
                        if (KokuKS.DEBUG_MODE) System.out.println("FIS: complete object");
                        //System.out.println("FIS: newbuf: " + new String(newbuf.array(), 0, newbuf.limit()));
                    }
                }
            }
        }
    }
    

    and

    /**
     * 
     */
    package objtest;
    
    import java.io.IOException;
    import java.nio.ByteBuffer;
    
    import koku.util.io.ByteBufferOutputStream;
    
    /**
     * UnrealConceptTest - FramedOutputStream
     * @version 1.0
     * @author Chris Dennett
     */
    public class FramedOutputStream extends ByteBufferOutputStream {
        public static final int FRAME_HEADER_1 = 0xBEEFFACE;
        public static final int FRAME_HEADER_2 = 0xFACEBEEF;
    
        public static final byte[] HEADER_BYTES = new byte[4 * 2];
        public static final byte[] CURR_HEADER_BUFF = new byte[HEADER_BYTES.length];
    
        /* We pad the beginning of our buffer so that we can write the frame
         * length when the time comes. */
        protected static final byte[] SIZE_PAD = new byte[4];
    
        static {
            ByteBuffer b = ByteBuffer.allocate(8);
    
            b.putInt(FRAME_HEADER_1);
            b.putInt(FRAME_HEADER_2);
    
            ByteBuffer b2 = (ByteBuffer) b.flip();
    
            b2.get(HEADER_BYTES, 0, 4);
            b2.get(HEADER_BYTES, 3, 4);
        }
    
        /**
         * 
         */
        public FramedOutputStream() {
            try {
                write(HEADER_BYTES);
                write(SIZE_PAD);
            } catch (IOException e) {
                System.out.println("Couldn't write header padding!");
            }
        }
    
        /* (non-Javadoc)
         * @see koku.util.io.ByteBufferOutputStream#flip()
         */
        @Override
        public ByteBuffer flip() {
            // flip the buffer which will limit it to it's current position
            super.flip();
    
            // then write the frame length and rewind back to the start of the
            // buffer so that all the data is available        
            _buffer.position(11);
            int size = _buffer.remaining();
    
            //System.out.println("remaining after complete header: " + size);
    
            _buffer.position(7);
    
            //System.out.println("remaining after frameheader: " + _buffer.remaining());
    
            putSizeAsBytes(size, _buffer);
    
            //System.out.println("written size: " + size);
    
           // System.out.println("buffer limit: " + _buffer.limit());
    
            //System.out.println("_buffer: " + new String( _buffer.array(), 0, _buffer.limit()));
    
            _buffer.position(11);
    
           // System.out.println("_buffer11: " + ObjTest.getByteStr(_buffer.get()));
            //System.out.println("_buffer12: " + ObjTest.getByteStr(_buffer.get()));
            //System.out.println("_buffer13: " + ObjTest.getByteStr(_buffer.get()));
            //System.out.println("_buffer14: " + ObjTest.getByteStr(_buffer.get()));
    
            _buffer.rewind();
    
            //_buffer.rewind();
    
            //while (_buffer.hasRemaining()) {
            //  byte b = _buffer.get();
            //  System.out.println("b: " + (b > 32 ? new String(new byte[] {b}) : "??") + ", " + b);
            //}
    
            _buffer.rewind();
    
            return _buffer;
        }
    
        /* (non-Javadoc)
         * @see koku.util.io.ByteBufferOutputStream#reset()
         */
        @Override
        public void reset() {
            super.reset();
    
            try {
                write(HEADER_BYTES);
                write(SIZE_PAD);
            } catch (IOException e) {
                System.out.println("Couldn't write header padding!");
            }
        }
    
        public static void putSizeAsBytes(int size, ByteBuffer bb) {
            //System.out.println("putSizeAsBytes: given size: " + size);
    
            // encode
            for (int i = 0; i < 4; i++) {
                bb.put((byte)((size >>> ((8 * 3) - (8 * i))) & 0xFF));
            }
        }
    }
    

    BBOS:

    //
    // $Id: ByteBufferOutputStream.java 5829 2009-06-20 21:09:34Z mdb $
    //
    // Narya library - tools for developing networked games
    // Copyright (C) 2002-2009 Three Rings Design, Inc., All Rights Reserved
    // http://www.threerings.net/code/narya/
    //
    // This library is free software; you can redistribute it and/or modify it
    // under the terms of the GNU Lesser General Public License as published
    // by the Free Software Foundation; either version 2.1 of the License, or
    // (at your option) any later version.
    //
    // This library is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    // Lesser General Public License for more details.
    //
    // You should have received a copy of the GNU Lesser General Public
    // License along with this library; if not, write to the Free Software
    // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    
    package misc;
    
    import java.io.OutputStream;
    import java.nio.BufferOverflowException;
    import java.nio.ByteBuffer;
    
    import org.apache.mina.core.buffer.IoBuffer;
    
    /**
     * Stores output in an {@link ByteBuffer} that grows automatically to accommodate the data.
     */
    public class ByteBufferOutputStream extends OutputStream
    {
        /**
         * Creates a new byte buffer output stream.
         */
        public ByteBufferOutputStream ()
        {
            _buffer = IoBuffer.allocate(INITIAL_BUFFER_SIZE);
        }
    
        /**
         * Returns a reference to the underlying buffer.
         */
        public IoBuffer getBuffer ()
        {
            return _buffer;
        }
    
        /**
         * Flips and returns the buffer.  The returned buffer will have a position of zero and a limit
         * equal to the number of bytes written.  Call {@link #reset} to reset the buffer before
         * writing again.
         */
        public IoBuffer flip ()
        {
            return _buffer.flip();
        }
    
        /**
         * Resets our internal buffer.
         */
        public void reset ()
        {
            _buffer.clear();
        }
    
        @Override // documentation inherited
        public void write (int b)
        {
            try {
                _buffer.put((byte)b);
            } catch (BufferOverflowException boe) {
                expand(1);
                _buffer.put((byte)b);
            }
        }
    
        @Override // documentation inherited
        public void write (byte[] b, int off, int len)
        {
            // sanity check the arguments
            if ((off < 0) || (off > b.length) || (len < 0) ||
                ((off + len) > b.length) || ((off + len) < 0)) {
                throw new IndexOutOfBoundsException();
            } else if (len == 0) {
                return;
            }
    
            try {
                _buffer.put(b, off, len);
            } catch (BufferOverflowException boe) {
                expand(len);
                _buffer.put(b, off, len);
            }
        }
    
        /**
         * Expands our buffer to accomodate the specified capacity.
         */
        protected final void expand (int needed)
        {
            _buffer.expand(needed);
        }
    
        /** The buffer in which we store our frame data. */
        protected IoBuffer _buffer;
    
        /** The default initial size of the internal buffer. */
        protected static final int INITIAL_BUFFER_SIZE = 32;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In serialization mechanism,we are wrote the object into stream using objectinputstream and object outputstream.These
Using Input/Output stream to pass objects between my client and server. I can both
I am trying to make a program that saves a list of strings to
I'm trying to create a new ObjectInputStream using an InputStream retrieved from a Socket.
I receive an object from the net through the the getInputStream() using ObjectInputStream My
InetAddress host = InetAddress.getLocalHost(); Socket link = new Socket(host, Integer.parseInt(args[0])); System.out.println(before input stream); ObjectInputStream
I am currently not receiving the last object from my object stream until another
I'm writing a server and I'm using an ObjectInputStream to handle proprietary packets. I've
I'm using an ObjectInputStream to call readObject for reading in serialized Objects . I
I am using an ObjectInputStream and ObjectOutputStream for my client to talk to 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.