/// <summary>
/// Decode a region of the buffer under the specified character set if
/// possible.
/// </summary>
/// <remarks>
/// Decode a region of the buffer under the specified character set if
/// possible.
/// If the byte stream cannot be decoded that way, the platform default is
/// tried and if that too fails, an exception is thrown.
/// </remarks>
/// <param name="cs">character set to use when decoding the buffer.</param>
/// <param name="buffer">buffer to pull raw bytes from.</param>
/// <param name="start">first position within the buffer to take data from.</param>
/// <param name="end">
/// one position past the last location within the buffer to take
/// data from.
/// </param>
/// <returns>
/// a string representation of the range <code>[start,end)</code>,
/// after decoding the region through the specified character set.
/// </returns>
/// <exception cref="Sharpen.CharacterCodingException">the input is not in any of the tested character sets.
/// </exception>
public static string DecodeNoFallback(System.Text.Encoding cs, byte[] buffer, int
start, int end)
{
ByteBuffer b = ByteBuffer.Wrap(buffer, start, end - start);
b.Mark();
// Try our built-in favorite. The assumption here is that
// decoding will fail if the data is not actually encoded
// using that encoder.
//
try
{
return(Decode(b, Constants.CHARSET));
}
catch (CharacterCodingException)
{
b.Reset();
}
if (!cs.Equals(Constants.CHARSET))
{
// Try the suggested encoding, it might be right since it was
// provided by the caller.
//
try
{
return(Decode(b, cs));
}
catch (CharacterCodingException)
{
b.Reset();
}
}
// Try the default character set. A small group of people
// might actually use the same (or very similar) locale.
//
System.Text.Encoding defcs = System.Text.Encoding.Default;
if (!defcs.Equals(cs) && !defcs.Equals(Constants.CHARSET))
{
try
{
return(Decode(b, defcs));
}
catch (CharacterCodingException)
{
b.Reset();
}
}
throw new CharacterCodingException();
}