File: VirtuosoOutputStream.java

package info (click to toggle)
virtuoso-opensource 6.1.6%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 260,060 kB
  • ctags: 123,765
  • sloc: ansic: 652,532; sql: 458,419; xml: 282,834; java: 61,031; sh: 40,031; cpp: 36,890; cs: 25,240; php: 12,692; yacc: 9,523; lex: 7,018; makefile: 6,157; jsp: 4,484; awk: 1,643; perl: 1,013; ruby: 1,003; python: 326
file content (1001 lines) | stat: -rw-r--r-- 29,676 bytes parent folder | download | duplicates (2)
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
/*
 *  $Id$
 *
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *
 *  Copyright (C) 1998-2012 OpenLink Software
 *
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

package virtuoso.jdbc2;

import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
import java.math.*;
import openlink.util.*;

/**
 * The VirtuosoOutputStream is used to serialize data during
 * a transaction between the JDBC driver and Virtuoso DBMS.
 *
 * @version 1.0 (JDBC API 2.0 implementation)
 */
class VirtuosoOutputStream extends BufferedOutputStream
{
   // The connection attached to this stream
   private VirtuosoConnection connection;
   private byte[] tmp = new byte[16];
/*
   // -------------------- A new BufferedOutputStream design -----------------
   // The buffer
   private byte[] buffer;

   private int count;

   private int pos;

   private OutputStream out;

   private void flushBuffer() throws IOException
   {
      //System.out.println("OutputStream:flushBuffer : "+this);
      if(count > 0)
      {
         out.write(buffer,0,count);
         count = 0;
      }
   }

   public void write(int b) throws IOException
   {
      //System.out.println("OutputStream:write(int) : "+this);
      if(count >= buffer.length)
         flushBuffer();
      buffer[count++] = (byte)b;
   }

   public void write(byte b[]) throws IOException
   {
      this.write(b, 0, b.length);
   }

   public void write(byte b[], int off, int len) throws IOException
   {
      //System.out.println("OutputStream:write(b,off,len) : "+this);
      if(len >= (buffer.length-count))
      {
         flushBuffer();
         out.write(b,off,len);
         return;
      }
      System.arraycopy(b,off,buffer,count,len);
      count += len;
   }

   public void flush() throws IOException
   {
      //System.out.println("OutputStream:flush : "+this);
      flushBuffer();
      out.flush();
   }

   public void close() throws IOException
   {
      flush();
      out.close();
      buffer = null;
   }
*/
   // End of the OutputBufferedStram design
   private static final int DefaultBufferSize = 2048;
   /**
    * Constructs a VirtuosoOutputStream using a OutputStream from
    * a socket connecting the driver to the database.
    *
    * @param connection The connection attached to this stream.
    * @param output  The OutputStream representing data from the database.
    * @exception java.io.IOException   An error occurred creating a
    * BufferedOutputStream for the OutputStream
    * @see java.io.BufferOutputStream
    */
   VirtuosoOutputStream(VirtuosoConnection connection, OutputStream output) throws IOException
   {
      this (connection,output,DefaultBufferSize);
   }

   /**
    * Constructs a VirtuosoOutputStream using a OutputStream from
    * a socket connecting the driver to the database with a specific
    * buffer size.
    *
    * @param connection The connection attached to this stream.
    * @param output  The OutputStream representing data from the database.
    * @param size The BufferOutputStream buffer size.
    * @exception java.io.IOException   An error occurred creating a
    * BufferedOutputStream for the OutputStream
    * @see java.io.BufferOutputStream
    */
   VirtuosoOutputStream(VirtuosoConnection connection, OutputStream output, int size) throws IOException
   {
     super (output, size);
     this.connection = connection;
/*
      out = output;
      // Setup the buffer
      buffer = new byte[size];
*/
   }

   /**
    * Constructs a VirtuosoOutputStream using from the Socket
    * connecting the driver to the database.
    *
    * @param connection The connection attached to this stream.
    * @param out  The Socket representing data from the database.
    * @exceptionput java.io.IOException   An error occurred creating a
    * BufferedOutputStream for the OutputStream
    * @see java.net.Socket
    */
   VirtuosoOutputStream(VirtuosoConnection connection, Socket output) throws IOException
   {
      this (connection,output.getOutputStream());
   }

