I need to transfer a byte[] via TCP Socket in android -client side.
i tried to use the following code :
public static int readBytes(byte[] myByteArray) throws IOException {
InputStream in = socket.getInputStream();
DataInputStream dis = new DataInputStream(in);
int len = dis.readInt(); //<-here i get the error
byte[] data = new byte[len];
if (len > 0) {
dis.readFully(data,0,len);
}
myByteArray=data;
return data.length;
}
but I get the following error:
12-23 17:30:49.814: E/AndroidRuntime(11717):
java.lang.OutOfMemoryError: array size too large(Heap Size=5699KB,
Allocated=3403KB, Bitmap Size=78KB)
You probably get the
OutOfMemoryErroron the line below. (The line that allocates new memory usingnew.)Print
lenbefore doing the allocation and you’ll probably see that it’s a huge number (a lot larger than what you expect).Presumably you don’t send the size of the byte array on the sending side. (Or if you do, your reading / writing has come out of sync.)