i am currently working at a program that transfer files via FTP. I send the files in binary because with ASCII I can´t send special characters.
Here is my currently code :
using(BinaryReader bReader = new BinaryReader(srcStream))
using (BinaryWriter bWriter = new BinaryWriter(destStream))
{
Byte[] readBytes = new Byte[1024];
for(int i = 0; i < bReader.BaseStream.Length; i += 1024)
{
readBytes = bReader.ReadBytes(1024);
bWriter.Write(readBytes);
}
}
My Problems with this code are :
Thanks alot :D
Why are you using BinaryReader and BinaryWriter at all? Why are you repeatedly asking for the length? Here's a method I've posted a bunch of times now:
public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[8192];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}
That uses an 8K buffer, but you can change that obviously. Oh, and it reuses the buffer rather than creating a new byte array every time, which is what your code will do :) (You don't need to allocate the byte array to start with - you could have declared readBytes at the point of the call to bReader.ReadBytes.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With