2017-05-12 21:49:44 +00:00
|
|
|
package dan200.computercraft.core.apis.handles;
|
|
|
|
|
|
|
|
import dan200.computercraft.api.lua.ILuaContext;
|
|
|
|
import dan200.computercraft.api.lua.LuaException;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
public class EncodedOutputHandle extends HandleGeneric
|
|
|
|
{
|
|
|
|
private final BufferedWriter m_writer;
|
|
|
|
|
|
|
|
public EncodedOutputHandle( BufferedWriter writer )
|
|
|
|
{
|
|
|
|
super( writer );
|
|
|
|
this.m_writer = writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public EncodedOutputHandle( OutputStream stream )
|
|
|
|
{
|
|
|
|
this( stream, "UTF-8" );
|
|
|
|
}
|
|
|
|
|
|
|
|
public EncodedOutputHandle( OutputStream stream, String encoding )
|
|
|
|
{
|
2017-05-17 18:25:13 +00:00
|
|
|
this( makeWriter( stream, encoding ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
private static BufferedWriter makeWriter( OutputStream stream, String encoding )
|
|
|
|
{
|
2017-05-12 21:49:44 +00:00
|
|
|
if( encoding == null ) encoding = "UTF-8";
|
|
|
|
OutputStreamWriter streamWriter;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
streamWriter = new OutputStreamWriter( stream, encoding );
|
|
|
|
}
|
|
|
|
catch( UnsupportedEncodingException e )
|
|
|
|
{
|
|
|
|
streamWriter = new OutputStreamWriter( stream );
|
|
|
|
}
|
2017-05-17 18:25:13 +00:00
|
|
|
return new BufferedWriter( streamWriter );
|
2017-05-12 21:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String[] getMethodNames()
|
|
|
|
{
|
|
|
|
return new String[] {
|
|
|
|
"write",
|
|
|
|
"writeLine",
|
|
|
|
"flush",
|
|
|
|
"close",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException
|
|
|
|
{
|
|
|
|
switch( method )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
// write
|
|
|
|
checkOpen();
|
|
|
|
String text;
|
|
|
|
if( args.length > 0 && args[ 0 ] != null )
|
|
|
|
{
|
|
|
|
text = args[ 0 ].toString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
text = "";
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_writer.write( text, 0, text.length() );
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
catch( IOException e )
|
|
|
|
{
|
|
|
|
throw new LuaException( e.getMessage() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
// writeLine
|
|
|
|
checkOpen();
|
|
|
|
String text;
|
|
|
|
if( args.length > 0 && args[ 0 ] != null )
|
|
|
|
{
|
|
|
|
text = args[ 0 ].toString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
text = "";
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_writer.write( text, 0, text.length() );
|
|
|
|
m_writer.newLine();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
catch( IOException e )
|
|
|
|
{
|
|
|
|
throw new LuaException( e.getMessage() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
// flush
|
|
|
|
checkOpen();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_writer.flush();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
catch( IOException e )
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
case 3:
|
|
|
|
// close
|
|
|
|
close();
|
|
|
|
return null;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|