   /**
    * Constructs a VirtuosoOutputStream using from the Socket
    * connecting the driver to the database with a specific size
    * buffer.
    *
    * @param connection The connection attached to this stream.
    * @param output  The Socket representing data from the database.
    * @param size The BufferOutputStream buffer size.
    * @exception java.io.IOException   An error occurred creating a
    * BufferedOutputStream for the OutputStream
    * @see java.net.Socket
    */
   VirtuosoOutputStream(VirtuosoConnection connection, Socket output, int size) throws IOException
   {
      this (connection,output.getOutputStream(),size);
   }

   /**
    * Check if the output stream is closed.
    *
    * @return true if the connection is closed, false if it's still open.
    */
   protected boolean isClosed()
   {
      return (out == null);
   }

   /**
    * Method uses to serialize an object in the DV format.
    *
    * @param Object	Object to serialize.
    * @exception java.io.IOException	An error occurred on the stream.
    * @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
    */
   protected void write_object(Object obj) throws IOException, VirtuosoException
   {
     int tag;
     //System.err.println("Writing : object=[" + obj + "]");
     switch(tag = whatIs(obj))
       {
	 case VirtuosoTypes.DV_LIST_OF_POINTER:
#if JDK_VER >= 12
	       {
		 //System.out.println("DV_ARRAY_OF_POINTER");
		 LinkedList o = (LinkedList)obj;
		 int length = o.size();
		 write(VirtuosoTypes.DV_ARRAY_OF_POINTER);
		 writeint(length);
		 for(ListIterator it = o.listIterator(); it.hasNext(); )
		   {
		     write_object(it.next());
		   }
		 return;
	       }
#endif
	 case VirtuosoTypes.DV_ARRAY_OF_POINTER:
	       {
		 //System.out.println("DV_ARRAY_OF_POINTER");
		 openlink.util.Vector o = (openlink.util.Vector)obj;
		 int length = o.size();
		 write(tag);
		 writeint(length);
		 for(int i = 0; i < length; i++)
		   {
		     write_object(o.elementAt(i));
		   }
		 return;
	       }
	 case VirtuosoTypes.DV_ARRAY_OF_LONG:
	       {
		 //System.out.println("DV_ARRAY_OF_LONG");
		 VectorOfLong o = (VectorOfLong)obj;
		 int length = o.size();
		 write(tag);
		 writeint(length);
		 for(int i = 0; i < length; i++)
		   {
		     writelongint(((Long)o.elementAt(i)).longValue());
		   }
		 return;
	       }
	 case VirtuosoTypes.DV_ARRAY_OF_LONG_PACKED:
	       {
		 // ???? Perhaps a VectorOfLongPacked class to design ????
		 //System.out.println("DV_ARRAY_OF_LONG_PACKED");
		 openlink.util.Vector o = (openlink.util.Vector)obj;
		 int length = o.size();
		 write(tag);
		 writeint(length);
		 for(int i = 0; i < length; i++)
		   {
		     writeint(((Number)o.elementAt(i)).longValue());
		   }
		 return;
	       }
	 case VirtuosoTypes.DV_ARRAY_OF_DOUBLE:
	       {
		 //System.out.println("DV_ARRAY_OF_DOUBLE");
		 VectorOfDouble o = (VectorOfDouble)obj;
		 int length = o.size();
		 write(tag);
		 writeint(length);
		 for(int i = 0; i < length; i++)
		   {
		     writerawdouble(((Double)o.elementAt(i)).doubleValue());
		   }
		 return;
	       }
	 case VirtuosoTypes.DV_ARRAY_OF_FLOAT:
	       {
		 //System.out.println("DV_ARRAY_OF_FLOAT");
		 VectorOfFloat o = (VectorOfFloat)obj;
		 int length = o.size();
		 write(tag);
		 writeint(length);
		 for(int i = 0; i < length; i++)
		   {
		     writerawfloat(((Float)o.elementAt(i)).floatValue());
		   }
		 return;
	       }
	 case VirtuosoTypes.DV_SHORT_STRING_SERIAL:
	 case VirtuosoTypes.DV_STRING:
	 case VirtuosoTypes.DV_C_STRING:
	 case VirtuosoTypes.DV_WIDE:
	 case VirtuosoTypes.DV_LONG_WIDE:
	       {
		 if (obj instanceof VirtuosoExplicitString)
		   ((VirtuosoExplicitString)obj).write(this);
		 else
		   writestring((String)obj);
		 return;
	       }
	 case VirtuosoTypes.DV_SINGLE_FLOAT:
	       {
		 writefloat(((Float)obj).floatValue());
		 return;
	       }
	 case VirtuosoTypes.DV_DOUBLE_FLOAT:
	       {
		 writedouble(((Double)obj).doubleValue());
		 return;
	       }
	 case VirtuosoTypes.DV_NULL:
	 case VirtuosoTypes.DV_DB_NULL:
	       {
		 //System.out.println("DV_DB_NULL");
		 write(tag);
		 return;
	       }
	 case VirtuosoTypes.DV_SHORT_CONT_STRING:
	 case VirtuosoTypes.DV_LONG_CONT_STRING:
	       {
		 //System.out.println("DV_xxx_CONT_STRING");
		 String o = (String)obj;
		 write(o.getBytes(),0,o.length());
		 return;
	       }
	 case VirtuosoTypes.DV_SHORT_INT:
	       {
                 if (obj instanceof Boolean)
		   writeint((((Boolean)obj).booleanValue()==true)?1:0);
                 else if (obj instanceof Byte)
		   writeint(((Byte)obj).shortValue());
                 else
		 writeint(((Number)obj).shortValue());
		 return;
	       }
	 case VirtuosoTypes.DV_LONG_INT:
	       {
		 writeint(((Number)obj).longValue());
		 return;
	       }
	 case VirtuosoTypes.DV_INT64:
	       {
		 writelong(((Number)obj).longValue());
		 return;
	       }
	 case VirtuosoTypes.DV_DATETIME:
	 case VirtuosoTypes.DV_TIMESTAMP_OBJ:
	 case VirtuosoTypes.DV_TIMESTAMP:
	 case VirtuosoTypes.DV_TIME:
	 case VirtuosoTypes.DV_DATE:
	       {
		 writeDate(obj,tag);
		 return;
	       }
	 case VirtuosoTypes.BYTEARRAY:
	     throw new VirtuosoException ("invalid type", VirtuosoException.NOTIMPLEMENTED);
         case VirtuosoTypes.DV_BIN:
               {
		 //System.out.println("writing DV_BIN");
                 byte [] bobj = (byte[]) obj;
		 write(VirtuosoTypes.DV_BIN);
                 write(bobj.length);
		 write(bobj, 0, bobj.length);
		 //System.out.println("writing DV_BIN done");
		 return;
	       }
         case VirtuosoTypes.DV_LONG_BIN:
               {
		 //System.out.println("writing DV_LONG_BIN");
                 byte [] bobj = (byte[]) obj;
		 write(VirtuosoTypes.DV_LONG_BIN);
                 writelongint(bobj.length);
		 write(bobj, 0, bobj.length);
		 //System.out.println("writing DV_LONG_BIN done");
		 return;
	       }
	 case VirtuosoTypes.DV_BLOB_BIN:
	       {
		 //System.out.println("DV_BLOB_BIN");
		 write(VirtuosoTypes.DV_BLOB_HANDLE);
		 writelongint(1l);
		 writelongint(((VirtuosoBlob)obj).hashCode());
		 writelongint(((VirtuosoBlob)obj).length());
                 // o12 only
		 writelongint(((VirtuosoBlob)obj).key_id);
		 writelongint(((VirtuosoBlob)obj).frag_no);
		 writelongint(((VirtuosoBlob)obj).dir_page);
		 writelongint(((VirtuosoBlob)obj).bh_timestamp);
		 write_object(((VirtuosoBlob)obj).pages);
		 return;
	       }
	 case VirtuosoTypes.DV_BLOB:
	       {
		 //System.out.println("DV_BLOB");
		 write(VirtuosoTypes.DV_BLOB_HANDLE);
		 writelongint(1l);
		 writelongint(((VirtuosoBlob)obj).hashCode());
		 writelongint(((VirtuosoBlob)obj).length());
                 // o12 only
		 writelongint(((VirtuosoBlob)obj).key_id);
		 writelongint(((VirtuosoBlob)obj).frag_no);
		 writelongint(((VirtuosoBlob)obj).dir_page);
		 writelongint(((VirtuosoBlob)obj).bh_timestamp);
		 write_object(((VirtuosoBlob)obj).pages);
		 return;
	       }
	 case VirtuosoTypes.DV_NUMERIC:
	       {
		 //System.out.println("DV_NUMERIC");
                 if (obj instanceof Long)
                   writenumeric (BigDecimal.valueOf (((Long)obj).longValue()));
                 else
		   writenumeric((BigDecimal)obj);
		 return;
	       }
	 case VirtuosoTypes.DV_BOX_FLAGS:
	       {
		 VirtuosoExtendedString o = (VirtuosoExtendedString) obj;
		 write (tag);
		 writelongint (o.strType);
		 write_object (o.str);
		 return;
	       }
	 case VirtuosoTypes.DV_RDF:
	       {
		   writeRdfBox ((VirtuosoRdfBox) obj);
		   return;
	       }
	 default:
	     // Problem !
	     //System.err.println("Tag not defined : "+tag + "object=[" + obj.toString() + "]");
	     if (obj instanceof Serializable)
	       {
		 writeobject (obj);
	       }
	     else
	       throw new VirtuosoException(
		   "Tag " + tag +
		   " not defined and the object " +
		   obj.getClass().getName() +
		   "is not serializable.",
		   VirtuosoException.BADTAG);
       }
   }

