Provided you can obtain an InputStream
from the source socket and an OutputStream
from the destination socket, you can use a method which will avoid copying into a file, such as:
public static void copyStream(final InputStream in, final OutputStream out)
throws IOException
{
final byte[] buf = new byte[32768]; // or more, or less; size appropriately
int nrBytes;
while ((nrBytes = in.read(buf)) != -1)
out.write(buf, 0, nrBytes);
out.flush();
}
However, before going this way and avoid a temporary file inbetween, consider what would happen if you had a short read or short write; how do the endpoints behave in this case?
Also, note that this method does not close either stream; you'll have to do it yourself.