comparison 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
comparison
equal deleted inserted replaced
280:2164b4479661 281:a1fa4fba99de
1 package luan.modules; 1 package luan.modules;
2 2
3 import java.io.InputStream;
3 import java.io.OutputStream; 4 import java.io.OutputStream;
4 import java.io.BufferedOutputStream;
5 import java.io.DataInputStream;
6 import java.io.DataOutputStream;
7 import java.io.IOException; 5 import java.io.IOException;
6 import java.io.EOFException;
7 import java.nio.charset.StandardCharsets;
8 import java.util.Set; 8 import java.util.Set;
9 import java.util.IdentityHashMap; 9 import java.util.IdentityHashMap;
10 import java.util.Collections; 10 import java.util.Collections;
11 import java.util.Map; 11 import java.util.Map;
12 import java.util.List; 12 import java.util.List;
19 import luan.LuanException; 19 import luan.LuanException;
20 20
21 21
22 public final class PickleCon { 22 public final class PickleCon {
23 final LuanState luan; 23 final LuanState luan;
24 private final DataInputStream in; 24 private final InputStream in;
25 private final LuanFunction _read_binary; 25 private final LuanFunction _read_binary;
26 private final DataOutputStream out; 26 private final OutputStream out;
27 private final List<byte[]> binaries = new ArrayList<byte[]>(); 27 private final List<byte[]> binaries = new ArrayList<byte[]>();
28 String src; 28 String src;
29 final LuanTable env = Luan.newTable(); 29 final LuanTable env = Luan.newTable();
30 30
31 PickleCon(LuanState luan,DataInputStream in,DataOutputStream out) { 31 PickleCon(LuanState luan,InputStream in,OutputStream out) {
32 this.in = in; 32 this.in = in;
33 this.luan = luan; 33 this.luan = luan;
34 try { 34 try {
35 this._read_binary = new LuanJavaFunction( 35 this._read_binary = new LuanJavaFunction(
36 PickleCon.class.getMethod( "_read_binary", Integer.TYPE ), this 36 PickleCon.class.getMethod( "_read_binary", Integer.TYPE ), this
40 } 40 }
41 41
42 this.out = out; 42 this.out = out;
43 } 43 }
44 44
45 public byte[] _read_binary(int size) throws IOException, LuanException { 45 public byte[] _read_binary(int size) throws IOException/*, LuanException*/ {
46 byte[] a = new byte[size]; 46 byte[] a = new byte[size];
47 int i = 0; 47 int i = 0;
48 while( i < size ) { 48 while( i < size ) {
49 int n = in.read(a,i,size-i); 49 int n = in.read(a,i,size-i);
50 if( n == -1 ) 50 if( n == -1 )
51 throw luan.exception( "end of stream" ); 51 // throw luan.exception( "end of stream" );
52 throw new EOFException();
52 i += n; 53 i += n;
53 } 54 }
54 return a; 55 return a;
55 } 56 }
56 57
57 public Object read() throws IOException, LuanException { 58 public Object read() throws IOException, LuanException {
58 env.put("_read_binary",_read_binary); 59 env.put("_read_binary",_read_binary);
59 try { 60 try {
60 src = in.readUTF(); 61 src = readString();
61 LuanFunction fn = BasicLuan.load(luan,src,"pickle-reader",env,false); 62 LuanFunction fn = BasicLuan.load(luan,src,"pickle-reader",env,false);
62 return luan.call(fn); 63 return luan.call(fn);
63 } finally { 64 } finally {
64 env.put("_binaries",null); 65 env.put("_binaries",null);
65 env.put("_read_binary",null); 66 env.put("_read_binary",null);
115 } 116 }
116 } 117 }
117 for( Object obj : args ) { 118 for( Object obj : args ) {
118 sb.append( luan.toString(obj) ); 119 sb.append( luan.toString(obj) );
119 } 120 }
120 out.writeUTF( sb.toString() ); 121 writeString( sb.toString() );
122 //System.out.println("aaaaaaaaaaaaaaaaaaaaaaaa");
123 //System.out.println(sb);
124 //System.out.println("zzzzzzzzzzzzzzzzzzzzzzzz");
121 for( byte[] a : binaries ) { 125 for( byte[] a : binaries ) {
122 out.write(a); 126 out.write(a);
123 } 127 }
124 out.flush(); 128 out.flush();
125 binaries.clear(); 129 binaries.clear();
127 131
128 public void close() throws IOException { 132 public void close() throws IOException {
129 in.close(); 133 in.close();
130 out.close(); 134 out.close();
131 } 135 }
136
137 String readString() throws IOException {
138 int len = readInt();
139 byte[] a = _read_binary(len);
140 return new String(a,StandardCharsets.UTF_8);
141 }
142
143 int readInt() throws IOException {
144 int ch1 = in.read();
145 int ch2 = in.read();
146 int ch3 = in.read();
147 int ch4 = in.read();
148 if ((ch1 | ch2 | ch3 | ch4) < 0)
149 throw new EOFException();
150 return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
151 }
152
153 void writeString(String s) throws IOException {
154 byte[] a = s.getBytes(StandardCharsets.UTF_8);
155 writeInt(a.length);
156 out.write(a);
157 }
158
159 void writeInt(int v) throws IOException {
160 out.write((v >>> 24) & 0xFF);
161 out.write((v >>> 16) & 0xFF);
162 out.write((v >>> 8) & 0xFF);
163 out.write((v >>> 0) & 0xFF);
164 }
132 } 165 }