diff core/src/luan/modules/PickleCon.java @ 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 ec016471c6eb
children 23b99a5039b5
line wrap: on
line diff
--- 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);
+	}
 }