Mercurial Hosting > luan
changeset 281:a1fa4fba99de
change PickleCon to allow any size string
git-svn-id: https://luan-java.googlecode.com/svn/trunk@282 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 26 Nov 2014 04:14:52 +0000 |
parents | 2164b4479661 |
children | 38bd29e59a6e |
files | core/src/luan/modules/IoLuan.java core/src/luan/modules/PickleClient.java core/src/luan/modules/PickleCon.java core/src/luan/modules/PickleServer.java dist/jars/luan-core-trunk.jar dist/jars/luan-logging-trunk.jar dist/jars/luan-lucene-trunk.jar dist/jars/luan-mail-trunk.jar dist/jars/luan-web-trunk.jar |
diffstat | 9 files changed, 77 insertions(+), 30 deletions(-) [+] |
line wrap: on
line diff
--- a/core/src/luan/modules/IoLuan.java Mon Nov 24 22:22:01 2014 +0000 +++ b/core/src/luan/modules/IoLuan.java Wed Nov 26 04:14:52 2014 +0000 @@ -589,14 +589,14 @@ } public LuanTable Pickle_client(LuanState luan) throws IOException { - DataInputStream in = new DataInputStream(new BufferedInputStream(inputStream())); - DataOutputStream out = new DataOutputStream(new BufferedOutputStream(outputStream())); + InputStream in = new BufferedInputStream(inputStream()); + OutputStream out = new BufferedOutputStream(outputStream()); return new PickleClient(luan,in,out).table(); } public void run_pickle_server(LuanState luan) throws IOException { - DataInputStream in = new DataInputStream(new BufferedInputStream(inputStream())); - DataOutputStream out = new DataOutputStream(new BufferedOutputStream(outputStream())); + InputStream in = new BufferedInputStream(inputStream()); + OutputStream out = new BufferedOutputStream(outputStream()); new PickleServer(luan,in,out).run(); }
--- a/core/src/luan/modules/PickleClient.java Mon Nov 24 22:22:01 2014 +0000 +++ b/core/src/luan/modules/PickleClient.java Wed Nov 26 04:14:52 2014 +0000 @@ -1,7 +1,7 @@ package luan.modules; -import java.io.DataInputStream; -import java.io.DataOutputStream; +import java.io.InputStream; +import java.io.OutputStream; import java.io.IOException; import luan.Luan; import luan.LuanState; @@ -16,7 +16,7 @@ private final PickleCon con; private final LuanFunction _reversed_pickle; - PickleClient(LuanState luan,DataInputStream in,DataOutputStream out) { + PickleClient(LuanState luan,InputStream in,OutputStream out) { this(new PickleCon(luan,in,out)); }
--- a/core/src/luan/modules/PickleCon.java Mon Nov 24 22:22:01 2014 +0000 +++ b/core/src/luan/modules/PickleCon.java Wed Nov 26 04:14:52 2014 +0000 @@ -1,10 +1,10 @@ package luan.modules; +import java.io.InputStream; import java.io.OutputStream; -import java.io.BufferedOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; import java.io.IOException; +import java.io.EOFException; +import java.nio.charset.StandardCharsets; import java.util.Set; import java.util.IdentityHashMap; import java.util.Collections; @@ -21,14 +21,14 @@ public final class PickleCon { final LuanState luan; - private final DataInputStream in; + private final InputStream in; private final LuanFunction _read_binary; - private final DataOutputStream out; + private final OutputStream out; private final List<byte[]> binaries = new ArrayList<byte[]>(); String src; final LuanTable env = Luan.newTable(); - PickleCon(LuanState luan,DataInputStream in,DataOutputStream out) { + PickleCon(LuanState luan,InputStream in,OutputStream out) { this.in = in; this.luan = luan; try { @@ -42,13 +42,14 @@ this.out = out; } - public byte[] _read_binary(int size) throws IOException, LuanException { + public byte[] _read_binary(int size) throws IOException/*, LuanException*/ { byte[] a = new byte[size]; int i = 0; while( i < size ) { int n = in.read(a,i,size-i); if( n == -1 ) - throw luan.exception( "end of stream" ); +// throw luan.exception( "end of stream" ); + throw new EOFException(); i += n; } return a; @@ -57,7 +58,7 @@ public Object read() throws IOException, LuanException { env.put("_read_binary",_read_binary); try { - src = in.readUTF(); + src = readString(); LuanFunction fn = BasicLuan.load(luan,src,"pickle-reader",env,false); return luan.call(fn); } finally { @@ -117,7 +118,10 @@ for( Object obj : args ) { sb.append( luan.toString(obj) ); } - out.writeUTF( sb.toString() ); + writeString( sb.toString() ); +//System.out.println("aaaaaaaaaaaaaaaaaaaaaaaa"); +//System.out.println(sb); +//System.out.println("zzzzzzzzzzzzzzzzzzzzzzzz"); for( byte[] a : binaries ) { out.write(a); } @@ -129,4 +133,33 @@ in.close(); out.close(); } + + String readString() throws IOException { + int len = readInt(); + byte[] a = _read_binary(len); + return new String(a,StandardCharsets.UTF_8); + } + + int readInt() throws IOException { + int ch1 = in.read(); + int ch2 = in.read(); + int ch3 = in.read(); + int ch4 = in.read(); + if ((ch1 | ch2 | ch3 | ch4) < 0) + throw new EOFException(); + return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); + } + + void writeString(String s) throws IOException { + byte[] a = s.getBytes(StandardCharsets.UTF_8); + writeInt(a.length); + out.write(a); + } + + void writeInt(int v) throws IOException { + out.write((v >>> 24) & 0xFF); + out.write((v >>> 16) & 0xFF); + out.write((v >>> 8) & 0xFF); + out.write((v >>> 0) & 0xFF); + } }
--- a/core/src/luan/modules/PickleServer.java Mon Nov 24 22:22:01 2014 +0000 +++ b/core/src/luan/modules/PickleServer.java Wed Nov 26 04:14:52 2014 +0000 @@ -1,7 +1,9 @@ package luan.modules; -import java.io.DataInputStream; -import java.io.DataOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.StringWriter; +import java.io.PrintWriter; import java.io.IOException; import java.io.EOFException; import java.util.List; @@ -19,7 +21,7 @@ private final PickleCon con; private boolean isRunning; - PickleServer(LuanState luan,DataInputStream in,DataOutputStream out) { + PickleServer(LuanState luan,InputStream in,OutputStream out) { this(new PickleCon(luan,in,out)); } @@ -29,24 +31,36 @@ void next() throws IOException { try { - List<String> list = new ArrayList<String>(); try { Object[] result = Luan.array(con.read()); - list.add( "return true" ); + StringBuilder sb = new StringBuilder(); + sb.append( "return true" ); for( Object obj : result ) { - list.add( ", " ); - list.add( con.pickle(obj) ); + sb.append( ", " ); + sb.append( con.pickle(obj) ); } + sb.append( '\n' ); + con.write( sb.toString() ); } catch(LuanException e) { // System.out.println(e); //e.printStackTrace(); - list.add( "return false, " ); - list.add( con.pickle(e.getMessage()) ); - list.add( ", " ); - list.add( con.pickle(con.src) ); + StringBuilder sb = new StringBuilder(); + sb.append( "return false, " ); + sb.append( con.pickle(e.getMessage()) ); + sb.append( ", " ); + sb.append( con.pickle(con.src) ); + sb.append( '\n' ); +/* + Throwable cause = e.getCause(); + if( cause != null ) { + sb.append( "\nCaused by: " ); + StringWriter sw = new StringWriter(); + cause.printStackTrace(new PrintWriter(sw)); + sb.append( sw ); + } +*/ + con.write( sb.toString() ); } - list.add( "\n" ); - con.write( list.toArray() ); } catch(LuanException e2) { throw new RuntimeException(e2); }