471,354 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

using FileStream

Hello!

Here I have an small piece of code from a program.

I have read about the last parameter of GetBytes but I can't really
understand it's use.

I mean that the GC would take care of flushing the Encoder object e without
having to say that.

So somebody explain the reason for having this last parameter in the method
GetBytes ?

Encoder e = Encoding.UTF8.GetEncoder();
e.GetBytes(chardata, 0, chardata.length, byteDataArray, 0, true):

//Tony

Aug 10 '08 #1
4 1952
Tony Johansson <jo*****************@telia.comwrote:
Here I have an small piece of code from a program.

I have read about the last parameter of GetBytes but I can't really
understand it's use.

I mean that the GC would take care of flushing the Encoder object e without
having to say that.
What does this have to do with a FileStream (your subject line)?
So somebody explain the reason for having this last parameter in the method
GetBytes ?

Encoder e = Encoding.UTF8.GetEncoder();
e.GetBytes(chardata, 0, chardata.length, byteDataArray, 0, true):
It's so that you can either reuse the encoder with a completely
different set of bytes, *or* you can reuse it with subsequent bytes.
This is important if the last set of bytes you passed ended half way
through a character. If you flush the encoder, it will start again from
scratch. If you don't, it will expect the second half of the character
to be at the start of the next block of data.

Most of the time you don't need Encoder though - simply using Encoding
is good enough in most cases.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 10 '08 #2
Hello!

What will the consquence be if I set the last parameter in method GetBytes
to false ?

static void Main(string[] args)
{
byte[] byData;
char[] charData;

try
{
FileStream aFile = new FileStream("Temp.txt", FileMode.Create);
charData = new byte(charData.Length);
Encoder e = Encoding,UTF8.GetEncoder());
e.GetBytes(charData, 0, charData.Length, byData, 0, true);

aFile.Write(byData, 0, byData.Length);
}
catch(IOException ex)
{
Console.Writeln(ex.ToString());
return;
}
}

//Tony

"Jon Skeet [C# MVP]" <sk***@pobox.comskrev i meddelandet
news:MP*********************@msnews.microsoft.com. ..
Tony Johansson <jo*****************@telia.comwrote:
Here I have an small piece of code from a program.

I have read about the last parameter of GetBytes but I can't really
understand it's use.

I mean that the GC would take care of flushing the Encoder object e
without
having to say that.

What does this have to do with a FileStream (your subject line)?
So somebody explain the reason for having this last parameter in the
method
GetBytes ?

Encoder e = Encoding.UTF8.GetEncoder();
e.GetBytes(chardata, 0, chardata.length, byteDataArray, 0, true):

It's so that you can either reuse the encoder with a completely
different set of bytes, *or* you can reuse it with subsequent bytes.
This is important if the last set of bytes you passed ended half way
through a character. If you flush the encoder, it will start again from
scratch. If you don't, it will expect the second half of the character
to be at the start of the next block of data.

Most of the time you don't need Encoder though - simply using Encoding
is good enough in most cases.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

Aug 11 '08 #3
On Mon, 11 Aug 2008 00:07:01 -0700, Tony Johansson
<jo*****************@telia.comwrote:
Hello!

What will the consquence be if I set the last parameter in method
GetBytes
to false ?
That's hard to say exactly, given that the code you posted doesn't seem to
be actual working code.

But the parameter tells the Encoder whether it should flush any partial
character data left after generating the bytes. Generally, you _don't_
want to do that and so _should_ pass false. The exception would be the
case where you're definitely done with some input stream, and you are
going to reuse the Encoder instance for another input stream.

Ideally though you shouldn't ever actually _need_ to pass "true". That
is, any input stream you're done with, you'd _hope_ that it didn't "leave
you hanging" with some partial character data at the end. But you can
pass "true" if you know that you've read the last bytes from your input
stream and you're going to reuse the Encoder instance, as a way of doing a
bit of "belt+suspenders" work to ensure that partial data from one stream
doesn't "infect" a subsequent stream.

Pete
Aug 11 '08 #4
Tony Johansson wrote:
Hello!

What will the consquence be if I set the last parameter in method GetBytes
to false ?
You might not get all the characters into the destination buffer. The
documentation clearly says that the last call to the GetBytes method
should have the parameter set to true to ensure that everything is
flushed to the output.
static void Main(string[] args)
{
byte[] byData;
char[] charData;

try
{
FileStream aFile = new FileStream("Temp.txt", FileMode.Create);
charData = new byte(charData.Length);
Encoder e = Encoding,UTF8.GetEncoder());
e.GetBytes(charData, 0, charData.Length, byData, 0, true);
You haven't created any array to use as destination buffer. The byData
variable is null, which will give you a ArgumentNullException when you
call the method.

Also, the method returns the number of bytes written into the buffer. If
you ignore this and write the entire buffer, you will get garbage bytes
appended to the data.
>
aFile.Write(byData, 0, byData.Length);
}
catch(IOException ex)
{
Console.Writeln(ex.ToString());
return;
}
}

//Tony

"Jon Skeet [C# MVP]" <sk***@pobox.comskrev i meddelandet
news:MP*********************@msnews.microsoft.com. ..
>Tony Johansson <jo*****************@telia.comwrote:
>>Here I have an small piece of code from a program.

I have read about the last parameter of GetBytes but I can't really
understand it's use.

I mean that the GC would take care of flushing the Encoder object e
without
>>having to say that.
What does this have to do with a FileStream (your subject line)?
>>So somebody explain the reason for having this last parameter in the
method
>>GetBytes ?

Encoder e = Encoding.UTF8.GetEncoder();
e.GetBytes(chardata, 0, chardata.length, byteDataArray, 0, true):
It's so that you can either reuse the encoder with a completely
different set of bytes, *or* you can reuse it with subsequent bytes.
This is important if the last set of bytes you passed ended half way
through a character. If you flush the encoder, it will start again from
scratch. If you don't, it will expect the second half of the character
to be at the start of the next block of data.

Most of the time you don't need Encoder though - simply using Encoding
is good enough in most cases.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com


--
Göran Andersson
_____
http://www.guffa.com
Aug 11 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

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.