1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-10 18:33:00 +00:00

Merge pull request #455 from Wilma456/fileread

Add read() to Filehandle
This commit is contained in:
Daniel Ratcliffe
2018-01-13 00:58:19 +00:00
committed by GitHub
3 changed files with 27 additions and 1 deletions

View File

@@ -6,6 +6,8 @@ import dan200.computercraft.api.lua.LuaException;
import javax.annotation.Nonnull;
import java.io.*;
import static dan200.computercraft.core.apis.ArgumentHelper.*;
public class EncodedInputHandle extends HandleGeneric
{
private final BufferedReader m_reader;
@@ -49,6 +51,7 @@ public class EncodedInputHandle extends HandleGeneric
"readLine",
"readAll",
"close",
"read",
};
}
@@ -102,6 +105,26 @@ public class EncodedInputHandle extends HandleGeneric
// close
close();
return null;
case 3:
// read
checkOpen();
try
{
int count = optInt( args, 0, 1 );
if( count <= 0 || count >= 1024 * 16 )
{
throw new LuaException( "Count out of range" );
}
char[] bytes = new char[ count ];
count = m_reader.read( bytes );
if( count < 0 ) return null;
String str = new String( bytes, 0, count );
return new Object[] { str };
}
catch( IOException e )
{
return null;
}
default:
return null;
}