comparison src/luan/lib/PickleCon.java @ 130:0594c132888b

cleanup git-svn-id: https://luan-java.googlecode.com/svn/trunk@131 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 10 Jun 2014 02:43:40 +0000
parents 486a0641bca4
children 0517a4a7fcc5
comparison
equal deleted inserted replaced
129:486a0641bca4 130:0594c132888b
47 byte[] a = new byte[size]; 47 byte[] a = new byte[size];
48 int i = 0; 48 int i = 0;
49 while( i < size ) { 49 while( i < size ) {
50 int n = in.read(a,i,size-i); 50 int n = in.read(a,i,size-i);
51 if( n == -1 ) 51 if( n == -1 )
52 throw luan.JAVA.exception( "end of stream" ); 52 throw luan.exception( "end of stream" );
53 i += n; 53 i += n;
54 } 54 }
55 return a; 55 return a;
56 } 56 }
57 57
58 public Object read() throws IOException, LuanException { 58 public Object read() throws IOException, LuanException {
59 ioModule.put("_read_binary",_read_binary); 59 ioModule.put("_read_binary",_read_binary);
60 src = in.readUTF(); 60 src = in.readUTF();
61 LuanFunction fn = BasicLib.load(luan,src,"pickle-reader",true,false); 61 LuanFunction fn = BasicLib.load(luan,src,"pickle-reader",true,false);
62 Object rtn = luan.JAVA.call(fn,null); 62 Object rtn = luan.call(fn);
63 ioModule.put("_binaries",null); 63 ioModule.put("_binaries",null);
64 return rtn; 64 return rtn;
65 } 65 }
66 66
67 public String pickle(Object obj) throws LuanException { 67 public String pickle(Object obj) throws LuanException {
76 if( obj instanceof byte[] ) { 76 if( obj instanceof byte[] ) {
77 byte[] a = (byte[])obj; 77 byte[] a = (byte[])obj;
78 binaries.add(a); 78 binaries.add(a);
79 return "Io._binaries[" + binaries.size() + "]"; 79 return "Io._binaries[" + binaries.size() + "]";
80 } 80 }
81 throw luan.JAVA.exception( "invalid type: " + obj.getClass() ); 81 throw luan.exception( "invalid type: " + obj.getClass() );
82 } 82 }
83 83
84 private String pickle(Object obj,Set<LuanTable> set) throws LuanException { 84 private String pickle(Object obj,Set<LuanTable> set) throws LuanException {
85 return obj instanceof LuanTable ? pickle((LuanTable)obj,set) : pickle(obj); 85 return obj instanceof LuanTable ? pickle((LuanTable)obj,set) : pickle(obj);
86 } 86 }
87 87
88 private String pickle(LuanTable tbl,Set<LuanTable> set) throws LuanException { 88 private String pickle(LuanTable tbl,Set<LuanTable> set) throws LuanException {
89 if( !set.add(tbl) ) { 89 if( !set.add(tbl) ) {
90 throw luan.JAVA.exception( "circular reference in table" ); 90 throw luan.exception( "circular reference in table" );
91 } 91 }
92 StringBuilder sb = new StringBuilder(); 92 StringBuilder sb = new StringBuilder();
93 sb.append( "{" ); 93 sb.append( "{" );
94 for( Map.Entry<Object,Object> entry : tbl ) { 94 for( Map.Entry<Object,Object> entry : tbl ) {
95 sb.append( "[" ); 95 sb.append( "[" );
109 for( byte[] a : binaries ) { 109 for( byte[] a : binaries ) {
110 sb.append( "Io._binaries[#Io._binaries+1] = Io._read_binary(" + a.length + ")\n" ); 110 sb.append( "Io._binaries[#Io._binaries+1] = Io._read_binary(" + a.length + ")\n" );
111 } 111 }
112 } 112 }
113 for( Object obj : args ) { 113 for( Object obj : args ) {
114 sb.append( luan.JAVA.toString(obj) ); 114 sb.append( luan.toString(obj) );
115 } 115 }
116 out.writeUTF( sb.toString() ); 116 out.writeUTF( sb.toString() );
117 for( byte[] a : binaries ) { 117 for( byte[] a : binaries ) {
118 out.write(a); 118 out.write(a);
119 } 119 }