   /**
    * Method to send a float value depending DV_SINGLE_FLOAT type.
    *
    * @param float Value to send.
    * @exception	java.io.IOException
    */
   private void writefloat(float n) throws IOException
   {
      //System.out.println("DV_SINGLE_FLOAT");
      write(VirtuosoTypes.DV_SINGLE_FLOAT);
      writerawfloat(n);
   }

   /**
    * Method to send a float value with its IEEE representation.
    *
    * @param int IEEE value.
    * @exception	java.io.IOException
    */
   private void writerawfloat(float m) throws IOException
   {
      writelongint(Float.floatToIntBits(m));
   }

   /**
    * Method to send a double value depending DV_DOUBLE_FLOAT type.
    *
    * @param double Value to send.
    * @exception	java.io.IOException
    */
   private void writedouble(double n) throws IOException
   {
      //System.out.println("DV_DOUBLE_FLOAT");
      write(VirtuosoTypes.DV_DOUBLE_FLOAT);
      writerawdouble(n);
   }

   /**
    * Method to send a double value with its IEEE representation.
    *
    * @param long IEEE value.
    * @exception	java.io.IOException
    */
   private void writerawdouble(double m) throws IOException
   {
      writerawlong(Double.doubleToLongBits(m));
   }

   /**
    * Method to send an int value depending DV_xxx_INT type.
    *
    * @param long	Value to send.
    * @exception	java.io.IOException
    */
   private void writeint(long n) throws IOException
   {
      if((n >= -128) && (n < 128))
      {
         //System.out.println("DV_SHORT_INT");  System.out.println((byte)n);
         write(VirtuosoTypes.DV_SHORT_INT);
         write((byte)n);
      }
      else
      {
         //System.out.println("DV_LONG_INT");
         write(VirtuosoTypes.DV_LONG_INT);
         writelongint(n);
      }
   }

