File: REXPFactory.java

package info (click to toggle)
rjava 1.0-14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,188 kB
  • sloc: java: 13,223; ansic: 5,503; sh: 3,776; xml: 325; makefile: 250; perl: 33
file content (707 lines) | stat: -rw-r--r-- 23,347 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
package org.rosuda.REngine.Rserve.protocol;

// JRclient library - client interface to Rserve, see http://www.rosuda.org/Rserve/
// Copyright (C) 2004-8 Simon Urbanek
// --- for licensing information see LICENSE file in the original JRclient distribution ---

import java.util.*;

import org.rosuda.REngine.*;
import org.rosuda.REngine.Rserve.*;

/** representation of R-eXpressions in Java

    @version $Id$
*/
public class REXPFactory {
    /** xpression type: NULL */
    public static final int XT_NULL=0;
    /** xpression type: integer */
    public static final int XT_INT=1;
    /** xpression type: double */
    public static final int XT_DOUBLE=2;
    /** xpression type: String */
    public static final int XT_STR=3;
    /** xpression type: language construct (currently content is same as list) */
    public static final int XT_LANG=4;
    /** xpression type: symbol (content is symbol name: String) */
    public static final int XT_SYM=5;
    /** xpression type: RBool */    
    public static final int XT_BOOL=6;
    /** xpression type: S4 object
	@since Rserve 0.5 */
    public static final int XT_S4=7;
    /** xpression type: generic vector (RList) */
    public static final int XT_VECTOR=16;
    /** xpression type: dotted-pair list (RList) */
    public static final int XT_LIST=17;
    /** xpression type: closure (there is no java class for that type (yet?). currently the body of the closure is stored in the content part of the REXP. Please note that this may change in the future!) */
    public static final int XT_CLOS=18;
    /** xpression type: symbol name
	@since Rserve 0.5 */
    public static final int XT_SYMNAME=19;
    /** xpression type: dotted-pair list (w/o tags)
	@since Rserve 0.5 */
    public static final int XT_LIST_NOTAG=20;
    /** xpression type: dotted-pair list (w tags)
	@since Rserve 0.5 */
    public static final int XT_LIST_TAG=21;
    /** xpression type: language list (w/o tags)
	@since Rserve 0.5 */
    public static final int XT_LANG_NOTAG=22;
    /** xpression type: language list (w tags)
	@since Rserve 0.5 */
    public static final int XT_LANG_TAG=23;
    /** xpression type: expression vector */
    public static final int XT_VECTOR_EXP=26;
    /** xpression type: string vector */
    public static final int XT_VECTOR_STR=27;
    /** xpression type: int[] */
    public static final int XT_ARRAY_INT=32;
    /** xpression type: double[] */
    public static final int XT_ARRAY_DOUBLE=33;
    /** xpression type: String[] (currently not used, Vector is used instead) */
    public static final int XT_ARRAY_STR=34;
    /** internal use only! this constant should never appear in a REXP */
    public static final int XT_ARRAY_BOOL_UA=35;
    /** xpression type: RBool[] */
    public static final int XT_ARRAY_BOOL=36;
    /** xpression type: raw (byte[])
	@since Rserve 0.4-? */
    public static final int XT_RAW=37;
    /** xpression type: Complex[]
	@since Rserve 0.5 */
    public static final int XT_ARRAY_CPLX=38;
    /** xpression type: unknown; no assumptions can be made about the content */
    public static final int XT_UNKNOWN=48;

    /** xpression type: RFactor; this XT is internally generated (ergo is does not come from Rsrv.h) to support RFactor class which is built from XT_ARRAY_INT */
    public static final int XT_FACTOR=127; 

	/** used for transport only - has attribute */
	private static final int XT_HAS_ATTR=128;
	   
	int type;
	REXPFactory attr;
	REXP cont;
	RList rootList;
	
    public REXP getREXP() { return cont; }
    public REXPList getAttr() { return (attr==null)?null:(REXPList)attr.cont; }
	
	public REXPFactory() {
	}
	
