diff --git a/app/src/main/scala/com/nutomic/ensichat/bluetooth/TransferThread.scala b/app/src/main/scala/com/nutomic/ensichat/bluetooth/TransferThread.scala index c507449..bc8081b 100644 --- a/app/src/main/scala/com/nutomic/ensichat/bluetooth/TransferThread.scala +++ b/app/src/main/scala/com/nutomic/ensichat/bluetooth/TransferThread.scala @@ -5,6 +5,7 @@ import java.io._ import android.bluetooth.BluetoothSocket import android.util.Log import com.nutomic.ensichat.protocol._ +import com.nutomic.ensichat.protocol.messages.Message.ReadMessageException import com.nutomic.ensichat.protocol.messages.{ConnectionInfo, Message, MessageHeader} /** @@ -54,8 +55,8 @@ class TransferThread(device: Device, socket: BluetoothSocket, Handler: Bluetooth onReceive(msg, device.Id) } } catch { - case e: RuntimeException => - Log.i(Tag, "Received invalid message", e) + case e: ReadMessageException => + Log.i(Tag, "Failed to read message", e) case e: IOException => Log.w(Tag, "Failed to read incoming message", e) close() diff --git a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/Message.scala b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/Message.scala index b8668cb..3d57862 100644 --- a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/Message.scala +++ b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/Message.scala @@ -1,6 +1,7 @@ package com.nutomic.ensichat.protocol.messages import java.io.InputStream +import java.security.spec.InvalidKeySpecException object Message { @@ -16,27 +17,35 @@ object Message { val Charset = "UTF-8" + class ReadMessageException(throwable: Throwable) + extends RuntimeException(throwable) + def read(stream: InputStream): Message = { - val headerBytes = new Array[Byte](MessageHeader.Length) - stream.read(headerBytes, 0, MessageHeader.Length) - val header = MessageHeader.read(headerBytes) + try { + val headerBytes = new Array[Byte](MessageHeader.Length) + stream.read(headerBytes, 0, MessageHeader.Length) + val header = MessageHeader.read(headerBytes) - val contentLength = (header.Length - MessageHeader.Length).toInt - val contentBytes = new Array[Byte](contentLength) - var numRead = 0 - do { - numRead += stream.read(contentBytes, numRead, contentLength - numRead) - } while (numRead < contentLength) + val contentLength = (header.Length - MessageHeader.Length).toInt + val contentBytes = new Array[Byte](contentLength) + var numRead = 0 + do { + numRead += stream.read(contentBytes, numRead, contentLength - numRead) + } while (numRead < contentLength) - val (crypto, remaining) = CryptoData.read(contentBytes) + val (crypto, remaining) = CryptoData.read(contentBytes) - val body = - header.MessageType match { - case ConnectionInfo.Type => ConnectionInfo.read(remaining) - case _ => new EncryptedBody(remaining) - } + val body = + header.MessageType match { + case ConnectionInfo.Type => ConnectionInfo.read(remaining) + case _ => new EncryptedBody(remaining) + } - new Message(header, crypto, body) + new Message(header, crypto, body) + } catch { + case e @ (_ : OutOfMemoryError | _ : InvalidKeySpecException) => + throw new ReadMessageException(e) + } } }