U
uzon
hi,
i'm writing a proxy for file transfers.
for example, one client sends a file to the proxy which forwards it to
another client.
i'm writing code for both the clients (send / receive file) and the
proxy (forward the file).
currently, i have this function to send the file-
InputStream fis = new FileInputStream(f.getPath() + f.separator +
param.filename);
BufferedOutputStream bos = new BufferedOutputStream(os);
do {
i = fis.read();
if (i != -1) {
bos.write(i);
}
} while (i != -1);
the forwarding code is very similar. just the input stream is a socket
not a file.
the receiving code is also similar. the output stream is to a file not
a socket.
this is working ok but it's very slow. it takes several minutes for a
file that's only a few mb.
there are two problems here i don't know how to code better-
1. loading and writing the file byte by byte is REALLY slow. how would
i go about doing it with some kind of buffer saving every X amount
received and what is the recommended amount? i want to support large
file transfers also, so saving the whole file in memory and writing to
disk in the end is not good.
2. sending byte by byte on the network (especially for the forwarder-
proxy, is also very slow). so here i also need some kind of buffer to
accept X amount and then send it.
i know there are other sending/receiving functions in java but don't
know which to use, or exactly how to use them.
any help would be greatly appreciated.
thanks in advance,
-Aaron
i'm writing a proxy for file transfers.
for example, one client sends a file to the proxy which forwards it to
another client.
i'm writing code for both the clients (send / receive file) and the
proxy (forward the file).
currently, i have this function to send the file-
InputStream fis = new FileInputStream(f.getPath() + f.separator +
param.filename);
BufferedOutputStream bos = new BufferedOutputStream(os);
do {
i = fis.read();
if (i != -1) {
bos.write(i);
}
} while (i != -1);
the forwarding code is very similar. just the input stream is a socket
not a file.
the receiving code is also similar. the output stream is to a file not
a socket.
this is working ok but it's very slow. it takes several minutes for a
file that's only a few mb.
there are two problems here i don't know how to code better-
1. loading and writing the file byte by byte is REALLY slow. how would
i go about doing it with some kind of buffer saving every X amount
received and what is the recommended amount? i want to support large
file transfers also, so saving the whole file in memory and writing to
disk in the end is not good.
2. sending byte by byte on the network (especially for the forwarder-
proxy, is also very slow). so here i also need some kind of buffer to
accept X amount and then send it.
i know there are other sending/receiving functions in java but don't
know which to use, or exactly how to use them.
any help would be greatly appreciated.
thanks in advance,
-Aaron