	public REXPFactory(REXP r) throws REXPMismatchException {
		if (r == null) r=new REXPNull();
		REXPList a = r._attr();
		cont = r;
		if (a != null) attr = new REXPFactory(a);
		if (r instanceof REXPNull) {
			type=XT_NULL;
		} else if (r instanceof REXPList) {
			RList l = r.asList();
			type = l.isNamed()?XT_LIST_TAG:XT_LIST_NOTAG;
			if (r instanceof REXPLanguage)
				type = (type==XT_LIST_TAG)?XT_LANG_TAG:XT_LANG_NOTAG;
		} else if (r instanceof REXPGenericVector) {
			type = XT_VECTOR; // FIXME: may have to adjust names attr
		} else if (r instanceof REXPS4) {
			type = XT_S4;
		} else if (r instanceof REXPInteger) { // this includes factor - FIXME: do we need speacial handling?
			type = XT_ARRAY_INT;
		} else if (r instanceof REXPDouble) {
			type = XT_ARRAY_DOUBLE;
		} else if (r instanceof REXPString) {
			type = XT_ARRAY_STR;
		} else if (r instanceof REXPSymbol) {
			type = XT_SYMNAME;
		} else if (r instanceof REXPRaw) {
			type = XT_RAW;
		} else if (r instanceof REXPLogical) {
			type = XT_ARRAY_BOOL;
		} else {
			// throw new REXPMismatchException(r, "decode");
			System.err.println("*** REXPFactory unable to interpret "+r);
		}
	}

