I am having troubles while uploading files via https (HttpsURLConnection). Using the same code with HttpURLConnection works like a charm.
here is the stack:
ERROR/dalvikvm-heap(29984): Out of memory on a 20932172-byte allocation.
INFO/dalvikvm(29984): "UploadingManager" prio=5 tid=9 RUNNABLE
INFO/dalvikvm(29984): | group="main" sCount=0 dsCount=0 obj=0x2b085018 self=0x3d4de8
INFO/dalvikvm(29984): | sysTid=30027 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=3868056
INFO/dalvikvm(29984): at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:~91)
INFO/dalvikvm(29984): at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:216)
INFO/dalvikvm(29984): at org.apache.harmony.luni.internal.net.www.protocol.http.RetryableOutputStream.write(RetryableOutputStream.java:60)
INFO/dalvikvm(29984): at java.io.DataOutputStream.write(DataOutputStream.java:99)
INFO/dalvikvm(29984): at com.my.app.utils.UploadingManager$UploadingThread.run(UploadingManager.java:568)
And here is the code: Initializing connection:
URL url = new URL(url_to_send);
trustAllHosts();
connection = (HttpsURLConnection) url.openConnection();
connection.setHostnameVerifier(DO_NOT_VERIFY);
connection.setChunkedStreamingMode(maxBufferSize);
I am using the same code for establishing "trustall" ssl connection as mentioned in here: ssl on android strange issue (I know that they are facing similar issue but I want to make sure that I am not making any mistake during the upload)
And here is the code for upload:
File uploaded_file = new File(pathToOurFile);
file_input = new FileInputStream(uploaded_file);
output_byte = new DataOutputStream(connection.getOutputStream());
int maxBufferSize = 20 * 1024;
bytesAvailable = file_input.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = file_input.read(buffer, 0, bufferSize);
already_send += bytesRead;
while (bytesRead > 0) {
output_byte.write(buffer, 0, bufferSize); < ----Exception occurs here
bytesAvailable = file_input.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = file_input.read(buffer, 0, bufferSize);
already_send += bytesRead;
}
output_byte.writeBytes(endBoundary);
output_byte.flush();
Have you got any idea why am I getting OutOfMemoryException?
Regards, Peter
EDIT1: I have noticed that if I omit:
connection.setChunkedStreamingMode(maxBufferSize);
using the working httpUrlConnection I am falling into same issue. I know that sending same message using ssl will generate longer content-length so I have changed the chunk size to 2 * maxBufferSize or even 20 * maxBufferSize but this makes no difference to me :/
How big is the file you are uploading? It seems that you are trying to write around ~20MB to output_byte buffer.
Also you are writing wrong number of bytes to your output.
read(buffer, 0, bufferSize); line doesn't guarantee bufferSize amount of bytes will be read. The amount read is returned from the call above so:
output_byte.write(buffer, 0, bufferSize);
should be instead:
output_byte.write(buffer, 0, bytesRead);
So for example if you read 1024bytes and try to write 20*1024 bytes each time, for a 1MB file you will have have 20 times the output bytes. This could cause OutOfMemory error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With