Re: using FileStream

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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]" <skeet@xxxxxxxxx> skrev i meddelandet
news:MPG.2308f496ca251b4beb9@xxxxxxxxxxxxxxxxxxxxxxx
Tony Johansson <johansson.andersson@xxxxxxxxx> wrote:
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 - <skeet@xxxxxxxxx>
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
.



Relevant Pages

  • Re: using FileStream
    ... I have read about the last parameter of GetBytes but I can't really ... I mean that the GC would take care of flushing the Encoder object e without ... *or* you can reuse it with subsequent bytes. ... it will expect the second half of the character ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A plea: Looking for a working MM5740AAE/N for Apple II Keyboard
    ... to see if any kind soul has any AAE/N encoder chips left to spare. ... the most common keyboard problems. ... In other words, the character that comes out of the character generator is used as an address for the address pins of the eprom, and the data at that address will be the proper ascii character. ...
    (comp.sys.apple2)
  • Re: codecs.getencoder encodes entire string ?
    ... For an encoder, I believe the answer is "no". ... the current UTF-8 encoder will then just encode the surrogate ... codepoint as if it was a proper character. ... If you extend the notion of "encoding", ...
    (comp.lang.python)
  • Re: How to generate java .properties files in python
    ... FTR, ... the 'backslashreplace' argument tells the encoder to replace any ... character that it can't encode with a backslash escape sequence. ...
    (comp.lang.python)