    /** parses byte buffer for binary representation of xpressions - read one xpression slot (descends recursively for aggregated xpressions such as lists, vectors etc.)
		@param buf buffer containing the binary representation
		@param o offset in the buffer to start at
        @return position just behind the parsed xpression. Can be use for successive calls to {@link #parseREXP} if more than one expression is stored in the binary array. */
    public int parseREXP(byte[] buf, int o) throws REXPMismatchException {
		int xl = RTalk.getLen(buf,o);
		boolean hasAtt = ((buf[o]&128)!=0);
        boolean isLong = ((buf[o]&64)!=0);
		int xt = (int)(buf[o]&63);
        //System.out.println("parseREXP: type="+xt+", len="+xl+", hasAtt="+hasAtt+", isLong="+isLong);
        if (isLong) o+=4;
        o+=4;
		int eox=o+xl;
	
		type=xt; attr=new REXPFactory(); cont=null;
		if (hasAtt) o = attr.parseREXP(buf, o);
		if (xt==XT_NULL) {
			cont = new REXPNull(getAttr());
			return o;
		}
		if (xt==XT_DOUBLE) {
			long lr = RTalk.getLong(buf,o);
			double[] d = new double[] { Double.longBitsToDouble(lr) };
			o+=8;
			if (o!=eox) {
				System.err.println("Warning: double SEXP size mismatch\n");
				o=eox;
			}
			cont = new REXPDouble(d, getAttr());
			return o;
		}
		if (xt==XT_ARRAY_DOUBLE) {
			int as=(eox-o)/8,i=0;
			double[] d=new double[as];
			while (o<eox) {
				d[i]=Double.longBitsToDouble(RTalk.getLong(buf,o));
				o+=8;
				i++;
			}
			if (o!=eox) {
				System.err.println("Warning: double array SEXP size mismatch\n");
				o=eox;
			}
			cont = new REXPDouble(d, getAttr());
			return o;
		}
		if (xt==XT_BOOL) {
			byte b[] = new byte[] { buf[o] };
			if (b[0] != 0 && b[0] != 1) b[0] = REXPLogical.NA;
			cont = new REXPLogical(b, getAttr());
			o++;
			if (o!=eox) {
                if (eox!=o+3) // o+3 could happen if the result was aligned (1 byte data + 3 bytes padding)
                    System.err.println("Warning: bool SEXP size mismatch\n");
				o=eox;
			}
			return o;
		}
		if (xt==XT_ARRAY_BOOL_UA) {
			int as=(eox-o), i=0;
			byte[] d=new byte[as];
			System.arraycopy(buf,o,d,0,eox-o);
			o = eox;
			for (int j = 0; j < d.length; j++) if (d[j] != 0 && d[j] != 1) d[j] = REXPLogical.NA;
			cont = new REXPLogical(d, getAttr());
			return o;
		}
        if (xt==XT_ARRAY_BOOL) {
            int as=RTalk.getInt(buf,o);
            o+=4;
            byte[] d=new byte[as];
			System.arraycopy(buf,o,d,0,as);
			for (int j = 0; j < d.length; j++) if (d[j] != 0 && d[j] != 1) d[j] = REXPLogical.NA;
			o = eox;
			cont = new REXPLogical(d, getAttr());
            return o;
        }
        if (xt==XT_INT) {
			int i[] = new int[] { RTalk.getInt(buf,o) };
			cont = new REXPInteger(i, getAttr());
			o+=4;
			if (o!=eox) {
				System.err.println("Warning: int SEXP size mismatch\n");
				o=eox;
			}
			return o;
		}
		if (xt==XT_ARRAY_INT) {
			int as=(eox-o)/4,i=0;
			int[] d=new int[as];
			while (o<eox) {
				d[i]=RTalk.getInt(buf,o);
				o+=4;
				i++;
			}
			if (o!=eox) {
				System.err.println("Warning: int array SEXP size mismatch\n");
				o=eox;
			}
			cont = null;
			// hack for lists - special lists attached to int are factors
			try {
			    if (getAttr()!=null) {
					REXP ca = getAttr().asList().at("class");
					REXP ls = getAttr().asList().at("levels");
					if (ca != null && ls != null && ca.asString().equals("factor")) {
						// R uses 1-based index, Java uses 0-based one
						cont = new REXPFactor(d, ls.asStrings(), getAttr());
						xt = XT_FACTOR;
					}
			    }
			} catch (Exception e) {
			}
			if (cont == null) cont = new REXPInteger(d, getAttr());
			return o;
		}
        if (xt==XT_RAW) {
            int as=RTalk.getInt(buf,o);
            o+=4;
            byte[] d=new byte[as];
			System.arraycopy(buf,o,d,0,as);
			o = eox;
			cont = new REXPRaw(d, getAttr());
            return o;
        }
		if (xt==XT_LIST_NOTAG || xt==XT_LIST_TAG ||
			xt==XT_LANG_NOTAG || xt==XT_LANG_TAG) {
			REXPFactory lc = new REXPFactory();
			REXPFactory nf = new REXPFactory();
			RList l = new RList();
			while (o<eox) {
				String name = null;
				o = lc.parseREXP(buf, o);
				if (xt==XT_LIST_TAG || xt==XT_LANG_TAG) {
					o = nf.parseREXP(buf, o);
					if (nf.cont.isSymbol() || nf.cont.isString()) name = nf.cont.asString();
				}
				if (name==null) l.add(lc.cont);
				else l.put(name, lc.cont);
			}
			cont = (xt==XT_LANG_NOTAG || xt==XT_LANG_TAG)?
				new REXPLanguage(l, getAttr()):
				new REXPList(l, getAttr());
			if (o!=eox) {
				System.err.println("Warning: int list SEXP size mismatch\n");
				o=eox;
			}
			return o;
		}
		if (xt==XT_LIST || xt==XT_LANG) { //old-style lists, for comaptibility with older Rserve versions - rather inefficient since we have to convert the recusively stored structures into a flat structure
			boolean isRoot = false;
			if (rootList == null) {
				rootList = new RList();
				isRoot = true;
			}
			REXPFactory headf = new REXPFactory();
			REXPFactory tagf = new REXPFactory();
			o = headf.parseREXP(buf, o);
			int elIndex = rootList.size();
			rootList.add(headf.cont);
			//System.out.println("HEAD="+headf.cont);
			o = parseREXP(buf, o); // we use ourselves recursively for the body
			if (o < eox) {
				o = tagf.parseREXP(buf, o);
				//System.out.println("TAG="+tagf.cont);
				if (tagf.cont != null && (tagf.cont.isString() || tagf.cont.isSymbol()))
					rootList.setKeyAt(elIndex, tagf.cont.asString());
			}
			if (isRoot) {
				cont = (xt==XT_LIST)?
				new REXPList(rootList, getAttr()):
				new REXPLanguage(rootList, getAttr());
				rootList = null;
				//System.out.println("result="+cont);
			}
			return o;
		}
		if (xt==XT_VECTOR || xt==XT_VECTOR_EXP) {
			Vector v=new Vector(); //FIXME: could we use RList?
			while(o<eox) {
				REXPFactory xx=new REXPFactory();
				o = xx.parseREXP(buf,o);
				v.addElement(xx.cont);
			}
			if (o!=eox) {
				System.err.println("Warning: int vector SEXP size mismatch\n");
				o=eox;
			}
			// fixup for lists since they're stored as attributes of vectors
			if (getAttr()!=null && getAttr().asList().at("names") != null) {
				REXP nam = getAttr().asList().at("names");
				String names[] = null;
				if (nam.isString()) names = nam.asStrings();
				else if (nam.isVector()) { // names could be a vector if supplied by old Rserve
					RList l = nam.asList();
					Object oa[] = l.toArray();
					names = new String[oa.length];
					for(int i = 0; i < oa.length; i++) names[i] = ((REXP)oa[i]).asString();
				}
				RList l = new RList(v, names);
				cont = (xt==XT_VECTOR_EXP)?
					new REXPExpressionVector(l, getAttr()):
					new REXPGenericVector(l, getAttr());
			} else
				cont = (xt==XT_VECTOR_EXP)?
					new REXPExpressionVector(new RList(v), getAttr()):
					new REXPGenericVector(new RList(v), getAttr());
			return o;
		}
		if (xt==XT_ARRAY_STR) {
			int c = 0, i = o;
			/* count the entries */
			while (i < eox) if (buf[i++] == 0) c++;
			String s[] = new String[c];
			if (c > 0) {
				c = 0; i = o;
				while (o < eox) {
					if (buf[o] == 0) {
						try {
							if (buf[i] == -1) { /* if the first byte is 0xff (-1 in signed char) then it either needs to be skipped (doubling) or there is an NA value */
								if (buf[i + 1] == 0)
									s[c] = null; /* NA */
								else
									s[c] = new String(buf, i + 1, o - i - 1, RConnection.transferCharset);
							} else
								s[c] = new String(buf, i, o - i, RConnection.transferCharset);
						} catch (java.io.UnsupportedEncodingException ex) {
							s[c]="";
						}
						c++;
						i = o + 1;
					}
					o++;
				}
			}
			cont = new REXPString(s, getAttr());
			return o;
		}
		if (xt==XT_VECTOR_STR) {
			Vector v=new Vector();
			while(o<eox) {
				REXPFactory xx=new REXPFactory();
				o = xx.parseREXP(buf,o);
				v.addElement(xx.cont.asString());
			}
			if (o!=eox) {
				System.err.println("Warning: int vector SEXP size mismatch\n");
				o=eox;
			}
			String sa[] = new String[v.size()];
			int i = 0; while (i < sa.length) { sa[i]=(String)v.get(i); i++; }
			cont = new REXPString(sa, getAttr());
			return o;
		}
		if (xt==XT_STR||xt==XT_SYMNAME) {
			int i = o;
			while (buf[i]!=0 && i<eox) i++;
			try {
				if (xt==XT_STR)
					cont = new REXPString(new String[] { new String(buf, o, i-o, RConnection.transferCharset) }, getAttr());
				else
					cont = new REXPSymbol(new String(buf, o, i-o, RConnection.transferCharset));					
			} catch(Exception e) {
				System.err.println("unable to convert string\n");
				cont = null;
			}
			o = eox;
			return o;
		}
		if (xt==XT_SYM) {
			REXPFactory sym = new REXPFactory();
			o = sym.parseREXP(buf, o); // PRINTNAME that's all we will use
			cont = new REXPSymbol(sym.getREXP().asString()); // content of a symbol is its printname string (so far)
			o=eox;
			return o;
		}
		
		if (xt==XT_CLOS) {
			/*
			REXP form=new REXP();
			REXP body=new REXP();
			o=parseREXP(form,buf,o);
			o=parseREXP(body,buf,o);
			if (o!=eox) {
				System.err.println("Warning: closure SEXP size mismatch\n");
				o=eox;
			}
			x.cont=body;
			 */
			o=eox;
			return o;
		}
		
		if (xt==XT_UNKNOWN) {
			cont = new REXPUnknown(RTalk.getInt(buf,o), getAttr());
			o=eox;
			return o;
		}
		
		if (xt==XT_S4) {
			cont = new REXPS4(getAttr());
			o=eox;
			return o;
		}
		
		cont = null;
		o = eox;
		System.err.println("unhandled type: "+xt);
		return o;
    }

