changeset 1278:d83f6cc558de

add charset support
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 17 Dec 2018 15:40:58 -0700
parents 5ba660381bd5
children 323743a7f317
files src/luan/modules/IoLuan.java
diffstat 1 files changed, 26 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/IoLuan.java	Tue Dec 11 03:38:43 2018 -0700
+++ b/src/luan/modules/IoLuan.java	Mon Dec 17 15:40:58 2018 -0700
@@ -202,12 +202,15 @@
 
 
 	public static abstract class LuanIn {
+		protected String charset = null;
+
 		public abstract InputStream inputStream(LuanState luan) throws IOException, LuanException;
 		public abstract String to_string();
 		public abstract String to_uri_string();
 
 		public Reader reader(LuanState luan) throws IOException, LuanException {
-			return new InputStreamReader(inputStream(luan));
+			InputStream in = inputStream(luan);
+			return charset==null ? new InputStreamReader(in) : new InputStreamReader(in,charset);
 		}
 
 		public String read_text(LuanState luan) throws IOException, LuanException {
@@ -255,6 +258,14 @@
 			return cs;
 		}
 
+		public String charset() {
+			return charset;
+		}
+
+		public void set_charset(String charset) {
+			this.charset = charset;
+		}
+
 		public LuanTable table(LuanState luan) {
 			LuanTable tbl = new LuanTable(luan);
 			try {
@@ -283,6 +294,12 @@
 				tbl.rawPut( "checksum", new LuanJavaFunction(
 					LuanIn.class.getMethod( "checksum", LuanState.class ), this
 				) );
+				tbl.rawPut( "charset", new LuanJavaFunction(
+					LuanIn.class.getMethod( "charset" ), this
+				) );
+				tbl.rawPut( "set_charset", new LuanJavaFunction(
+					LuanIn.class.getMethod( "set_charset", String.class ), this
+				) );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
 			}
@@ -320,10 +337,15 @@
 	public static abstract class LuanIO extends LuanIn {
 		abstract OutputStream outputStream() throws IOException;
 
+		private Writer writer() throws IOException {
+			OutputStream out = outputStream();
+			return charset==null ? new OutputStreamWriter(out) : new OutputStreamWriter(out,charset);
+		}
+
 		public void write(LuanState luan,Object obj) throws LuanException, IOException {
 			if( obj instanceof String ) {
 				String s = (String)obj;
-				Writer out = new OutputStreamWriter(outputStream());
+				Writer out = writer();
 				out.write(s);
 				out.close();
 				return;
@@ -352,7 +374,7 @@
 		}
 
 		public LuanTable text_writer(LuanState luan) throws IOException {
-			return textWriter(luan,new BufferedWriter(new OutputStreamWriter(outputStream())));
+			return textWriter(luan,new BufferedWriter(writer()));
 		}
 
 		public LuanTable binary_writer(LuanState luan) throws IOException {
@@ -360,7 +382,7 @@
 		}
 
 		public void write_text(LuanState luan,Object... args) throws LuanException, IOException {
-			LuanWriter luanWriter = luanWriter(new BufferedWriter(new OutputStreamWriter(outputStream())));
+			LuanWriter luanWriter = luanWriter(new BufferedWriter(writer()));
 			luanWriter.write(luan,args);
 			luanWriter.close();
 		}