-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathHessian2Output.java
1537 lines (1290 loc) · 39.4 KB
/
Hessian2Output.java
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
/*
* Copyright (c) 2001-2008 Caucho Technology, Inc. All rights reserved.
*
* The Apache Software License, Version 1.1
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Caucho Technology (http://www.caucho.com/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "Burlap", "Resin", and "Caucho" must not be used to
* endorse or promote products derived from this software without prior
* written permission. For written permission, please contact
* info@caucho.com.
*
* 5. Products derived from this software may not be called "Resin"
* nor may "Resin" appear in their names without prior written
* permission of Caucho Technology.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Scott Ferguson
*/
package com.alibaba.com.caucho.hessian.io;
import com.alibaba.com.caucho.hessian.util.IdentityIntMap;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
/**
* Output stream for Hessian 2 requests.
* <p>
* <p>Since HessianOutput does not depend on any classes other than
* in the JDK, it can be extracted independently into a smaller package.
* <p>
* <p>HessianOutput is unbuffered, so any client needs to provide
* its own buffering.
* <p>
* <pre>
* OutputStream os = ...; // from http connection
* Hessian2Output out = new Hessian2Output(os);
* String value;
*
* out.startCall("hello", 1); // start hello call
* out.writeString("arg1"); // write a string argument
* out.completeCall(); // complete the call
* </pre>
*/
public class Hessian2Output
extends AbstractHessianOutput
implements Hessian2Constants {
public final static int SIZE = 4096;
private final byte[] _buffer = new byte[SIZE];
// the output stream/
protected OutputStream _os;
// map of references
private IdentityIntMap _refs = new IdentityIntMap();
private boolean _isCloseStreamOnClose;
// map of classes
private HashMap _classRefs;
// map of types
private HashMap _typeRefs;
private int _offset;
private boolean _isStreaming;
/**
* Creates a new Hessian output stream, initialized with an
* underlying output stream.
*
* @param os the underlying output stream.
*/
public Hessian2Output(OutputStream os) {
_os = os;
}
public boolean isCloseStreamOnClose() {
return _isCloseStreamOnClose;
}
public void setCloseStreamOnClose(boolean isClose) {
_isCloseStreamOnClose = isClose;
}
/**
* Writes a complete method call.
*/
@Override
public void call(String method, Object[] args)
throws IOException {
int length = args != null ? args.length : 0;
startCall(method, length);
for (int i = 0; i < args.length; i++)
writeObject(args[i]);
completeCall();
}
/**
* Starts the method call. Clients would use <code>startCall</code>
* instead of <code>call</code> if they wanted finer control over
* writing the arguments, or needed to write headers.
* <p>
* <code><pre>
* C
* string # method name
* int # arg count
* </pre></code>
*
* @param method the method name to call.
*/
@Override
public void startCall(String method, int length)
throws IOException {
int offset = _offset;
if (SIZE < offset + 32) {
flush();
offset = _offset;
}
byte[] buffer = _buffer;
buffer[_offset++] = (byte) 'C';
writeString(method);
writeInt(length);
}
/**
* Writes the call tag. This would be followed by the
* method and the arguments
* <p>
* <code><pre>
* C
* </pre></code>
*
* @param method the method name to call.
*/
@Override
public void startCall()
throws IOException {
flushIfFull();
_buffer[_offset++] = (byte) 'C';
}
/**
* Starts an envelope.
* <p>
* <code><pre>
* E major minor
* m b16 b8 method-name
* </pre></code>
*
* @param method the method name to call.
*/
public void startEnvelope(String method)
throws IOException {
int offset = _offset;
if (SIZE < offset + 32) {
flush();
offset = _offset;
}
_buffer[_offset++] = (byte) 'E';
writeString(method);
}
/**
* Completes an envelope.
* <p>
* <p>A successful completion will have a single value:
* <p>
* <pre>
* Z
* </pre>
*/
public void completeEnvelope()
throws IOException {
flushIfFull();
_buffer[_offset++] = (byte) 'Z';
}
/**
* Writes the method tag.
* <p>
* <code><pre>
* string
* </pre></code>
*
* @param method the method name to call.
*/
@Override
public void writeMethod(String method)
throws IOException {
writeString(method);
}
/**
* Completes.
* <p>
* <code><pre>
* z
* </pre></code>
*/
@Override
public void completeCall()
throws IOException {
/*
flushIfFull();
_buffer[_offset++] = (byte) 'Z';
*/
}
/**
* Starts the reply
* <p>
* <p>A successful completion will have a single value:
* <p>
* <pre>
* R
* </pre>
*/
@Override
public void startReply()
throws IOException {
writeVersion();
flushIfFull();
_buffer[_offset++] = (byte) 'R';
}
public void writeVersion()
throws IOException {
flushIfFull();
_buffer[_offset++] = (byte) 'H';
_buffer[_offset++] = (byte) 2;
_buffer[_offset++] = (byte) 0;
}
/**
* Completes reading the reply
* <p>
* <p>A successful completion will have a single value:
* <p>
* <pre>
* z
* </pre>
*/
@Override
public void completeReply()
throws IOException {
}
/**
* Starts a packet
* <p>
* <p>A message contains several objects encapsulated by a length</p>
* <p>
* <pre>
* p x02 x00
* </pre>
*/
public void startMessage()
throws IOException {
flushIfFull();
_buffer[_offset++] = (byte) 'p';
_buffer[_offset++] = (byte) 2;
_buffer[_offset++] = (byte) 0;
}
/**
* Completes reading the message
* <p>
* <p>A successful completion will have a single value:
* <p>
* <pre>
* z
* </pre>
*/
public void completeMessage()
throws IOException {
flushIfFull();
_buffer[_offset++] = (byte) 'z';
}
/**
* Writes a fault. The fault will be written
* as a descriptive string followed by an object:
* <p>
* <code><pre>
* F map
* </pre></code>
* <p>
* <code><pre>
* F H
* \x04code
* \x10the fault code
* <p>
* \x07message
* \x11the fault message
* <p>
* \x06detail
* M\xnnjavax.ejb.FinderException
* ...
* Z
* Z
* </pre></code>
*
* @param code the fault code, a three digit
*/
@Override
public void writeFault(String code, String message, Object detail)
throws IOException {
flushIfFull();
writeVersion();
_buffer[_offset++] = (byte) 'F';
_buffer[_offset++] = (byte) 'H';
_refs.put(new HashMap(), _refs.size());
writeString("code");
writeString(code);
writeString("message");
writeString(message);
if (detail != null) {
writeString("detail");
writeObject(detail);
}
flushIfFull();
_buffer[_offset++] = (byte) 'Z';
}
/**
* Writes any object to the output stream.
*/
@Override
public void writeObject(Object object)
throws IOException {
if (object == null) {
writeNull();
return;
}
Serializer serializer;
serializer = findSerializerFactory().getSerializer(object.getClass());
serializer.writeObject(object, this);
}
/**
* Writes the list header to the stream. List writers will call
* <code>writeListBegin</code> followed by the list contents and then
* call <code>writeListEnd</code>.
* <p>
* <code><pre>
* list ::= V type value* Z
* ::= v type int value*
* </pre></code>
*
* @return true for variable lists, false for fixed lists
*/
@Override
public boolean writeListBegin(int length, String type)
throws IOException {
flushIfFull();
if (length < 0) {
if (type != null) {
_buffer[_offset++] = (byte) BC_LIST_VARIABLE;
writeType(type);
} else
_buffer[_offset++] = (byte) BC_LIST_VARIABLE_UNTYPED;
return true;
} else if (length <= LIST_DIRECT_MAX) {
if (type != null) {
_buffer[_offset++] = (byte) (BC_LIST_DIRECT + length);
writeType(type);
} else {
_buffer[_offset++] = (byte) (BC_LIST_DIRECT_UNTYPED + length);
}
return false;
} else {
if (type != null) {
_buffer[_offset++] = (byte) BC_LIST_FIXED;
writeType(type);
} else {
_buffer[_offset++] = (byte) BC_LIST_FIXED_UNTYPED;
}
writeInt(length);
return false;
}
}
/**
* Writes the tail of the list to the stream for a variable-length list.
*/
@Override
public void writeListEnd()
throws IOException {
flushIfFull();
_buffer[_offset++] = (byte) BC_END;
}
/**
* Writes the map header to the stream. Map writers will call
* <code>writeMapBegin</code> followed by the map contents and then
* call <code>writeMapEnd</code>.
* <p>
* <code><pre>
* map ::= M type (<value> <value>)* Z
* ::= H (<value> <value>)* Z
* </pre></code>
*/
@Override
public void writeMapBegin(String type)
throws IOException {
if (SIZE < _offset + 32)
flush();
if (type != null) {
_buffer[_offset++] = BC_MAP;
writeType(type);
} else
_buffer[_offset++] = BC_MAP_UNTYPED;
}
/**
* Writes the tail of the map to the stream.
*/
@Override
public void writeMapEnd()
throws IOException {
if (SIZE < _offset + 32)
flush();
_buffer[_offset++] = (byte) BC_END;
}
/**
* Writes the object definition
* <p>
* <code><pre>
* C <string> <int> <string>*
* </pre></code>
*/
@Override
public int writeObjectBegin(String type)
throws IOException {
if (_classRefs == null)
_classRefs = new HashMap();
Integer refV = (Integer) _classRefs.get(type);
if (refV != null) {
int ref = refV.intValue();
if (SIZE < _offset + 32)
flush();
if (ref <= OBJECT_DIRECT_MAX) {
_buffer[_offset++] = (byte) (BC_OBJECT_DIRECT + ref);
} else {
_buffer[_offset++] = (byte) 'O';
writeInt(ref);
}
return ref;
} else {
int ref = _classRefs.size();
_classRefs.put(type, Integer.valueOf(ref));
if (SIZE < _offset + 32)
flush();
_buffer[_offset++] = (byte) 'C';
writeString(type);
return -1;
}
}
/**
* Writes the tail of the class definition to the stream.
*/
@Override
public void writeClassFieldLength(int len)
throws IOException {
writeInt(len);
}
/**
* Writes the tail of the object definition to the stream.
*/
@Override
public void writeObjectEnd()
throws IOException {
}
/**
* <code><pre>
* type ::= string
* ::= int
* </code></pre>
*/
private void writeType(String type)
throws IOException {
flushIfFull();
int len = type.length();
if (len == 0) {
throw new IllegalArgumentException("empty type is not allowed");
}
if (_typeRefs == null)
_typeRefs = new HashMap();
Integer typeRefV = (Integer) _typeRefs.get(type);
if (typeRefV != null) {
int typeRef = typeRefV.intValue();
writeInt(typeRef);
} else {
_typeRefs.put(type, Integer.valueOf(_typeRefs.size()));
writeString(type);
}
}
/**
* Writes a boolean value to the stream. The boolean will be written
* with the following syntax:
* <p>
* <code><pre>
* T
* F
* </pre></code>
*
* @param value the boolean value to write.
*/
@Override
public void writeBoolean(boolean value)
throws IOException {
if (SIZE < _offset + 16)
flush();
if (value)
_buffer[_offset++] = (byte) 'T';
else
_buffer[_offset++] = (byte) 'F';
}
/**
* Writes an integer value to the stream. The integer will be written
* with the following syntax:
* <p>
* <code><pre>
* I b32 b24 b16 b8
* </pre></code>
*
* @param value the integer value to write.
*/
@Override
public void writeInt(int value)
throws IOException {
int offset = _offset;
byte[] buffer = _buffer;
if (SIZE <= offset + 16) {
flush();
offset = _offset;
}
if (INT_DIRECT_MIN <= value && value <= INT_DIRECT_MAX)
buffer[offset++] = (byte) (value + BC_INT_ZERO);
else if (INT_BYTE_MIN <= value && value <= INT_BYTE_MAX) {
buffer[offset++] = (byte) (BC_INT_BYTE_ZERO + (value >> 8));
buffer[offset++] = (byte) (value);
} else if (INT_SHORT_MIN <= value && value <= INT_SHORT_MAX) {
buffer[offset++] = (byte) (BC_INT_SHORT_ZERO + (value >> 16));
buffer[offset++] = (byte) (value >> 8);
buffer[offset++] = (byte) (value);
} else {
buffer[offset++] = (byte) ('I');
buffer[offset++] = (byte) (value >> 24);
buffer[offset++] = (byte) (value >> 16);
buffer[offset++] = (byte) (value >> 8);
buffer[offset++] = (byte) (value);
}
_offset = offset;
}
/**
* Writes a long value to the stream. The long will be written
* with the following syntax:
* <p>
* <code><pre>
* L b64 b56 b48 b40 b32 b24 b16 b8
* </pre></code>
*
* @param value the long value to write.
*/
@Override
public void writeLong(long value)
throws IOException {
int offset = _offset;
byte[] buffer = _buffer;
if (SIZE <= offset + 16) {
flush();
offset = _offset;
}
if (LONG_DIRECT_MIN <= value && value <= LONG_DIRECT_MAX) {
buffer[offset++] = (byte) (value + BC_LONG_ZERO);
} else if (LONG_BYTE_MIN <= value && value <= LONG_BYTE_MAX) {
buffer[offset++] = (byte) (BC_LONG_BYTE_ZERO + (value >> 8));
buffer[offset++] = (byte) (value);
} else if (LONG_SHORT_MIN <= value && value <= LONG_SHORT_MAX) {
buffer[offset++] = (byte) (BC_LONG_SHORT_ZERO + (value >> 16));
buffer[offset++] = (byte) (value >> 8);
buffer[offset++] = (byte) (value);
} else if (-0x80000000L <= value && value <= 0x7fffffffL) {
buffer[offset + 0] = (byte) BC_LONG_INT;
buffer[offset + 1] = (byte) (value >> 24);
buffer[offset + 2] = (byte) (value >> 16);
buffer[offset + 3] = (byte) (value >> 8);
buffer[offset + 4] = (byte) (value);
offset += 5;
} else {
buffer[offset + 0] = (byte) 'L';
buffer[offset + 1] = (byte) (value >> 56);
buffer[offset + 2] = (byte) (value >> 48);
buffer[offset + 3] = (byte) (value >> 40);
buffer[offset + 4] = (byte) (value >> 32);
buffer[offset + 5] = (byte) (value >> 24);
buffer[offset + 6] = (byte) (value >> 16);
buffer[offset + 7] = (byte) (value >> 8);
buffer[offset + 8] = (byte) (value);
offset += 9;
}
_offset = offset;
}
/**
* Writes a double value to the stream. The double will be written
* with the following syntax:
* <p>
* <code><pre>
* D b64 b56 b48 b40 b32 b24 b16 b8
* </pre></code>
*
* @param value the double value to write.
*/
@Override
public void writeDouble(double value)
throws IOException {
int offset = _offset;
byte[] buffer = _buffer;
if (SIZE <= offset + 16) {
flush();
offset = _offset;
}
int intValue = (int) value;
if (intValue == value) {
if (intValue == 0) {
buffer[offset++] = (byte) BC_DOUBLE_ZERO;
_offset = offset;
return;
} else if (intValue == 1) {
buffer[offset++] = (byte) BC_DOUBLE_ONE;
_offset = offset;
return;
} else if (-0x80 <= intValue && intValue < 0x80) {
buffer[offset++] = (byte) BC_DOUBLE_BYTE;
buffer[offset++] = (byte) intValue;
_offset = offset;
return;
} else if (-0x8000 <= intValue && intValue < 0x8000) {
buffer[offset + 0] = (byte) BC_DOUBLE_SHORT;
buffer[offset + 1] = (byte) (intValue >> 8);
buffer[offset + 2] = (byte) intValue;
_offset = offset + 3;
return;
}
}
int mills = (int) (value * 1000);
if (0.001 * mills == value) {
buffer[offset + 0] = (byte) (BC_DOUBLE_MILL);
buffer[offset + 1] = (byte) (mills >> 24);
buffer[offset + 2] = (byte) (mills >> 16);
buffer[offset + 3] = (byte) (mills >> 8);
buffer[offset + 4] = (byte) (mills);
_offset = offset + 5;
return;
}
long bits = Double.doubleToLongBits(value);
buffer[offset + 0] = (byte) 'D';
buffer[offset + 1] = (byte) (bits >> 56);
buffer[offset + 2] = (byte) (bits >> 48);
buffer[offset + 3] = (byte) (bits >> 40);
buffer[offset + 4] = (byte) (bits >> 32);
buffer[offset + 5] = (byte) (bits >> 24);
buffer[offset + 6] = (byte) (bits >> 16);
buffer[offset + 7] = (byte) (bits >> 8);
buffer[offset + 8] = (byte) (bits);
_offset = offset + 9;
}
/**
* Writes a date to the stream.
* <p>
* <code><pre>
* date ::= d b7 b6 b5 b4 b3 b2 b1 b0
* ::= x65 b3 b2 b1 b0
* </pre></code>
*
* @param time the date in milliseconds from the epoch in UTC
*/
@Override
public void writeUTCDate(long time)
throws IOException {
if (SIZE < _offset + 32)
flush();
int offset = _offset;
byte[] buffer = _buffer;
if (time % 60000L == 0) {
// compact date ::= x65 b3 b2 b1 b0
long minutes = time / 60000L;
if ((minutes >> 31) == 0 || (minutes >> 31) == -1) {
buffer[offset++] = (byte) BC_DATE_MINUTE;
buffer[offset++] = ((byte) (minutes >> 24));
buffer[offset++] = ((byte) (minutes >> 16));
buffer[offset++] = ((byte) (minutes >> 8));
buffer[offset++] = ((byte) (minutes >> 0));
_offset = offset;
return;
}
}
buffer[offset++] = (byte) BC_DATE;
buffer[offset++] = ((byte) (time >> 56));
buffer[offset++] = ((byte) (time >> 48));
buffer[offset++] = ((byte) (time >> 40));
buffer[offset++] = ((byte) (time >> 32));
buffer[offset++] = ((byte) (time >> 24));
buffer[offset++] = ((byte) (time >> 16));
buffer[offset++] = ((byte) (time >> 8));
buffer[offset++] = ((byte) (time));
_offset = offset;
}
/**
* Writes a null value to the stream.
* The null will be written with the following syntax
* <p>
* <code><pre>
* N
* </pre></code>
*
* @param value the string value to write.
*/
@Override
public void writeNull()
throws IOException {
int offset = _offset;
byte[] buffer = _buffer;
if (SIZE <= offset + 16) {
flush();
offset = _offset;
}
buffer[offset++] = 'N';
_offset = offset;
}
/**
* Writes a string value to the stream using UTF-8 encoding.
* The string will be written with the following syntax:
* <p>
* <code><pre>
* S b16 b8 string-value
* </pre></code>
* <p>
* If the value is null, it will be written as
* <p>
* <code><pre>
* N
* </pre></code>
*
* @param value the string value to write.
*/
@Override
public void writeString(String value)
throws IOException {
int offset = _offset;
byte[] buffer = _buffer;
if (SIZE <= offset + 16) {
flush();
offset = _offset;
}
if (value == null) {
buffer[offset++] = (byte) 'N';
_offset = offset;
} else {
int length = value.length();
int strOffset = 0;
while (length > 0x8000) {
int sublen = 0x8000;
offset = _offset;
if (SIZE <= offset + 16) {
flush();
offset = _offset;
}
// chunk can't end in high surrogate
char tail = value.charAt(strOffset + sublen - 1);
if (0xd800 <= tail && tail <= 0xdbff)
sublen--;
buffer[offset + 0] = (byte) BC_STRING_CHUNK;
buffer[offset + 1] = (byte) (sublen >> 8);
buffer[offset + 2] = (byte) (sublen);
_offset = offset + 3;
printString(value, strOffset, sublen);
length -= sublen;
strOffset += sublen;
}
offset = _offset;
if (SIZE <= offset + 16) {
flush();
offset = _offset;
}
if (length <= STRING_DIRECT_MAX) {
buffer[offset++] = (byte) (BC_STRING_DIRECT + length);
} else if (length <= STRING_SHORT_MAX) {
buffer[offset++] = (byte) (BC_STRING_SHORT + (length >> 8));
buffer[offset++] = (byte) (length);
} else {
buffer[offset++] = (byte) ('S');
buffer[offset++] = (byte) (length >> 8);
buffer[offset++] = (byte) (length);
}
_offset = offset;
printString(value, strOffset, length);
}
}
/**
* Writes a string value to the stream using UTF-8 encoding.
* The string will be written with the following syntax:
* <p>
* <code><pre>
* S b16 b8 string-value
* </pre></code>
* <p>
* If the value is null, it will be written as
* <p>
* <code><pre>
* N
* </pre></code>
*
* @param value the string value to write.
*/
@Override
public void writeString(char[] buffer, int offset, int length)
throws IOException {
if (buffer == null) {
if (SIZE < _offset + 16)
flush();
_buffer[_offset++] = (byte) ('N');
} else {
while (length > 0x8000) {
int sublen = 0x8000;
if (SIZE < _offset + 16)
flush();
// chunk can't end in high surrogate
char tail = buffer[offset + sublen - 1];
if (0xd800 <= tail && tail <= 0xdbff)
sublen--;
_buffer[_offset++] = (byte) BC_STRING_CHUNK;
_buffer[_offset++] = (byte) (sublen >> 8);
_buffer[_offset++] = (byte) (sublen);
printString(buffer, offset, sublen);
length -= sublen;
offset += sublen;
}
if (SIZE < _offset + 16)
flush();
if (length <= STRING_DIRECT_MAX) {
_buffer[_offset++] = (byte) (BC_STRING_DIRECT + length);
} else if (length <= STRING_SHORT_MAX) {
_buffer[_offset++] = (byte) (BC_STRING_SHORT + (length >> 8));
_buffer[_offset++] = (byte) length;