1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 22:53:22 +00:00

Strip \r from .readLine on binary handles

Note, this is technically inconsistent with the spec. However, we'll use
this method for now in order to remain backwards compatible.

Fixes #109.
This commit is contained in:
SquidDev 2019-02-11 16:58:43 +00:00
parent e5f988e3fe
commit ae0f093e73
2 changed files with 45 additions and 11 deletions

View File

@ -169,27 +169,42 @@ else if( count == 0 && m_seekable != null )
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
boolean readAnything = false;
boolean readAnything = false, readRc = false;
while( true )
{
single.clear();
int r = m_reader.read( single );
if( r == -1 ) break;
int read = m_reader.read( single );
if( read <= 0 )
{
// Nothing else to read, and we saw no \n. Return the array. If we saw a \r, then add it
// back.
if( readRc ) stream.write( '\r' );
return readAnything ? new Object[] { stream.toByteArray() } : null;
}
readAnything = true;
byte b = single.get( 0 );
if( b == '\n' )
byte chr = single.get( 0 );
if( chr == '\n' )
{
if( withTrailing ) stream.write( b );
break;
if( withTrailing )
{
if( readRc ) stream.write( '\r' );
stream.write( chr );
}
return new Object[] { stream.toByteArray() };
}
else
{
stream.write( b );
// We want to skip \r\n, but obviously need to include cases where \r is not followed by \n.
// Note, this behaviour is non-standard compliant (strictly speaking we should have no
// special logic for \r), but we preserve compatibility with EncodedReadableHandle and
// previous behaviour of the io library.
if( readRc ) stream.write( '\r' );
readRc = chr == '\r';
if( !readRc ) stream.write( chr );
}
}
return readAnything ? new Object[] { stream.toByteArray() } : null;
}
catch( IOException e )
{

View File

@ -10,9 +10,10 @@
import dan200.computercraft.core.apis.ObjectWrapper;
import org.junit.Test;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
public class BinaryReadableHandleTest
{
@ -59,6 +60,24 @@ public void testReadLongPartialSmaller() throws LuaException
assertEquals( 1000, wrapper.<byte[]>callOf( "read", 11000 ).length );
}
@Test
public void testReadLine() throws LuaException
{
ObjectWrapper wrapper = new ObjectWrapper( new BinaryReadableHandle( new ArrayByteChannel( "hello\r\nworld\r!".getBytes( StandardCharsets.UTF_8 ) ) ) );
assertArrayEquals( "hello".getBytes( StandardCharsets.UTF_8 ), wrapper.callOf( "readLine" ) );
assertArrayEquals( "world\r!".getBytes( StandardCharsets.UTF_8 ), wrapper.callOf( "readLine" ) );
assertNull( wrapper.call( "readLine" ) );
}
@Test
public void testReadLineTrailing() throws LuaException
{
ObjectWrapper wrapper = new ObjectWrapper( new BinaryReadableHandle( new ArrayByteChannel( "hello\r\nworld\r!".getBytes( StandardCharsets.UTF_8 ) ) ) );
assertArrayEquals( "hello\r\n".getBytes( StandardCharsets.UTF_8 ), wrapper.callOf( "readLine", true ) );
assertArrayEquals( "world\r!".getBytes( StandardCharsets.UTF_8 ), wrapper.callOf( "readLine", true ) );
assertNull( wrapper.call( "readLine", true ) );
}
private static ObjectWrapper fromLength( int length )
{
byte[] input = new byte[length];