Re: What replaces StringBufferInputStream
"Piotr Kobzda" <pikob@gazeta.pl> wrote in message
news:ed6l7p$lfh$1@inews.gazeta.pl...
Chris Uppal wrote:
A pity that it doesn't throw an exception. "Suppressing"
the error is probably the best behaviour for most purposes, but it would
be
nice (now I come to think of it) if we could tell a decoding/encoding
stream
that we want it to be strict (just as we can tell an actual
CharserDecoder how
it should treat "wrong" inputs).
You can easily achieve that setting "strictness" on CharsetDecoder
produced by your desired Charset.
Strictness you can express this way:
CharsetDecoder dec = Charset.forName("US-ASCII").newDecoder();
dec.onMalformedInput(CodingErrorAction.REPORT);
dec.onUnmappableCharacter(CodingErrorAction.REPORT);
Applying it to Mike's example as:
InputStreamReader isr = new InputStreamReader(bais, dec);
Should give you an expected results.
Thank you; I've changed my example to use it, resulting in
import java.io.*;
import java.nio.charset.*;
public class BadAscii
{
public static void main(String[] args) throws Exception
{
byte arr[] = { (byte)0x40, (byte)0x80};
CharsetDecoder dec = Charset.forName("US-ASCII").newDecoder();
dec.onMalformedInput(CodingErrorAction.REPORT);
dec.onUnmappableCharacter(CodingErrorAction.REPORT);
ByteArrayInputStream bais = new ByteArrayInputStream(arr);
InputStreamReader isr = new InputStreamReader(bais, dec);
while (true)
{
int r = isr.read();
if (r < 0)
break;
System.out.println(
(char)r + "(" + Integer.toHexString(r) + ")");
}
}
}
and it now produces
Exception in thread "main" java.nio.charset.MalformedInputException: Input
length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:260)
at
sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:463)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:182)
at sun.nio.cs.StreamDecoder.read0(StreamDecoder.java:131)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:117)
at java.io.InputStreamReader.read(InputStreamReader.java:151)
at BadAscii.main(BadAscii.java:18)
By the way, you can observe that the decoder tries to process the entire
array at once, since the first character, which is legitimate ASCII, is
never returned.