简体   繁体   中英

Sending very large string trough socket from server to android client

I have a problem with sending large string through socket from server to android client.

String is about 10MB.

Code for writing data to socket is 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 for reading data from socket is this:

Socket s = params[0];
InputStream is = null;
try {
    is = 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 at line where I'm reading from inputStream where I get OutOfMemoryException. I tried using different examples of reading string from stream. I tried with BufferedInputStream , InputStreamReader , IOUtils , StringBuilder , BufferedReader ..etc. and all of them give me OutOfMemory exception when the string is large. I tested with smaller data something around 100K and it works perfectly.

Exception that I get on server-side is "Connection is reset by peer, socket write error."

You can read byte by byte in the client and write to a File byte by byte, in that way you are not holding the whole string in memory.

And then of course read that file by tokens or lines, not the whole string at once

By storing it in a ByteArrayOutputStream (or similar), you are coming up against the maximum heap size for the JVM in Android. This is a different size depending on the device. See: Android heap size on different phones/devices and OS versions

As has already been suggested, you should consider using a file stream to write the received data to disk.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM