mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-06-25 22:53:22 +00:00
Fix read methods failing on malformed unicode.
Java configured the charset decoders/encoders for streams to REPLACE malformed characters rather than the default REPORT. It does not do the same for channels, and so we were catching an IO exception and returning null.
This commit is contained in:
parent
5fa01f8b96
commit
43459ec825
|
@ -10,6 +10,8 @@
|
|||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetDecoder;
|
||||
import java.nio.charset.CodingErrorAction;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static dan200.computercraft.core.apis.ArgumentHelper.optBoolean;
|
||||
|
@ -35,7 +37,7 @@ public EncodedReadableHandle( @Nonnull BufferedReader reader )
|
|||
@Nonnull
|
||||
@Override
|
||||
public String[] getMethodNames()
|
||||
{
|
||||
{
|
||||
return new String[] {
|
||||
"readLine",
|
||||
"readAll",
|
||||
|
@ -160,6 +162,11 @@ public static BufferedReader openUtf8( ReadableByteChannel channel )
|
|||
|
||||
public static BufferedReader open( ReadableByteChannel channel, Charset charset )
|
||||
{
|
||||
return new BufferedReader( Channels.newReader( channel, charset.newDecoder(), -1 ) );
|
||||
// Create a charset decoder with the same properties as StreamDecoder does for
|
||||
// InputStreams: namely, replace everything instead of erroring.
|
||||
CharsetDecoder decoder = charset.newDecoder()
|
||||
.onMalformedInput( CodingErrorAction.REPLACE )
|
||||
.onUnmappableCharacter( CodingErrorAction.REPLACE );
|
||||
return new BufferedReader( Channels.newReader( channel, decoder, -1 ) );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetEncoder;
|
||||
import java.nio.charset.CodingErrorAction;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class EncodedWritableHandle extends HandleGeneric
|
||||
|
@ -120,6 +122,11 @@ public static BufferedWriter openUtf8( WritableByteChannel channel )
|
|||
|
||||
public static BufferedWriter open( WritableByteChannel channel, Charset charset )
|
||||
{
|
||||
return new BufferedWriter( Channels.newWriter( channel, charset.newEncoder(), -1 ) );
|
||||
// Create a charset encoder with the same properties as StreamEncoder does for
|
||||
// OutputStreams: namely, replace everything instead of erroring.
|
||||
CharsetEncoder encoder = charset.newEncoder()
|
||||
.onMalformedInput( CodingErrorAction.REPLACE)
|
||||
.onUnmappableCharacter(CodingErrorAction.REPLACE);
|
||||
return new BufferedWriter( Channels.newWriter( channel, encoder, -1 ) );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user