473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

FileStream is adding extra characters

Hi group,

I 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!

Mar 7 '06 #1
3 2494
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

Mar 8 '06 #2
FileSTR.Write(bytesToRead, 0, numBytesRead)
"Hugh Janus" <my*************@hotmail.com> wrote in message
news:11**********************@p10g2000cwp.googlegr oups.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

Mar 8 '06 #3

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.

Mar 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex | last post by:
Here's a tricky SQL question that has definitely driven me to the end of my rope. I'm using Oracle 9i and I need to perform some simple multiplication on a field and then display it with a percent...
6
by: DraguVaso | last post by:
Hi, I have a ComboBox of +- 15.000 items in it, via a DataSource. On Top of the Items-list, I want to add some values, based on a user choise (via a filter on the DataSource). Does anybody know...
0
by: BPNA | last post by:
I've encountered a problem with the SmtpMail.Send .Net framework 1.1 call in Windows 2003. What I'm seeing are 4 junk characters added at a consistent message position which happens at the 989th...
11
by: santosh | last post by:
Hi, A book that I'm currently using notes that the fgets() function does not return until Return is pressed or an EOF or other error is encountered. It then at most (in the absence of...
2
by: Roger Binns | last post by:
One thing I would like to do in my extension module is add extra frames to the traceback when an extension occurs. At the moment C code is invisible to tracebacks. This is relevant when the C...
2
by: ricky | last post by:
Can anybody help with the function to get rid of extra characters in the file. I want to remove the string from the file.So i read from input file and pass the string say "john" if found dnt write...
5
by: acarroll | last post by:
We're currently using Sharepoint to pull data into Access, so our tables are linked. In our tables in Access we are getting extra characters (;#) in our data that needs to be removed. I'm assuming...
2
by: =?Utf-8?B?YmJkb2J1ZGR5?= | last post by:
Hi, I am retrieving data from a binary field from a sql table and I am having a problem with extra characters being tacked onto the end. The thePass variable has some unknown spaces after it...
1
Ajm113
by: Ajm113 | last post by:
For some reason when I try to pass a string that is this: 'man!'; Into the first param of copy and the value of 4 in to the second param and 6 into the last argument for some reason copy is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.