   /**
    * Method to send a long value depending DV_xxx_INT type.
    *
    * @param long	Value to send.
    * @exception	java.io.IOException
    */
   protected void writelongint(long data) throws IOException
   {
     tmp[0] = ((byte) ((data >> 24) & 0xFF));
     tmp[1] = ((byte) ((data >> 16) & 0xFF));
     tmp[2] = ((byte) ((data >> 8) & 0xFF));
     tmp[3] = ((byte) (data & 0xFF));
     write(tmp, 0, 4);
   }

   /**
    * Method to send a long value depending DV_xxx_INT type.
    *
    * @param long	Value to send.
    * @exception	java.io.IOException
    */
   protected void writelongint(int data) throws IOException
   {
     tmp[0] = ((byte) ((data >> 24) & 0xFF));
     tmp[1] = ((byte) ((data >> 16) & 0xFF));
     tmp[2] = ((byte) ((data >> 8) & 0xFF));
     tmp[3] = ((byte) (data & 0xFF));
     write(tmp, 0, 4);
   }

   protected void writeshort (short data) throws IOException
   {
     tmp[0] = ((byte) ((data >> 8) & 0xFF));
     tmp[1] = ((byte) (data & 0xFF));
     write(tmp, 0, 2);
   }


   protected void writelong(long data) throws IOException
   {
     write(VirtuosoTypes.DV_INT64);
     writerawlong(data);
   }