    /** Calculates the length of the binary representation of the REXP including all headers. This is the amount of memory necessary to store the REXP via {@link #getBinaryRepresentation}.
        <p>Please note that currently only XT_[ARRAY_]INT, XT_[ARRAY_]DOUBLE and XT_[ARRAY_]STR are supported! All other types will return 4 which is the size of the header.
        @return length of the REXP including headers (4 or 8 bytes)*/
    public int getBinaryLength() throws REXPMismatchException {
		int l=0;
		int rxt = type;
		if (type==XT_LIST || type==XT_LIST_TAG || type==XT_LIST_NOTAG)
			rxt=(cont.asList()!=null && cont.asList().isNamed())?XT_LIST_TAG:XT_LIST_NOTAG;
		//System.out.print("len["+xtName(type)+"/"+xtName(rxt)+"] ");
		if (type==XT_VECTOR_STR) rxt=XT_ARRAY_STR; // VECTOR_STR is broken right now
		
		/*
		if (type==XT_VECTOR && cont.asList()!=null && cont.asList().isNamed())
			setAttribute("names",new REXPString(cont.asList().keys()));
		 */
		
		boolean hasAttr = false;
		REXPList a = getAttr();
		RList al = null;
		if (a!=null) al = a.asList();
		if (al != null && al.size()>0) hasAttr=true;
		if (hasAttr)
			l+=attr.getBinaryLength();
		switch (rxt) {
			case XT_NULL:
			case XT_S4:
				break;
			case XT_INT: l+=4; break;
			case XT_DOUBLE: l+=8; break;
			case XT_RAW: l+=4 + cont.asBytes().length; if ((l&3)>0) l=l-(l&3)+4; break;
			case XT_STR:
			case XT_SYMNAME:
				l+=(cont==null)?1:(cont.asString().length()+1);
				if ((l&3)>0) l=l-(l&3)+4;
					break;
			case XT_ARRAY_INT: l+=cont.asIntegers().length*4; break;
			case XT_ARRAY_DOUBLE: l+=cont.asDoubles().length*8; break;
			case XT_ARRAY_CPLX: l+=cont.asDoubles().length*8; break;
			case XT_ARRAY_BOOL: l += cont.asBytes().length + 4; if ((l & 3) > 0) l = l - (l & 3) + 4; break;
			case XT_LIST_TAG:
			case XT_LIST_NOTAG:
			case XT_LANG_TAG:
			case XT_LANG_NOTAG:
			case XT_LIST:
			case XT_VECTOR:
			{
				final RList lst = cont.asList();
				int i=0;
				while (i<lst.size()) {
					REXP x = lst.at(i);
					l += (x==null)?4:(new REXPFactory(x).getBinaryLength());
					if (rxt==XT_LIST_TAG) {
						int pl=l;
						String s = lst.keyAt(i);
						l+=4; // header for a symbol
						l+=(s==null)?1:(s.length()+1);
						if ((l&3)>0) l=l-(l&3)+4;
						// System.out.println("TAG length: "+(l-pl));
					}
					i++;
				}
				if ((l&3)>0) l=l-(l&3)+4;
				break;
			}
			case XT_ARRAY_STR:
			{
				String sa[] = cont.asStrings();
				int i=0;
				while (i < sa.length) {
					if (sa[i] != null) {
						try {
							byte b[] = sa[i].getBytes(RConnection.transferCharset);
							if (b.length > 0) {
								if (b[0] == -1) l++;
								l += b.length;
							}
							b = null;
						} catch (java.io.UnsupportedEncodingException uex) {
							// FIXME: we should so something ... so far we hope noone's gonna mess with the encoding
						}
					} else l++; // NA = -1
					l++;
					i++;
				}
				if ((l&3)>0) l=l-(l&3)+4;
				break;
			}
		} // switch
        if (l>0xfffff0) l+=4; // large data need 4 more bytes
							  // System.out.println("len:"+(l+4)+" "+xtName(rxt)+"/"+xtName(type)+" "+cont);
		return l+4; // add the header
    }

