|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
FileStream is adding extra charactersI am using a TCPStream together with a FileStream to send a file across a network. Everything works fine except for one thing. Always, at the end of the file there are several lines of white space. This is no problem for text files but for binaries it is making them corrupt. I only realised this when using checksums to verify that the copied file is the same. However, I cannot seem to work out where this additional data is coming from. Any ideas? See below my code. At a guess I am assuming that the when EOF is reached additional info is written to the file causing the checksum to be different. Dim NWStream As NetworkStream = FTPClient.GetStream Try Dim BytesToSend(FTPClient.SendBufferSize) As Byte Dim FileStream As New FileStream(strFile.FullName, FileMode.Open, FileAccess.Read) Dim FileReader As New BinaryReader(FileStream) Dim NumBytesRead As Integer Dim Limit As Integer Dim Bytes As Integer ProgressBar.Minimum = 0 ProgressBar.Maximum = 100 Do Until Limit = strFile.Length NumBytesRead = FileStream.Read(BytesToSend, 0, BytesToSend.Length) NWStream.Write(BytesToSend, 0, NumBytesRead) Limit += NumBytesRead Bytes += NumBytesRead ProgressBar.Value = (Bytes / strFile.Length * 100) ProgressBar.Update() ProgressPercent.Text = ProgressBar.Value & "%" ProgressPercent.Update() NWStream.Flush() Loop FileStream.Close() FileReader.Close() NWStream.Close() FTPClient.Close() Threading.Thread.Sleep(1000) ProgressBar.Value = 0 ProgressPercent.Text = ProgressBar.Value & "%" Catch ex As Exception MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK) End Try Thanks! OK, this is a buffer issue it seems but I still cannot solve it.
Please help someone! I think it is a buffer issue because of the following: Dim numBytesRead As Integer Dim BUFFER_SIZE As Integer = FTPClient.ReceiveBufferSize <--- this value comes out at 8192 numBytesRead = NWstream.Read(bytesToRead, 0, BUFFER_SIZE) FileSTR.Write(bytesToRead, 0, BUFFER_SIZE) The file size is 211 bytes that I am testing with. So, the above code writes out the 211 bytes and then files the file until it contains 8192 bytes. If I debug the code, after the 211 bytes have passed each subsequent byte has a value of 0 (zero). First of all, any ideas why it is doing this and not stopping when the filestream has read it's 211 bytes like it is supposed to? And second, any ideas on how to fix it? I tried setting the buffer size manually to 211 bytes but then the file length is 8 bytes in size. This is driving me crazy! TIA FileSTR.Write(bytesToRead, 0, numBytesRead)
Show quoteHide quote "Hugh Janus" <my-junk-acco***@hotmail.com> wrote in message news:1141831278.760148.283520@p10g2000cwp.googlegroups.com... > OK, this is a buffer issue it seems but I still cannot solve it. > Please help someone! I think it is a buffer issue because of the > following: > > Dim numBytesRead As Integer > Dim BUFFER_SIZE As Integer = FTPClient.ReceiveBufferSize <--- this > value comes out at 8192 > > numBytesRead = NWstream.Read(bytesToRead, 0, BUFFER_SIZE) > FileSTR.Write(bytesToRead, 0, BUFFER_SIZE) > > The file size is 211 bytes that I am testing with. So, the above code > writes out the 211 bytes and then files the file until it contains 8192 > bytes. If I debug the code, after the 211 bytes have passed each > subsequent byte has a value of 0 (zero). > > First of all, any ideas why it is doing this and not stopping when the > filestream has read it's 211 bytes like it is supposed to? And second, > any ideas on how to fix it? I tried setting the buffer size manually > to 211 bytes but then the file length is 8 bytes in size. > > This is driving me crazy! > > TIA > Rocky wrote:
> FileSTR.Write(bytesToRead, 0, numBytesRead) Sigh. If only everything in life was so simple. Looking at it now, I> > cannot believe how obvious my error was! I only wasted 3 days of my life on this. :-( Thanks. |
|||||||||||||||||||||||