I am creating a System.IO.Stream
implementation for the purpose of setting a filter for HttpContext.Response.Filter
. I wish to know if all calls of Write(byte[], int, int)
will guarantee that the bytes written contain whole character byte sequences or if it is possible that a single character (in the case of a utf-32 encoding) could be split amongst calls.
public override void Write(byte[] buffer, int offset, int count) {
// Here `e' is a reference to `ctx.Response.ContentEncoding'
// from the original context.
char[] chars = e.GetChars(buffer, offset, count);
//... Stream processing logic here.
}
My current testing using utf-32 has proven that calls seem to always contain only whole character byte sequences but I wanted confirmation before I validated my assumption.
If it is likely that the bytes being written could be split amongst calls to Write
, what is the best approach to tackle this? I was thinking of performing a single byte width check in my constructor like so and using that to see if the byte array is divisible by that value. This however is naturally undesired though fairly trivial to implement.
// Here `e' is a reference to `ctx.Response.ContentEncoding'
// from the original context.
// `charLen' will yield 4 for a utf-32 encoding.
charLen = e.GetByteCount(new char[] { ' ' });