    /** Stores the REXP in its binary (ready-to-send) representation including header into a buffer and returns the index of the byte behind the REXP.
        <p>Please note that currently only XT_[ARRAY_]INT, XT_[ARRAY_]DOUBLE and XT_[ARRAY_]STR are supported! All other types will be stored as SEXP of the length 0 without any contents.
        @param buf buffer to store the REXP binary into
        @param off offset of the first byte where to store the REXP
        @return the offset of the first byte behind the stored REXP */
    public int getBinaryRepresentation(byte[] buf, int off) throws REXPMismatchException {
		int myl=getBinaryLength();
        boolean isLarge=(myl>0xfffff0);
		boolean hasAttr = false;
		final REXPList a = getAttr();
		RList al = null;
		if (a != null) al = a.asList();
		if (al != null && al.size()>0) hasAttr=true;
		int rxt=type, ooff=off;
		if (type==XT_VECTOR_STR) rxt=XT_ARRAY_STR; // VECTOR_STR is broken right now
		if (type==XT_LIST || type==XT_LIST_TAG || type==XT_LIST_NOTAG)
			rxt=(cont.asList()!=null && cont.asList().isNamed())?XT_LIST_TAG:XT_LIST_NOTAG;
		// System.out.println("@"+off+": "+xtName(rxt)+"/"+xtName(type)+" "+cont+" ("+myl+"/"+buf.length+") att="+hasAttr);
        RTalk.setHdr(rxt|(hasAttr?XT_HAS_ATTR:0),myl-(isLarge?8:4),buf,off);
        off+=(isLarge?8:4);
		if (hasAttr) off=attr.getBinaryRepresentation(buf, off);
		switch (rxt) {
			case XT_S4:
			case XT_NULL:
				break;
			case XT_INT: RTalk.setInt(cont.asInteger(),buf,off); break;
			case XT_DOUBLE: RTalk.setLong(Double.doubleToRawLongBits(cont.asDouble()),buf,off); break;
			case XT_ARRAY_INT:
			{
				int ia[]=cont.asIntegers();
				int i=0, io=off;
				while(i<ia.length) {
					RTalk.setInt(ia[i++],buf,io); io+=4;
				}
				break;
			}
			case XT_ARRAY_BOOL:
			{
				byte ba[] = cont.asBytes();
				int io = off;
				RTalk.setInt(ba.length, buf, io);
				io += 4;
				if (ba.length > 0) {
					for(int i =0; i < ba.length; i++)
						buf[io++] = (byte) ( (ba[i] == REXPLogical.NA) ? 2 : ((ba[i] == REXPLogical.FALSE) ? 0 : 1) );
					while ((io & 3) != 0) buf[io++] = 3;
				}
				break;
			}
			case XT_ARRAY_DOUBLE:
			{
				double da[]=cont.asDoubles();
				int i=0, io=off;
				while(i<da.length) {
					RTalk.setLong(Double.doubleToRawLongBits(da[i++]),buf,io); io+=8;
				}
				break;
			}
			case XT_RAW:
			{
				byte by[] = cont.asBytes();
				RTalk.setInt(by.length, buf, off); off+=4;
				System.arraycopy(by, 0, buf, off, by.length);
				break;
			}
			case XT_ARRAY_STR:
			{
				String sa[] = cont.asStrings();
				int i = 0, io = off;
				while (i < sa.length) {
					if (sa[i] != null) {
						try {
							byte b[] = sa[i].getBytes(RConnection.transferCharset);
							if (b.length > 0) {
								if (b[0] == -1) /* if the first entry happens to be -1 then we need to double it so it doesn't get confused with NAs */
									buf[io++] = -1;
								System.arraycopy(b, 0, buf, io, b.length);
								io += b.length;
							}
							b = null;
						} catch (java.io.UnsupportedEncodingException uex) {
							// FIXME: we should so something ... so far we hope noone's gonna mess with the encoding
						}
					} else
						buf[io++] = -1; /* NAs are stored as 0xff (-1 in signed bytes) */
					buf[io++] = 0;
					i++;
				}
				i = io - off;
				while ((i & 3) != 0) { buf[io++] = 1; i++; } // padding if necessary..
				break;
			}
			case XT_LIST_TAG:
			case XT_LIST_NOTAG:
			case XT_LANG_TAG:
			case XT_LANG_NOTAG:
			case XT_LIST:
			case XT_VECTOR:
			case XT_VECTOR_EXP:
			{
				int io = off;
				final RList lst = cont.asList();
				if (lst != null) {
					int i=0;
					while (i<lst.size()) {
						REXP x = lst.at(i);
						if (x == null) x=new REXPNull();
						io = new REXPFactory(x).getBinaryRepresentation(buf, io);
						if (rxt == XT_LIST_TAG || rxt == XT_LANG_TAG)
							io = new REXPFactory(new REXPSymbol(lst.keyAt(i))).getBinaryRepresentation(buf, io);
						i++;
					}
				}
				// System.out.println("io="+io+", expected: "+(ooff+myl));
				break;
			}
	    
			case XT_SYMNAME:
			case XT_STR:
				getStringBinaryRepresentation(buf, off, cont.asString());
				break;
		}
		return ooff+myl;
    }