   protected void writerawlong(long data) throws IOException
   {
     tmp[0] = ((byte) ((data >> 56) & 0xFF));
     tmp[1] = ((byte) ((data >> 48) & 0xFF));
     tmp[2] = ((byte) ((data >> 40) & 0xFF));
     tmp[3] = ((byte) ((data >> 32) & 0xFF));
     tmp[4] = ((byte) ((data >> 24) & 0xFF));
     tmp[5] = ((byte) ((data >> 16) & 0xFF));
     tmp[6] = ((byte) ((data >> 8) & 0xFF));
     tmp[7] = ((byte) (data & 0xFF));
     write(tmp, 0, 8);
   }


   /**
    * Method to send a numeric value depending DV_NUMERIC type.
    *
    * @param BigDecimal The numeric value to send.
    * @exception	java.io.IOException
    */
   private void writenumeric(BigDecimal bd) throws IOException, VirtuosoException
   {
      write(VirtuosoTypes.DV_NUMERIC);
      // Check if its a specific value
      if(bd.doubleValue() == Double.NaN)
      {
         write(3);
         write(0x8);
         write(0);
         write(VirtuosoTypes.DV_NULL);
         return;
      }
      else
         if(bd.doubleValue() == Double.NEGATIVE_INFINITY)
         {
            write(3);
            write(0x11);
            write(0);
            write(VirtuosoTypes.DV_NULL);
            return;
         }
         else
            if(bd.doubleValue() == Double.POSITIVE_INFINITY)
            {
               write(3);
               write(0x10);
               write(0);
               write(VirtuosoTypes.DV_NULL);
               return;
            }
      // Here, it's a pure Big Decimal ....
      int flags = ((bd.signum() == -1) ? 1 : 0);
      if(bd.signum() == -1)
         bd = bd.negate();
      String bcd = bd.toString();
      int len = bcd.length();
      if (len - 1 > 40)
	throw new VirtuosoException ("Numeric " + bcd + " too large to transfer to Virtuoso as numeric",
		VirtuosoException.BADPARAM);
      int numbefdot = bcd.indexOf('.');
      int i = 0;
      // Check the number of digits before the dot ...
      if(numbefdot < 0)
         numbefdot = len;
      if((numbefdot & 0x1) == 0x1)
         flags |= 0x4;
      // Check the number of digits after the dot
      int numaftdot = len - numbefdot - 1;
      if(numaftdot < 0)
         numaftdot = 0;
      if((numaftdot & 0x1) == 0x1)
         flags |= 0x2;
      // Write flags and number before dot
      write(((len + (((flags & 0x2) == 0x2) ? 1 : 0) + (((flags & 0x4) == 0x4) ? 1 : 0)) >> 1) + 2);
      write(flags);
      write((numbefdot + (((flags & 0x4) == 0x4) ? 1 : 0)) >> 1);
      // Begin to write the bcd string
      if((flags & 0x04) == 0x04)
         write(bcd.charAt(i++) - '0');
      for(;i < numbefdot;i += 2)
         write(((bcd.charAt(i) - '0') << 4) | (bcd.charAt(i + 1) - '0'));
      if(numaftdot == 0)
         return;
      for(i++;i <= numaftdot + numbefdot - (((flags & 0x2) == 0x2)?1:0);i += 2)
         write(((bcd.charAt(i) - '0') << 4) | (bcd.charAt(i + 1) - '0'));
      if((flags & 0x02) == 0x02)
         write((bcd.charAt(i++) - '0') << 4);
//      for(i++;i <= numaftdot + numbefdot;i += 2)
//         write(((bcd.charAt(i) - '0') << 4) | ((((flags & 0x2) == 0x2) && (i + 1) >= bcd.length()) ? 0 : (bcd.charAt(i + 1) - '0')));

   }

