stream copy function from Claudiu