Reputation: 1093
I am trying to transfer an XML file from a desktop server to an Android client but I get on the Android device just 1024 bytes of the entire file. My code is:
byte[] mybytearray = new byte[(int) filePianificazione.length()];
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(filePianificazione));
bufferedInputStream.read(mybytearray, 0, mybytearray.length);
bufferedInputStream.close();
out.write(mybytearray, 0, mybytearray.length);
out.flush();
byte[] mybytearray = new byte[1024];
FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = in.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
bos.close();
Upvotes: 0
Views: 181
Reputation: 311039
The canonical way to copy streams in Java is as follows:
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
Upvotes: 0
Reputation: 2887
First you declare byte[] mybytearray = new byte[1024];
Then you're doing a single
int bytesRead = in.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
In your read code (Android client side), you're only reading 1024 bytes because that's the length of your input buffer, and you're only reading into it once. You need to have a while
loop that'll continue to read from your input stream and then write that out until you reach EOF.
Something like:
while(in.available() > 0)
{
int bytesRead = in.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
}
Upvotes: 1