mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-12 02:10:30 +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:
parent
e5f988e3fe
commit
ae0f093e73
@ -169,27 +169,42 @@ public class BinaryReadableHandle extends HandleGeneric
|
|||||||
{
|
{
|
||||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||||
|
|
||||||
boolean readAnything = false;
|
boolean readAnything = false, readRc = false;
|
||||||
while( true )
|
while( true )
|
||||||
{
|
{
|
||||||
single.clear();
|
single.clear();
|
||||||
int r = m_reader.read( single );
|
int read = m_reader.read( single );
|
||||||
if( r == -1 ) break;
|
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;
|
readAnything = true;
|
||||||
byte b = single.get( 0 );
|
|
||||||
if( b == '\n' )
|
byte chr = single.get( 0 );
|
||||||
|
if( chr == '\n' )
|
||||||
{
|
{
|
||||||
if( withTrailing ) stream.write( b );
|
if( withTrailing )
|
||||||
break;
|
{
|
||||||
|
if( readRc ) stream.write( '\r' );
|
||||||
|
stream.write( chr );
|
||||||
|
}
|
||||||
|
return new Object[] { stream.toByteArray() };
|
||||||
}
|
}
|
||||||
else
|
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 )
|
catch( IOException e )
|
||||||
{
|
{
|
||||||
|
@ -10,9 +10,10 @@ import dan200.computercraft.api.lua.LuaException;
|
|||||||
import dan200.computercraft.core.apis.ObjectWrapper;
|
import dan200.computercraft.core.apis.ObjectWrapper;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class BinaryReadableHandleTest
|
public class BinaryReadableHandleTest
|
||||||
{
|
{
|
||||||
@ -59,6 +60,24 @@ public class BinaryReadableHandleTest
|
|||||||
assertEquals( 1000, wrapper.<byte[]>callOf( "read", 11000 ).length );
|
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 )
|
private static ObjectWrapper fromLength( int length )
|
||||||
{
|
{
|
||||||
byte[] input = new byte[length];
|
byte[] input = new byte[length];
|
||||||
|
Loading…
Reference in New Issue
Block a user