473,249 Members | 1,552 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,249 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 2007
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Shawn | last post by:
Hi. I'm using a FileStream (instead of just the path to the xml file) to load an XmlDocument. I'm doing this because I need to be able to prevent other processes to update the file I'm working on....
1
by: AMP | last post by:
Hello, I am trying to see if a FileStream Opened using an"if" clause: FileStream infile = new FileStream("c:\\blink2.txt", FileMode.Open, FileAccess.Read, FileShare.Read); if (infile==0)..... ...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
13
by: =?Utf-8?B?Um9nZXIgTWFydGlu?= | last post by:
This is a follow-up to my post "Silverlight video doesn't work when file is streamed from handler in ASP.net" at...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.