comparison src/luan/modules/IoLuan.java @ 1257:e38f5869e9df

don't reset in send_redirect and other improvements
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 20 Sep 2018 21:04:41 -0600
parents ddd2ec2c0769
children 9fa8b8389578
comparison
equal deleted inserted replaced
1256:c147e2e877e3 1257:e38f5869e9df
53 return System.console().readLine(prompt); 53 return System.console().readLine(prompt);
54 } 54 }
55 55
56 56
57 public interface LuanWriter { 57 public interface LuanWriter {
58 public Object out();
58 public void write(LuanState luan,Object... args) throws LuanException, IOException; 59 public void write(LuanState luan,Object... args) throws LuanException, IOException;
59 public void close() throws IOException; 60 public void close() throws IOException;
60 } 61 }
61 62
62 private static LuanWriter luanWriter(final PrintStream out) { 63 private static LuanWriter luanWriter(final PrintStream out) {
63 return new LuanWriter() { 64 return new LuanWriter() {
65
66 public Object out() {
67 return out;
68 }
64 69
65 public void write(LuanState luan,Object... args) throws LuanException { 70 public void write(LuanState luan,Object... args) throws LuanException {
66 for( Object obj : args ) { 71 for( Object obj : args ) {
67 out.print( luan.toString(obj) ); 72 out.print( luan.toString(obj) );
68 } 73 }
79 } 84 }
80 85
81 private static LuanWriter luanWriter(final Writer out) { 86 private static LuanWriter luanWriter(final Writer out) {
82 return new LuanWriter() { 87 return new LuanWriter() {
83 88
89 public Object out() {
90 return out;
91 }
92
84 public void write(LuanState luan,Object... args) throws LuanException, IOException { 93 public void write(LuanState luan,Object... args) throws LuanException, IOException {
85 for( Object obj : args ) { 94 for( Object obj : args ) {
86 out.write( luan.toString(obj) ); 95 out.write( luan.toString(obj) );
87 } 96 }
88 } 97 }
97 return writer(luanWriter(out)); 106 return writer(luanWriter(out));
98 } 107 }
99 108
100 private static LuanTable writer(LuanWriter luanWriter) { 109 private static LuanTable writer(LuanWriter luanWriter) {
101 LuanTable writer = new LuanTable(); 110 LuanTable writer = new LuanTable();
111 writer.rawPut( "java", luanWriter.out() );
102 try { 112 try {
103 writer.rawPut( "write", new LuanJavaFunction( 113 writer.rawPut( "write", new LuanJavaFunction(
104 LuanWriter.class.getMethod( "write", LuanState.class, new Object[0].getClass() ), luanWriter 114 LuanWriter.class.getMethod( "write", LuanState.class, new Object[0].getClass() ), luanWriter
105 ) ); 115 ) );
106 writer.rawPut( "close", new LuanJavaFunction( 116 writer.rawPut( "close", new LuanJavaFunction(
113 } 123 }
114 124
115 125
116 public static LuanTable binaryWriter(final OutputStream out) { 126 public static LuanTable binaryWriter(final OutputStream out) {
117 LuanTable writer = new LuanTable(); 127 LuanTable writer = new LuanTable();
128 writer.rawPut( "java", out );
118 try { 129 try {
119 writer.rawPut( "write", new LuanJavaFunction( 130 writer.rawPut( "write", new LuanJavaFunction(
120 OutputStream.class.getMethod( "write", new byte[0].getClass() ), out 131 OutputStream.class.getMethod( "write", new byte[0].getClass() ), out
121 ) ); 132 ) );
122 writer.rawPut( "close", new LuanJavaFunction( 133 writer.rawPut( "close", new LuanJavaFunction(
440 } 451 }
441 452
442 @Override public LuanTable text_writer() throws IOException { 453 @Override public LuanTable text_writer() throws IOException {
443 LuanWriter luanWriter = new LuanWriter() { 454 LuanWriter luanWriter = new LuanWriter() {
444 private final Writer out = new StringWriter(); 455 private final Writer out = new StringWriter();
456
457 public Object out() {
458 return out;
459 }
445 460
446 public void write(LuanState luan,Object... args) throws LuanException, IOException { 461 public void write(LuanState luan,Object... args) throws LuanException, IOException {
447 for( Object obj : args ) { 462 for( Object obj : args ) {
448 out.write( luan.toString(obj) ); 463 out.write( luan.toString(obj) );
449 } 464 }