   private void writeRdfBox (VirtuosoRdfBox rb) throws IOException, VirtuosoException
   {
      int flags = 0;

      write (VirtuosoTypes.DV_RDF);

      if (rb.rb_ro_id != 0)
	flags |= VirtuosoRdfBox.RBS_OUTLINED;
      if (rb.rb_ro_id > 0xffffffffL)
	flags |= VirtuosoRdfBox.RBS_64;
      if (VirtuosoRdfBox.RDF_BOX_DEFAULT_LANG != rb.rb_lang)
	flags |= VirtuosoRdfBox.RBS_HAS_LANG;
      if (VirtuosoRdfBox.RDF_BOX_DEFAULT_TYPE != rb.rb_type)
	flags |= VirtuosoRdfBox.RBS_HAS_TYPE;
      if (rb.rb_is_complete)
	flags |= VirtuosoRdfBox.RBS_COMPLETE;

      write (flags);
      write_object (rb.rb_box);
      if (rb.rb_ro_id != 0)
      {
	if (rb.rb_ro_id > 0xffffffffL)
	{
	  writerawlong(rb.rb_ro_id);
	}
	else
	{
	  writelongint (rb.rb_ro_id);
	}
      }
      if (VirtuosoRdfBox.RDF_BOX_DEFAULT_TYPE != rb.rb_type)
	writeshort (rb.rb_type);
      if (VirtuosoRdfBox.RDF_BOX_DEFAULT_LANG != rb.rb_lang)
	writeshort (rb.rb_lang);
   }

   private void writeobject(Object obj) throws IOException
   {
     ByteArrayOutputStream bos = new ByteArrayOutputStream ();
     ObjectOutputStream oos = new ObjectOutputStream (bos);
     oos.writeObject (obj);
     oos.flush();

     byte[] bytes = bos.toByteArray();
     oos = null;
     bos = null;

     write (VirtuosoTypes.DV_OBJECT);
     writelongint (VirtuosoTypes.UDT_JAVA_CLIENT_OBJECT_ID);
     if (bytes.length > 255)
       {
	 write (VirtuosoTypes.DV_LONG_BIN);
	 writelongint (bytes.length);
       }
     else
       {
	 write (VirtuosoTypes.DV_BIN);
	 write (bytes.length);
       }
     write (bytes, 0, bytes.length);
     bytes = null;
   }
   /**
    * Method to send a string depending DV_xxx_STRING type.
    *
    * @param String	Value to send.
    * @exception	java.io.IOException
    */
   private void writestring(String string) throws IOException
   {
     int len = string.length();
     byte[] bytes = new byte [len];
     for (int i = 0; i < len; i++)
       bytes[i] = (byte)string.charAt(i);

     if(len < 256)
      {
         //System.out.println("DV_SHORT_STRING_SERIAL");	System.out.println(string.length());
         write(VirtuosoTypes.DV_SHORT_STRING_SERIAL);
         write(len);
      }
     else
      {
         //System.out.println("DV_STRING");	System.out.println(string.length());
         write(VirtuosoTypes.DV_STRING);
         writelongint(len);
      }
      //System.out.println(string.toString());
     write(bytes, 0, len);
   }