    public static int getStringBinaryRepresentation(byte[] buf, int off, String s) {
		if (s==null) s="";
		int io=off;
		try {
			byte b[]=s.getBytes(RConnection.transferCharset);
			// System.out.println("<str> @"+off+", len "+b.length+" (cont "+buf.length+") \""+s+"\"");
			System.arraycopy(b,0,buf,io,b.length);
			io+=b.length;
			b=null;
		} catch (java.io.UnsupportedEncodingException uex) {
			// FIXME: we should so something ... so far we hope noone's gonna mess with the encoding
		}
		buf[io++]=0;
		while ((io&3)!=0) buf[io++]=0; // padding if necessary..
		return io;
    }

    /** returns human-readable name of the xpression type as string. Arrays are denoted by a trailing asterisk (*).
	@param xt xpression type
	@return name of the xpression type */
    public static String xtName(int xt) {
		if (xt==XT_NULL) return "NULL";
		if (xt==XT_INT) return "INT";
		if (xt==XT_STR) return "STRING";
		if (xt==XT_DOUBLE) return "REAL";
		if (xt==XT_BOOL) return "BOOL";
		if (xt==XT_ARRAY_INT) return "INT*";
		if (xt==XT_ARRAY_STR) return "STRING*";
		if (xt==XT_ARRAY_DOUBLE) return "REAL*";
		if (xt==XT_ARRAY_BOOL) return "BOOL*";
		if (xt==XT_ARRAY_CPLX) return "COMPLEX*";
		if (xt==XT_SYM) return "SYMBOL";
		if (xt==XT_SYMNAME) return "SYMNAME";
		if (xt==XT_LANG) return "LANG";
		if (xt==XT_LIST) return "LIST";
		if (xt==XT_LIST_TAG) return "LIST+T";
		if (xt==XT_LIST_NOTAG) return "LIST/T";
		if (xt==XT_LANG_TAG) return "LANG+T";
		if (xt==XT_LANG_NOTAG) return "LANG/T";
		if (xt==XT_CLOS) return "CLOS";
		if (xt==XT_RAW) return "RAW";
		if (xt==XT_S4) return "S4";
		if (xt==XT_VECTOR) return "VECTOR";
		if (xt==XT_VECTOR_STR) return "STRING[]";
		if (xt==XT_VECTOR_EXP) return "EXPR[]";
		if (xt==XT_FACTOR) return "FACTOR";
		if (xt==XT_UNKNOWN) return "UNKNOWN";
		return "<unknown "+xt+">";
    }	
}