I am developing a HTTP-based client-server application in java. I have a client program and a serer program. Basically, the client sends a file via Http, but I have a problem at the server side when reading the data. If I send a file larger than 8KB, I only get the first 8KB of characters at the servers side. I've searched on stackoverflow for similar problems and it turned out that the Entity is buffered, so I have to use BufferedHttpEntity. Here is my code:
BufferedHttpEntity buffEntity = new BufferedHttpEntity(entity);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
buffEntity.writeTo(baos);
while (buffEntity.isStreaming()) {
buffEntity.writeTo(baos);
}
Log.i("Data received", baos.toString());
The problem is that I am sending a 16KB file, and the last letters shown in the console are at the half at the document. I really don't know how to read the rest of the buffered entity. However, if I write the content of the entity to a file (like shown below) it works just fine, so it is very strange:
File f = new File("mnt/sdcard/file.txt");
FileOutputStream os = new FileOutputStream(f);
buffEntity.writeTo(os);
while (buffEntity.isStreaming()) {
buffEntity.writeTo(os);
}
After this step, if I look at "mnt/sdcard/file.txt", the file is complete (no characters missing). I do not know what I am doing wrong with the ByteArrayOutputStream, since it does not get all the content. Any help would be greatfully appreciated!