   /**
    * Method uses send a date or timestamp.
    *
    * @param obj  The object to send.
    * @param tag	Value to send.
    * @exception	java.io.IOException
    */
   private void writeDate(Object obj, int tag) throws IOException
   {
      int temp, tz;
      int _year, _month, _day, _hour, _minute, _second, _frac, yday;
      java.util.Date date = (java.util.Date)obj;
      //System.out.println("DV_DATE");
      GregorianCalendar cal = new GregorianCalendar();
      cal.setTime((java.util.Date)obj);

      if (!(obj instanceof java.sql.Time))
	{
	  _year = cal.get (Calendar.YEAR);
	  _month = cal.get (Calendar.MONTH) + 1;
	  _day = cal.get (Calendar.DAY_OF_MONTH);
	  yday = date2num(_year, _month, _day);
	}
      else
	yday = _year = _month = _day = 0;

      if (!(obj instanceof java.sql.Date))
	{
	  _hour = cal.get(Calendar.HOUR_OF_DAY);
	  _minute = cal.get (Calendar.MINUTE);
	  _second = cal.get (Calendar.SECOND);
	  if (obj instanceof java.sql.Timestamp)
	    _frac = ((Timestamp)obj).getNanos();
	  else
	    _frac = 0;
	}
      else
	{
	  _hour = _minute = _second = _frac = 0;
	}
      tz = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
      tz = tz / (60*1000);
      //System.err.println ("write: type=" + obj.getClass().getName() + " <date>=" +
      //	  _year + "-" + _month + "-" + _day + " " + _hour + ":" + _minute + "." + _second + ":" + _frac +
      //	  " tz=" + tz);
      if(tz != 0)
	{
	  int day = yday;
	  int sec = 0;
	  sec = VirtuosoInputStream.time_to_sec (0, _hour, _minute, _second);
	  sec -= 60 * tz;
	  if (sec < 0)
	    {
	      day = day - (1 + ((-sec) / VirtuosoInputStream.SPERDAY));

	      sec = sec % VirtuosoInputStream.SPERDAY;

	      if (sec == 0)
		day++;

	      sec = VirtuosoInputStream.SPERDAY + sec;
	    }
	  else
	    {
	      day = day + sec / VirtuosoInputStream.SPERDAY;
	      sec = sec % VirtuosoInputStream.SPERDAY;
	    }
	  int dummy_day = sec / VirtuosoInputStream.SPERDAY;

	  _hour = (sec - (dummy_day * VirtuosoInputStream.SPERDAY)) / (60 * 60);
	  _minute = (sec - (dummy_day * VirtuosoInputStream.SPERDAY) - (_hour * 60 * 60)) / 60;
	  _second = sec % 60;
	  yday = day;
	}

      // Send date or timestamp object
      write(VirtuosoTypes.DV_DATETIME);
      // Send the number of days
      write(yday >> 16);
      write(yday >> 8);
      write(yday);
      // Send the number of hour
      write(_hour);
      // Send the number of minute
      yday = _second;
      write(((_minute << 2) & 0xfc) | ((yday >> 4) & 0x3));
//--      temp = _frac / 1000;
      temp = _frac;
      write((yday << 4) | ((temp >> 16) & 0xf));
      // Send the fraction if it's a time stamp
      write(temp >> 8);
      write(temp);
      // Send the time zone
      //System.out.println ("tz is " + tz);
      if (tz < 0)
	tz = 65536 + tz;
      //System.out.println ("tz is now : " + tz);
      int tz_bytes[] = new int[2];
      tz_bytes[0] = (tz >> 8);
      tz_bytes[1] = tz & 0xFF;
      byte type = (byte) VirtuosoTypes.DT_TYPE_DATETIME;
      if (obj instanceof java.sql.Time)
	type = (byte) VirtuosoTypes.DT_TYPE_TIME;
      if (obj instanceof java.sql.Date)
	type = (byte) VirtuosoTypes.DT_TYPE_DATE;
      //System.out.println ("Type is " + type);
      //System.out.println ("bytes=" + MD5.asHex (tz_bytes));
      //System.out.println ("bytes[0] is " + ((int)tz_bytes[0]));
      //System.out.println ("bytes[1] is " + ((int)tz_bytes[1]));
      tz_bytes[0] &= 0x07;
      tz_bytes[0] |= (type << 5);
      //System.out.println ("bytes=" + MD5.asHex (tz_bytes));
      //System.out.println ("bytes[0] is " + ((int)tz_bytes[0]));
      //System.out.println ("bytes[1] is " + ((int)tz_bytes[1]));

      write(tz_bytes[0]);
      write(tz_bytes[1]);
   }

