internal void ChangeFormat( System.Text.Encoding encoding )
{
if (encoding == null)
{
return;
}
BCLDebug.Assert( _inSavedCharacter == -1, "There was a lookahead character at the stream change point, that means the parser is changing encodings too late" );
switch (_inTokenSource)
{
case TokenSource.UnicodeByteArray:
case TokenSource.UTF8ByteArray:
case TokenSource.ASCIIByteArray:
// these are the ones we can change on the fly
if (encoding == System.Text.Encoding.Unicode)
{
_inTokenSource = TokenSource.UnicodeByteArray;
return;
}
if (encoding == System.Text.Encoding.UTF8)
{
_inTokenSource = TokenSource.UTF8ByteArray;
return;
}
if (encoding == System.Text.Encoding.ASCII)
{
_inTokenSource = TokenSource.ASCIIByteArray;
return;
}
break;
case TokenSource.String:
case TokenSource.CharArray:
case TokenSource.NestedStrings:
// these are already unicode and encoding changes are moot
// they can't be further decoded
return;
}
// if we're here it means we don't know how to change
// to the desired encoding with the memory that we have
// we'll have to do this the hard way -- that means
// creating a suitable stream from what we've got
// this is thankfully the rare case as UTF8 and unicode
// dominate the scene
Stream stream = null;
switch (_inTokenSource)
{
case TokenSource.UnicodeByteArray:
case TokenSource.UTF8ByteArray:
case TokenSource.ASCIIByteArray:
stream = new MemoryStream(_inBytes, _inIndex, _inSize - _inIndex);
break;
case TokenSource.CharArray:
case TokenSource.String:
case TokenSource.NestedStrings:
BCLDebug.Assert(false, "attempting to change encoding on a non-changable source, should have been prevented earlier" );
return;
default:
StreamTokenReader reader = _inTokenReader as StreamTokenReader;
if (reader == null)
{
BCLDebug.Assert(false, "A new input source type has been added to the Tokenizer but it doesn't support encoding changes");
return;
}
stream = reader._in.BaseStream;
BCLDebug.Assert( reader._in.CurrentEncoding != null, "Tokenizer's StreamReader does not have an encoding" );
String fakeReadString = new String(' ', reader.NumCharEncountered);
stream.Position = reader._in.CurrentEncoding.GetByteCount( fakeReadString );
break;
}
BCLDebug.Assert(stream != null, "The XML stream with new encoding was not properly initialized for kind of input we had");
// we now have an initialized memory stream based on whatever source we had before
_inTokenReader = new StreamTokenReader( new StreamReader( stream, encoding ) );
_inTokenSource = TokenSource.Other;
}