mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-06-26 07:03:22 +00:00
83 lines
2.2 KiB
Java
83 lines
2.2 KiB
Java
![]() |
package dan200.computercraft.core.apis.handles;
|
||
|
|
||
|
import dan200.computercraft.api.lua.ILuaContext;
|
||
|
import dan200.computercraft.api.lua.LuaException;
|
||
|
import dan200.computercraft.shared.util.StringUtil;
|
||
|
|
||
|
import javax.annotation.Nonnull;
|
||
|
import java.io.IOException;
|
||
|
import java.io.OutputStream;
|
||
|
|
||
|
public class BinaryOutputHandle extends HandleGeneric
|
||
|
{
|
||
|
private final OutputStream m_writer;
|
||
|
|
||
|
public BinaryOutputHandle( OutputStream writer )
|
||
|
{
|
||
|
super( writer );
|
||
|
this.m_writer = writer;
|
||
|
}
|
||
|
|
||
|
@Nonnull
|
||
|
@Override
|
||
|
public String[] getMethodNames()
|
||
|
{
|
||
|
return new String[] {
|
||
|
"write",
|
||
|
"flush",
|
||
|
"close",
|
||
|
};
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException
|
||
|
{
|
||
|
switch( method )
|
||
|
{
|
||
|
case 0:
|
||
|
// write
|
||
|
checkOpen();
|
||
|
try
|
||
|
{
|
||
|
if( args.length > 0 && args[ 0 ] instanceof Number )
|
||
|
{
|
||
|
int number = ((Number) args[ 0 ]).intValue();
|
||
|
m_writer.write( number );
|
||
|
}
|
||
|
else if( args.length > 0 && args[ 0 ] instanceof String )
|
||
|
{
|
||
|
String value = (String) args[ 0 ];
|
||
|
m_writer.write( StringUtil.encodeString( value ) );
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
throw new LuaException( "Expected number" );
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
catch( IOException e )
|
||
|
{
|
||
|
throw new LuaException( e.getMessage() );
|
||
|
}
|
||
|
case 1:
|
||
|
// flush
|
||
|
checkOpen();
|
||
|
try
|
||
|
{
|
||
|
m_writer.flush();
|
||
|
return null;
|
||
|
}
|
||
|
catch( IOException e )
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
case 2:
|
||
|
//close
|
||
|
close();
|
||
|
return null;
|
||
|
default:
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
}
|