   static final int cumdays_in_month[] = {  0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
   public static int date2num(int year, int month, int day)
   {
      int julian_days = (year - 1) * 365 + ((year - 1) >> 2);
      if(year > 1582 ||
	  ((year == 1582)
	   && (month > 10 ||
	     ((month == 10)
	      && (day > 14)))))
         julian_days -= 10;
      if(year > 1582)
      {
         julian_days += (((year - 1) / 400) - (1582 / 400));
         julian_days -= (((year - 1) / 100) - (1582 / 100));
         if(((1582 % 100) == 0) && ((1582 % 400) != 0))
            julian_days--;
      }
      julian_days += cumdays_in_month[month - 1];
      julian_days += day;
      if(days_in_february(year) == 29 && month > 2)
         julian_days++;
      return julian_days;
   }

   public static int days_in_february(int year)
   {
      int day;
      if((year > 1582) ||
	  ((year == 1582)
	   &&
	   (10 == 1 ||
	    ((10 == 2)
	     && (14 >= 28)))))
         day = ((year & 3) != 0) ? 28 : (((year % 100) == 0 && (year % 400) != 0) ? 28 : 29);
      else
         day = ((year & 3) != 0) ? 28 : 29;
      if(year == 4)
         day--;
      return day;
   }

   /**
    * Method uses to determine which is the DV tag for the object.
    *
    * @param Object	Object to scan.
    * @return byte	The corresponding tag.
    */
   private int whatIs(Object obj)
   {
      // Treat the NULL case
      if(obj == null)
	return VirtuosoTypes.DV_NULL;
      if(obj instanceof VirtuosoNullParameter)
         return VirtuosoTypes.DV_DB_NULL;
      // Treat Arrays
      if(obj instanceof VectorOfLong)
         return VirtuosoTypes.DV_ARRAY_OF_LONG;
      if(obj instanceof VectorOfDouble)
         return VirtuosoTypes.DV_ARRAY_OF_DOUBLE;
      if(obj instanceof VectorOfFloat)
         return VirtuosoTypes.DV_ARRAY_OF_FLOAT;
#if JDK_VER >= 12
      if(obj instanceof LinkedList)
         return VirtuosoTypes.DV_LIST_OF_POINTER;
#endif
      if(obj instanceof openlink.util.Vector)
         return VirtuosoTypes.DV_ARRAY_OF_POINTER;
      // Treat Short object
      if(obj instanceof Short)
         return VirtuosoTypes.DV_SHORT_INT;
      // Treat boolean object
      if(obj instanceof Boolean)
         return VirtuosoTypes.DV_SHORT_INT;
      // Treat byte object
      if(obj instanceof Byte)
         return VirtuosoTypes.DV_SHORT_INT;
      // Treat Long object
      if(obj instanceof Long)
      {
         Long o = (Long)obj;
         if (o.longValue () >= -128 && o.longValue() < 128)
           return VirtuosoTypes.DV_SHORT_INT;
         else if (o.longValue () >= (long) Integer.MIN_VALUE && o.longValue() <= (long) Integer.MAX_VALUE)
	   return VirtuosoTypes.DV_LONG_INT;
         else
           return VirtuosoTypes.DV_INT64;
      }
      // Treat Integer object
      if(obj instanceof Integer)
      {
         Integer o = (Integer)obj;
         return ((o.longValue() >= -128) && (o.longValue() < 128)) ? VirtuosoTypes.DV_SHORT_INT : VirtuosoTypes.DV_LONG_INT;
      }
      // Treat Float object
      if(obj instanceof Float)
         return VirtuosoTypes.DV_SINGLE_FLOAT;
      // Treat Double object
      if(obj instanceof Double)
         return VirtuosoTypes.DV_DOUBLE_FLOAT;
      // Treat String object
      if(obj instanceof String)
      {
         String o = (String)obj;
         return (o.length() < 256) ? VirtuosoTypes.DV_SHORT_STRING_SERIAL : VirtuosoTypes.DV_STRING;
      }
      // Treat Numeric type
      if(obj instanceof BigDecimal)
         return VirtuosoTypes.DV_NUMERIC;
      // Treat different kind of arrays
      if(obj instanceof byte[])
         return ((byte [])obj).length < 256 ? VirtuosoTypes.DV_BIN : VirtuosoTypes.DV_LONG_BIN;
      // Treat Date and timestamps
      if(obj instanceof java.sql.Date)
         return VirtuosoTypes.DV_DATE;
      if(obj instanceof java.sql.Time)
         return VirtuosoTypes.DV_TIME;
      if(obj instanceof java.sql.Timestamp)
         return VirtuosoTypes.DV_TIMESTAMP;
      if(obj instanceof java.util.Date)
         return VirtuosoTypes.DV_DATETIME;
      // Treat blob objects
#if JDK_VER >= 12
      if(obj instanceof Clob)
#else
      if(obj instanceof VirtuosoClob)
#endif
         return VirtuosoTypes.DV_BLOB;
#if JDK_VER >= 12
      if(obj instanceof Blob)
#else
      if(obj instanceof VirtuosoBlob)
#endif
         return VirtuosoTypes.DV_BLOB_BIN;
      if (obj instanceof VirtuosoExplicitString)
	{
	  VirtuosoExplicitString sobj = (VirtuosoExplicitString)obj;
	  //System.err.println ("OUT: ExpObj=" + sobj.toString());
	  return ((VirtuosoExplicitString)obj).getDtp();
	}
      if (obj instanceof VirtuosoExtendedString)
	return VirtuosoTypes.DV_BOX_FLAGS;
      if (obj instanceof VirtuosoRdfBox)
	return VirtuosoTypes.DV_RDF;
      return 0;
   }

}