Friday, 5 February 2021

Why this Exception occurs 'o.StreamCorruptedException: invalid stream header: D8322EDA'

I'm working with Bluetooth in android.My main goal is to transmit large video file via Bluetooth. I'm following Bluetooth chat sample for connectivity. Actually i convert video into byte array and then in onRecieve method convert back to File. when converting back to file some exception occurs. I increased the buffer size in connected Thread.

To convert recieved byte[] into Hashmap<String, Byte[]>

private Object convertFromBytes(byte[] bytes) {
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        Object out = null;
        ObjectInput in = null;
        try {
            in = new ObjectInputStream(bis);
            out = in.readObject();
            Log.d("bytetodata out", "");
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            Log.d("bytetodata out", e.getLocalizedMessage());
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                Log.e(TAG, ex.getLocalizedMessage());
                // ignore close exception
            }
        }
        Log.d("bytetodata out", "");
        return out;
    }

Exception occurs at in = new ObjectInputStream(bis);

Bluetooth Reciever code

case MESSAGE_READ:

                    byte[] readBuf = (byte[]) msg.obj;
                    // construct a string from the valid bytes in the buffer
                    onRecieve(readBuf);
private void onRecieve(byte[] readBuf) {

        HashMap<String, byte[]> receivedMap = (HashMap<String, byte[]>) convertFromBytes(readBuf);
        //Log.d("recieveMAP Size" , String.valueOf(receivedMap.size()));
                if (receivedMap != null && receivedMap.size() == 2) {
                    byte[] filebyte = receivedMap.get("file");
                    byte[] objectbyte = receivedMap.get("object");
                    if (filebyte != null) {
                        File file = new File("storage/emulated/0/Download/name.mp4");
                        try {
                            file.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                            Log.e("Mainactivity", e.getLocalizedMessage());
                        }
                        if (file.exists()) {
                            try {
                                FileUtils.writeByteArrayToFile(file, filebyte);
                            } catch (IOException e) {
                                e.printStackTrace();
                                Log.e(TAG, e.getLocalizedMessage());
                            }
                        } else {
                            //System.out.println("file created: " + file);
                            Toast.makeText(context, "File is Empty", Toast.LENGTH_LONG).show();
                        }
                    } else {
                        Log.e(TAG, "selected video path = null!");
                    }

                    //System.out.println("file created: " + file);
                    //Toast.makeText(context, "File Created", Toast.LENGTH_LONG).show();

                   // Video video = (Video) convertFromBytes(objectbyte);
                }
    }

Bluetooth Connectivity code

 private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

        public ConnectedThread(BluetoothSocket socket) {
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;
            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
            }
            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run() {
            byte[] buffer = new byte[ 4096 * 16 ];
            int bytes;
            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);
                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(BTListFragment.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();
                } catch (IOException e) {
                    connectionLost();
                    break;
                }
            }
        }

        /**
         * Write to the connected OutStream.
         *
         * @param buffer The bytes to write
         */
        public void write(byte[] buffer) {
            try {
                mmOutStream.write(buffer);
                // Share the sent message back to the UI Activity
                mHandler.obtainMessage(BTListFragment.MESSAGE_WRITE, -1, -1, buffer)
                        .sendToTarget();
            } catch (IOException e) {
            }
        }

        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
            }
        }
    }


from Why this Exception occurs 'o.StreamCorruptedException: invalid stream header: D8322EDA'

No comments:

Post a Comment