OckhamsRazor
OckhamsRazor

Reputation: 4906

Java: Distinguishing inputs in InputStream

I have an InputStream which runs on a thread and reads any data which is passed over the network. My question is- how do I distinguish between the bytes received by the InputStream object? e.g. if the bytes received point to a Car object, do something, if bytes received point to a Person object, do something else.

Thanks.

EDIT: here's a snippet of my code. does it seem fine? sorry, im new to network programming.

private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final ObjectOutputStream mmObjOutStream;

        public ConnectedThread(BluetoothSocket socket) {
            Log.d(TAG, "create ConnectedThread");
            mmSocket = socket;
            InputStream tmpIn = null;
            ObjectOutputStream tmpOut = null;

            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = socket.getInputStream();
                tmpOut = new ObjectOutputStream(socket.getOutputStream());
            } catch (IOException e) {
                Log.e(TAG, "temp sockets not created", e);
            }

            mmInStream = tmpIn;
            mmObjOutStream = tmpOut;
        }

        public void run() {
            Log.i(TAG, "BEGIN mConnectedThread");
            byte[] buffer = new byte[1024];
            int bytes;

            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);
                    Log.i(TAG, "PERFORMING MESSAGE READ");
                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(GameboardResourceActivity.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();
                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    break;
                }
            }
        }

        /**
         * Write to the connected OutStream.
         * @param buffer  The bytes to write
         */
        public void write(CardResource buffer) {
            try {
                mmObjOutStream.writeObject(buffer);
                System.out.println("Reached here at least........");
                // Share the sent message back to the UI Activity
                mHandler.obtainMessage(GameboardResourceActivity.MESSAGE_WRITE, -1, -1, buffer)
                        .sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "Exception during write", e);
            }
        }

        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of connect socket failed", e);
            }
        }
    }

Upvotes: 1

Views: 126

Answers (2)

icirellik
icirellik

Reputation: 766

You can pass yor sockets input stream directly into the constructor for ObjectInputStream:

ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
    
while ((obj = inputStream.readObject()) != null) {
    if (obj instanceof Person) {   
        System.out.println(((Person)obj).toString());
    }
}

EDIT

As the comments state this will prematurely exit if you pass a null value into your ObjectOutputStream on the other end. Its best to guard against this, null values are never a good thing to receive unexpectedly.

Upvotes: 1

user207421
user207421

Reputation: 310980

Well you do have to know what your application protocol is to understand it. It sounds like the other end is using Serialization, which you need to read up. See the Javadoc for ObjectOutputStream and ObjectInputStream. What you need is ObjectInputStream.readObject(), if this supposition is correct. If it isn't, you will just have to find out what they are sending you and proceed accordingly, probably with a DataInputStream to handle the various datatypes.

Upvotes: 1

Related Questions