0

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!

user2435860
  • 738
  • 3
  • 9
  • 19

2 Answers2

1

try to read the BufferedHttpEntity using an InputStream like the following :

   HttpEntity entity = httpResponse.getEntity();

BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);

InputStraem is = bufHttpEntity .getContent() ; 

ByteArrayOutputStream bOutput = new ByteArrayOutputStream(is.available() );


int nRead;
byte[] data = new byte[is.available()];

while ((nRead = is.read(data, 0, data.length)) != -1) {
  buffer.write(data, 0, nRead);
}

buffer.flush();

and please give me some feedback .

Hope that Helps .

  • I've already tried this (I found the same tutorial), but I have the same problem. I only get the first piece of the content. What is really strange is that it works fine for FileOutputStream – user2435860 Feb 24 '14 at 09:27
  • the inputStream and the answer i post is about reading the BufferedHttpEntity not about writing as you say with FileOutputStream , and your question about how to read data from BufferedHttpEntity –  Feb 24 '14 at 09:29
  • I said I've already tried this and it does not work. I still have the same problem. – user2435860 Feb 24 '14 at 10:18
  • i updated my answer , try to take the HttpEntity from the response , then take the HttpBufferedEntity –  Feb 24 '14 at 10:23
  • This is exactly what I did, and it works, but the problem is that I need to keep it in a ByteArrayOutputStream, not a FileOutputStream. The FileOutputStream works, but it is useless for me. I really need to keep the data received into an ArrayByteOutputStream, because it will be further processed. If I put it into a file, then I have to read it,and it costs a lot of operations(I am on android, so it consumes a lot). – user2435860 Feb 24 '14 at 11:08
  • I tried to implement it and it still does not work. But I've tested it on a java program on my PC and it works. So it is not a bug, it is a limitation set by Android. I've +1 for you for your effort. Thanks – user2435860 Feb 24 '14 at 12:32
0

I finally managed to find the issue. I tried doing the same thing on a java program on PC, and with ByteArrayOutputStream it works just fine, I receive the whole content. But on Android, I only get a piece of the document. I think it's some sort of limitation of Android, because with ByteArrayOutputStream it holds the content in memory, so if I receive a content of 1GB it is not very convenient, so it makes sense. In the case of FileOutputStream there is no problem because the content is not hold in memory, it is directly written in the file.

Does anybody have a clue how to split the content from BufferedHttpEntity into chunks?

user2435860
  • 738
  • 3
  • 9
  • 19