2018-12-31 09:41:58 +00:00
|
|
|
/*
|
|
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
2019-01-01 01:10:18 +00:00
|
|
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
2018-12-31 09:41:58 +00:00
|
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
|
|
*/
|
|
|
|
|
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;
|
2018-09-21 15:00:26 +00:00
|
|
|
import java.io.BufferedWriter;
|
|
|
|
import java.io.Closeable;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.channels.Channels;
|
|
|
|
import java.nio.channels.WritableByteChannel;
|
|
|
|
import java.nio.charset.Charset;
|
2018-11-16 20:58:49 +00:00
|
|
|
import java.nio.charset.CharsetEncoder;
|
|
|
|
import java.nio.charset.CodingErrorAction;
|
2018-09-21 15:00:26 +00:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2017-05-12 21:49:44 +00:00
|
|
|
|
2018-09-21 15:00:26 +00:00
|
|
|
public class EncodedWritableHandle extends HandleGeneric
|
2017-05-12 21:49:44 +00:00
|
|
|
{
|
2018-09-21 15:00:26 +00:00
|
|
|
private BufferedWriter m_writer;
|
2017-05-12 21:49:44 +00:00
|
|
|
|
2018-09-21 15:00:26 +00:00
|
|
|
public EncodedWritableHandle( @Nonnull BufferedWriter writer, @Nonnull Closeable closable )
|
2017-05-12 21:49:44 +00:00
|
|
|
{
|
2018-09-21 15:00:26 +00:00
|
|
|
super( closable );
|
2017-05-12 21:49:44 +00:00
|
|
|
this.m_writer = writer;
|
|
|
|
}
|
|
|
|
|
2018-09-21 15:00:26 +00:00
|
|
|
public EncodedWritableHandle( @Nonnull BufferedWriter writer )
|
2017-05-12 21:49:44 +00:00
|
|
|
{
|
2018-09-21 15:00:26 +00:00
|
|
|
this( writer, writer );
|
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;
|
2018-12-23 17:46:58 +00:00
|
|
|
if( args.length > 0 && args[0] != null )
|
2017-05-12 21:49:44 +00:00
|
|
|
{
|
2018-12-23 17:46:58 +00:00
|
|
|
text = args[0].toString();
|
2017-05-12 21:49:44 +00:00
|
|
|
}
|
|
|
|
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;
|
2018-12-23 17:46:58 +00:00
|
|
|
if( args.length > 0 && args[0] != null )
|
2017-05-12 21:49:44 +00:00
|
|
|
{
|
2018-12-23 17:46:58 +00:00
|
|
|
text = args[0].toString();
|
2017-05-12 21:49:44 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2018-09-21 15:00:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
public static BufferedWriter openUtf8( WritableByteChannel channel )
|
|
|
|
{
|
|
|
|
return open( channel, StandardCharsets.UTF_8 );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static BufferedWriter open( WritableByteChannel channel, Charset charset )
|
|
|
|
{
|
2018-12-23 17:46:58 +00:00
|
|
|
// Create a charset encoder with the same properties as StreamEncoder does for
|
2018-11-16 20:58:49 +00:00
|
|
|
// OutputStreams: namely, replace everything instead of erroring.
|
|
|
|
CharsetEncoder encoder = charset.newEncoder()
|
2018-12-23 17:46:58 +00:00
|
|
|
.onMalformedInput( CodingErrorAction.REPLACE )
|
|
|
|
.onUnmappableCharacter( CodingErrorAction.REPLACE );
|
2018-11-16 20:58:49 +00:00
|
|
|
return new BufferedWriter( Channels.newWriter( channel, encoder, -1 ) );
|
2018-09-21 15:00:26 +00:00
|
|
|
}
|
2017-05-12 21:49:44 +00:00
|
|
|
}
|