Mercurial Hosting > luan
comparison src/luan/modules/IoLuan.java @ 1278:d83f6cc558de
add charset support
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 17 Dec 2018 15:40:58 -0700 |
parents | 9fa8b8389578 |
children | 781ec0a92bb5 |
comparison
equal
deleted
inserted
replaced
1277:5ba660381bd5 | 1278:d83f6cc558de |
---|---|
200 return null; | 200 return null; |
201 } | 201 } |
202 | 202 |
203 | 203 |
204 public static abstract class LuanIn { | 204 public static abstract class LuanIn { |
205 protected String charset = null; | |
206 | |
205 public abstract InputStream inputStream(LuanState luan) throws IOException, LuanException; | 207 public abstract InputStream inputStream(LuanState luan) throws IOException, LuanException; |
206 public abstract String to_string(); | 208 public abstract String to_string(); |
207 public abstract String to_uri_string(); | 209 public abstract String to_uri_string(); |
208 | 210 |
209 public Reader reader(LuanState luan) throws IOException, LuanException { | 211 public Reader reader(LuanState luan) throws IOException, LuanException { |
210 return new InputStreamReader(inputStream(luan)); | 212 InputStream in = inputStream(luan); |
213 return charset==null ? new InputStreamReader(in) : new InputStreamReader(in,charset); | |
211 } | 214 } |
212 | 215 |
213 public String read_text(LuanState luan) throws IOException, LuanException { | 216 public String read_text(LuanState luan) throws IOException, LuanException { |
214 Reader in = reader(luan); | 217 Reader in = reader(luan); |
215 String s = Utils.readAll(in); | 218 String s = Utils.readAll(in); |
253 } | 256 } |
254 in.close(); | 257 in.close(); |
255 return cs; | 258 return cs; |
256 } | 259 } |
257 | 260 |
261 public String charset() { | |
262 return charset; | |
263 } | |
264 | |
265 public void set_charset(String charset) { | |
266 this.charset = charset; | |
267 } | |
268 | |
258 public LuanTable table(LuanState luan) { | 269 public LuanTable table(LuanState luan) { |
259 LuanTable tbl = new LuanTable(luan); | 270 LuanTable tbl = new LuanTable(luan); |
260 try { | 271 try { |
261 tbl.rawPut( "java", this ); | 272 tbl.rawPut( "java", this ); |
262 tbl.rawPut( "to_string", new LuanJavaFunction( | 273 tbl.rawPut( "to_string", new LuanJavaFunction( |
281 LuanIn.class.getMethod( "exists", LuanState.class ), this | 292 LuanIn.class.getMethod( "exists", LuanState.class ), this |
282 ) ); | 293 ) ); |
283 tbl.rawPut( "checksum", new LuanJavaFunction( | 294 tbl.rawPut( "checksum", new LuanJavaFunction( |
284 LuanIn.class.getMethod( "checksum", LuanState.class ), this | 295 LuanIn.class.getMethod( "checksum", LuanState.class ), this |
285 ) ); | 296 ) ); |
297 tbl.rawPut( "charset", new LuanJavaFunction( | |
298 LuanIn.class.getMethod( "charset" ), this | |
299 ) ); | |
300 tbl.rawPut( "set_charset", new LuanJavaFunction( | |
301 LuanIn.class.getMethod( "set_charset", String.class ), this | |
302 ) ); | |
286 } catch(NoSuchMethodException e) { | 303 } catch(NoSuchMethodException e) { |
287 throw new RuntimeException(e); | 304 throw new RuntimeException(e); |
288 } | 305 } |
289 return tbl; | 306 return tbl; |
290 } | 307 } |
317 } | 334 } |
318 }; | 335 }; |
319 | 336 |
320 public static abstract class LuanIO extends LuanIn { | 337 public static abstract class LuanIO extends LuanIn { |
321 abstract OutputStream outputStream() throws IOException; | 338 abstract OutputStream outputStream() throws IOException; |
339 | |
340 private Writer writer() throws IOException { | |
341 OutputStream out = outputStream(); | |
342 return charset==null ? new OutputStreamWriter(out) : new OutputStreamWriter(out,charset); | |
343 } | |
322 | 344 |
323 public void write(LuanState luan,Object obj) throws LuanException, IOException { | 345 public void write(LuanState luan,Object obj) throws LuanException, IOException { |
324 if( obj instanceof String ) { | 346 if( obj instanceof String ) { |
325 String s = (String)obj; | 347 String s = (String)obj; |
326 Writer out = new OutputStreamWriter(outputStream()); | 348 Writer out = writer(); |
327 out.write(s); | 349 out.write(s); |
328 out.close(); | 350 out.close(); |
329 return; | 351 return; |
330 } | 352 } |
331 if( obj instanceof byte[] ) { | 353 if( obj instanceof byte[] ) { |
350 } | 372 } |
351 throw new LuanException( "bad argument #1 to 'write' (string or binary or Io.uri expected)" ); | 373 throw new LuanException( "bad argument #1 to 'write' (string or binary or Io.uri expected)" ); |
352 } | 374 } |
353 | 375 |
354 public LuanTable text_writer(LuanState luan) throws IOException { | 376 public LuanTable text_writer(LuanState luan) throws IOException { |
355 return textWriter(luan,new BufferedWriter(new OutputStreamWriter(outputStream()))); | 377 return textWriter(luan,new BufferedWriter(writer())); |
356 } | 378 } |
357 | 379 |
358 public LuanTable binary_writer(LuanState luan) throws IOException { | 380 public LuanTable binary_writer(LuanState luan) throws IOException { |
359 return binaryWriter(luan,new BufferedOutputStream(outputStream())); | 381 return binaryWriter(luan,new BufferedOutputStream(outputStream())); |
360 } | 382 } |
361 | 383 |
362 public void write_text(LuanState luan,Object... args) throws LuanException, IOException { | 384 public void write_text(LuanState luan,Object... args) throws LuanException, IOException { |
363 LuanWriter luanWriter = luanWriter(new BufferedWriter(new OutputStreamWriter(outputStream()))); | 385 LuanWriter luanWriter = luanWriter(new BufferedWriter(writer())); |
364 luanWriter.write(luan,args); | 386 luanWriter.write(luan,args); |
365 luanWriter.close(); | 387 luanWriter.close(); |
366 } | 388 } |
367 | 389 |
368 @Override public LuanTable table(LuanState luan) { | 390 @Override public LuanTable table(LuanState luan) { |