comparison src/luan/modules/IoLuan.java @ 1181:51d1342e25ad

luanhost password handling
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 20 Feb 2018 19:50:30 -0700
parents e54ae41e9501
children 5d0cbd908582
comparison
equal deleted inserted replaced
1180:8ca49f5e114d 1181:51d1342e25ad
57 public interface LuanWriter { 57 public interface LuanWriter {
58 public void write(LuanState luan,Object... args) throws LuanException, IOException; 58 public void write(LuanState luan,Object... args) throws LuanException, IOException;
59 public void close() throws IOException; 59 public void close() throws IOException;
60 } 60 }
61 61
62 public static LuanTable textWriter(final PrintStream out) { 62 private static LuanWriter luanWriter(final PrintStream out) {
63 LuanWriter luanWriter = new LuanWriter() { 63 return new LuanWriter() {
64 64
65 public void write(LuanState luan,Object... args) throws LuanException { 65 public void write(LuanState luan,Object... args) throws LuanException {
66 for( Object obj : args ) { 66 for( Object obj : args ) {
67 out.print( luan.toString(obj) ); 67 out.print( luan.toString(obj) );
68 } 68 }
70 70
71 public void close() { 71 public void close() {
72 out.close(); 72 out.close();
73 } 73 }
74 }; 74 };
75 return writer(luanWriter); 75 }
76 } 76
77 77 public static LuanTable textWriter(final PrintStream out) {
78 public static LuanTable textWriter(final Writer out) { 78 return writer(luanWriter(out));
79 LuanWriter luanWriter = new LuanWriter() { 79 }
80
81 private static LuanWriter luanWriter(final Writer out) {
82 return new LuanWriter() {
80 83
81 public void write(LuanState luan,Object... args) throws LuanException, IOException { 84 public void write(LuanState luan,Object... args) throws LuanException, IOException {
82 for( Object obj : args ) { 85 for( Object obj : args ) {
83 out.write( luan.toString(obj) ); 86 out.write( luan.toString(obj) );
84 } 87 }
86 89
87 public void close() throws IOException { 90 public void close() throws IOException {
88 out.close(); 91 out.close();
89 } 92 }
90 }; 93 };
91 return writer(luanWriter); 94 }
95
96 public static LuanTable textWriter(final Writer out) {
97 return writer(luanWriter(out));
92 } 98 }
93 99
94 private static LuanTable writer(LuanWriter luanWriter) { 100 private static LuanTable writer(LuanWriter luanWriter) {
95 LuanTable writer = new LuanTable(); 101 LuanTable writer = new LuanTable();
96 try { 102 try {
338 344
339 public LuanTable binary_writer() throws IOException { 345 public LuanTable binary_writer() throws IOException {
340 return binaryWriter(new BufferedOutputStream(outputStream())); 346 return binaryWriter(new BufferedOutputStream(outputStream()));
341 } 347 }
342 348
349 public void write_text(LuanState luan,Object... args) throws LuanException, IOException {
350 LuanWriter luanWriter = luanWriter(new BufferedWriter(new OutputStreamWriter(outputStream())));
351 luanWriter.write(luan,args);
352 luanWriter.close();
353 }
354
343 @Override public LuanTable table() { 355 @Override public LuanTable table() {
344 LuanTable tbl = super.table(); 356 LuanTable tbl = super.table();
345 try { 357 try {
346 tbl.rawPut( "write", new LuanJavaFunction( 358 tbl.rawPut( "write", new LuanJavaFunction(
347 LuanIO.class.getMethod( "write", Object.class ), this 359 LuanIO.class.getMethod( "write", Object.class ), this
349 tbl.rawPut( "text_writer", new LuanJavaFunction( 361 tbl.rawPut( "text_writer", new LuanJavaFunction(
350 LuanIO.class.getMethod( "text_writer" ), this 362 LuanIO.class.getMethod( "text_writer" ), this
351 ) ); 363 ) );
352 tbl.rawPut( "binary_writer", new LuanJavaFunction( 364 tbl.rawPut( "binary_writer", new LuanJavaFunction(
353 LuanIO.class.getMethod( "binary_writer" ), this 365 LuanIO.class.getMethod( "binary_writer" ), this
366 ) );
367 tbl.rawPut( "write_text", new LuanJavaFunction(
368 LuanIO.class.getMethod( "write_text", LuanState.class, new Object[0].getClass() ), this
354 ) ); 369 ) );
355 } catch(NoSuchMethodException e) { 370 } catch(NoSuchMethodException e) {
356 throw new RuntimeException(e); 371 throw new RuntimeException(e);
357 } 372 }
358 return tbl; 373 return tbl;