using FileStream

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

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
 
Tony Johansson said:
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.
 
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
 
Tony said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top