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 );
|
2019-03-29 21:21:39 +00:00
|
|
|
m_writer = writer;
|
2017-05-12 21:49:44 +00:00
|
|
|
}
|
|
|
|
|
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 )
|
|
|
|
{
|
2019-01-05 18:50:47 +00:00
|
|
|
case 0: // write
|
2017-05-12 21:49:44 +00:00
|
|
|
{
|
|
|
|
checkOpen();
|
2019-01-05 18:50:47 +00:00
|
|
|
String text = args.length > 0 && args[0] != null ? args[0].toString() : "";
|
2017-05-12 21:49:44 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_writer.write( text, 0, text.length() );
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
catch( IOException e )
|
|
|
|
{
|
|
|
|
throw new LuaException( e.getMessage() );
|
|
|
|
}
|
|
|
|
}
|
2019-01-05 18:50:47 +00:00
|
|
|
case 1: // writeLine
|
2017-05-12 21:49:44 +00:00
|
|
|
{
|
|
|
|
checkOpen();
|
2019-01-05 18:50:47 +00:00
|
|
|
String text = args.length > 0 && args[0] != null ? args[0].toString() : "";
|
2017-05-12 21:49:44 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_writer.write( text, 0, text.length() );
|
|
|
|
m_writer.newLine();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
catch( IOException e )
|
|
|
|
{
|
|
|
|
throw new LuaException( e.getMessage() );
|
|
|
|
}
|
|
|
|
}
|
2019-01-05 18:50:47 +00:00
|
|
|
case 2: // flush
|
2017-05-12 21:49:44 +00:00
|
|
|
checkOpen();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_writer.flush();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
catch( IOException e )
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2019-01-05 18:50:47 +00:00
|
|
|
case 3: // close
|
2019-06-04 20:34:19 +00:00
|
|
|
checkOpen();
|
2017-05-12 21:49:44 +00:00
|
|
|
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
|
|
|
}
|