java - Sending very large string trough socket from server to android client -
i have problem sending large string through socket server android client.
string 10mb.
code writing data socket this:
int sockettimeout = 200; socket = new socket(client.gethost(), client.getport()); socket.setsotimeout(sockettimeout); outputstreamwriter oos=new outputstreamwriter(socket.getoutputstream()); string d = data.getdata().tostring() + "\n"; oos.write(d); oos.flush();
code reading data socket this:
socket s = params[0]; inputstream = null; try { = s.getinputstream(); bytearrayoutputstream baos = new bytearrayoutputstream(); int nread; byte[] data = new byte[32768]; while ((nread = is.read(data, 0, data.length)) != -1) { baos.write(data, 0, nread); } return new string(baos.tobytearray()); }
so problem comes @ line i'm reading inputstream
outofmemoryexception. tried using different examples of reading string stream. tried bufferedinputstream
, inputstreamreader
, ioutils
, stringbuilder
, bufferedreader
..etc. , of them give me outofmemory exception when string large. tested smaller data around 100k , works perfectly.
exception on server-side "connection reset peer, socket write error."
you can read byte byte in client , write file byte byte, in way not holding whole string in memory.
and of course read file tokens or lines, not whole string @ once
Comments
Post a Comment