/
ArrayEncoding.java
1436 lines (1267 loc) · 40.5 KB
/
ArrayEncoding.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) 2020, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.jdbc;
import org.postgresql.core.BaseConnection;
import org.postgresql.core.Encoding;
import org.postgresql.core.Oid;
import org.postgresql.util.ByteConverter;
import org.postgresql.util.GT;
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;
import org.checkerframework.checker.index.qual.Positive;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.HashMap;
import java.util.Map;
/**
* Utility for using arrays in requests.
*
* <p>
* Binary format:
* <ul>
* <li>4 bytes with number of dimensions</li>
* <li>4 bytes, boolean indicating nulls present or not</li>
* <li>4 bytes type oid</li>
* <li>8 bytes describing the length of each dimension (repeated for each dimension)</li>
* <ul>
* <li>4 bytes for length</li>
* <li>4 bytes for lower bound on length to check for overflow (it appears this value can always be 0)</li>
* </ul>
* <li>data in depth first element order corresponding number and length of dimensions</li>
* <ul>
* <li>4 bytes describing length of element, {@code 0xFFFFFFFF} ({@code -1}) means {@code null}</li>
* <li>binary representation of element (iff not {@code null}).
* </ul>
* </ul>
* </p>
*
* @author Brett Okken
*/
final class ArrayEncoding {
interface ArrayEncoder<A> {
/**
* The default array type oid supported by this instance.
*
* @return The default array type oid supported by this instance.
*/
int getDefaultArrayTypeOid();
/**
* Creates {@code String} representation of the <i>array</i>.
*
* @param delim
* The character to use to delimit between elements.
* @param array
* The array to represent as a {@code String}.
* @return {@code String} representation of the <i>array</i>.
*/
String toArrayString(char delim, A array);
/**
* Indicates if an array can be encoded in binary form to array <i>oid</i>.
*
* @param oid
* The array oid to see check for binary support.
* @return Indication of whether
* {@link #toBinaryRepresentation(BaseConnection, Object, int)} is
* supported for <i>oid</i>.
*/
boolean supportBinaryRepresentation(int oid);
/**
* Creates binary representation of the <i>array</i>.
*
* @param connection
* The connection the binary representation will be used on. Attributes
* from the connection might impact how values are translated to
* binary.
* @param array
* The array to binary encode. Must not be {@code null}, but may
* contain {@code null} elements.
* @param oid
* The array type oid to use. Calls to
* {@link #supportBinaryRepresentation(int)} must have returned
* {@code true}.
* @return The binary representation of <i>array</i>.
* @throws SQLFeatureNotSupportedException
* If {@link #supportBinaryRepresentation(int)} is false for
* <i>oid</i>.
*/
byte[] toBinaryRepresentation(BaseConnection connection, A array, int oid)
throws SQLException, SQLFeatureNotSupportedException;
/**
* Append {@code String} representation of <i>array</i> to <i>sb</i>.
*
* @param sb
* The {@link StringBuilder} to append to.
* @param delim
* The delimiter between elements.
* @param array
* The array to represent. Will not be {@code null}, but may contain
* {@code null} elements.
*/
void appendArray(StringBuilder sb, char delim, A array);
}
/**
* Base class to implement {@link ArrayEncoding.ArrayEncoder} and provide
* multi-dimensional support.
*
* @param <A>
* Base array type supported.
*/
private abstract static class AbstractArrayEncoder<A extends Object>
implements ArrayEncoder<A> {
private final int oid;
final int arrayOid;
/**
*
* @param oid
* The default/primary base oid type.
* @param arrayOid
* The default/primary array oid type.
*/
AbstractArrayEncoder(int oid, int arrayOid) {
this.oid = oid;
this.arrayOid = arrayOid;
}
/**
*
* @param arrayOid
* The array oid to get base oid type for.
* @return The base oid type for the given array oid type given to
* {@link #toBinaryRepresentation(BaseConnection, Object, int)}.
*/
int getTypeOID(@SuppressWarnings("unused") int arrayOid) {
return oid;
}
/**
* By default returns the <i>arrayOid</i> this instance was instantiated with.
*/
@Override
public int getDefaultArrayTypeOid() {
return arrayOid;
}
/**
* Counts the number of {@code null} elements in <i>array</i>.
*
* @param array
* The array to count {@code null} elements in.
* @return The number of {@code null} elements in <i>array</i>.
*/
int countNulls(A array) {
int nulls = 0;
final int arrayLength = Array.getLength(array);
for (int i = 0; i < arrayLength; ++i) {
if (Array.get(array, i) == null) {
++nulls;
}
}
return nulls;
}
/**
* Creates {@code byte[]} of just the raw data (no metadata).
*
* @param connection
* The connection the binary representation will be used on.
* @param array
* The array to create binary representation of. Will not be
* {@code null}, but may contain {@code null} elements.
* @return {@code byte[]} of just the raw data (no metadata).
* @throws SQLFeatureNotSupportedException
* If {@link #supportBinaryRepresentation(int)} is false for
* <i>oid</i>.
*/
abstract byte[] toSingleDimensionBinaryRepresentation(BaseConnection connection, A array)
throws SQLException, SQLFeatureNotSupportedException;
/**
* {@inheritDoc}
*/
@Override
public String toArrayString(char delim, A array) {
final StringBuilder sb = new StringBuilder(1024);
appendArray(sb, delim, array);
return sb.toString();
}
/**
* By default returns {@code true} if <i>oid</i> matches the <i>arrayOid</i>
* this instance was instantiated with.
*/
@Override
public boolean supportBinaryRepresentation(int oid) {
return oid == arrayOid;
}
}
/**
* Base class to provide support for {@code Number} based arrays.
*
* @param <N>
* The base type of array.
*/
private abstract static class NumberArrayEncoder<N extends Number> extends AbstractArrayEncoder<N[]> {
private final int fieldSize;
/**
*
* @param fieldSize
* The fixed size to represent each value in binary.
* @param oid
* The base type oid.
* @param arrayOid
* The array type oid.
*/
NumberArrayEncoder(int fieldSize, int oid, int arrayOid) {
super(oid, arrayOid);
this.fieldSize = fieldSize;
}
/**
* {@inheritDoc}
*/
@Override
final int countNulls(N[] array) {
int count = 0;
for (int i = 0; i < array.length; ++i) {
if (array[i] == null) {
++count;
}
}
return count;
}
/**
* {@inheritDoc}
*/
@Override
public final byte[] toBinaryRepresentation(BaseConnection connection, N[] array, int oid)
throws SQLException, SQLFeatureNotSupportedException {
assert oid == this.arrayOid;
final int nullCount = countNulls(array);
final byte[] bytes = writeBytes(array, nullCount, 20);
// 1 dimension
ByteConverter.int4(bytes, 0, 1);
// no null
ByteConverter.int4(bytes, 4, nullCount == 0 ? 0 : 1);
// oid
ByteConverter.int4(bytes, 8, getTypeOID(oid));
// length
ByteConverter.int4(bytes, 12, array.length);
// postgresql uses 1 base by default
ByteConverter.int4(bytes, 16, 1);
return bytes;
}
/**
* {@inheritDoc}
*/
@Override
final byte[] toSingleDimensionBinaryRepresentation(BaseConnection connection, N[] array)
throws SQLException, SQLFeatureNotSupportedException {
final int nullCount = countNulls(array);
return writeBytes(array, nullCount, 0);
}
private byte[] writeBytes(final N[] array, final int nullCount, final int offset) {
final int length = offset + (4 * array.length) + (fieldSize * (array.length - nullCount));
final byte[] bytes = new byte[length];
int idx = offset;
for (int i = 0; i < array.length; ++i) {
if (array[i] == null) {
ByteConverter.int4(bytes, idx, -1);
idx += 4;
} else {
ByteConverter.int4(bytes, idx, fieldSize);
idx += 4;
write(array[i], bytes, idx);
idx += fieldSize;
}
}
return bytes;
}
/**
* Write single value (<i>number</i>) to <i>bytes</i> beginning at
* <i>offset</i>.
*
* @param number
* The value to write to <i>bytes</i>. This will never be {@code null}.
* @param bytes
* The {@code byte[]} to write to.
* @param offset
* The offset into <i>bytes</i> to write the <i>number</i> value.
*/
protected abstract void write(N number, byte[] bytes, int offset);
/**
* {@inheritDoc}
*/
@Override
public final void appendArray(StringBuilder sb, char delim, N[] array) {
sb.append('{');
for (int i = 0; i < array.length; ++i) {
if (i != 0) {
sb.append(delim);
}
if (array[i] == null) {
sb.append('N').append('U').append('L').append('L');
} else {
sb.append('"');
sb.append(array[i].toString());
sb.append('"');
}
}
sb.append('}');
}
}
/**
* Base support for primitive arrays.
*
* @param <A>
* The primitive array to support.
*/
private abstract static class FixedSizePrimitiveArrayEncoder<A extends Object>
extends AbstractArrayEncoder<A> {
private final int fieldSize;
FixedSizePrimitiveArrayEncoder(int fieldSize, int oid, int arrayOid) {
super(oid, arrayOid);
this.fieldSize = fieldSize;
}
/**
* {@inheritDoc}
*
* <p>
* Always returns {@code 0}.
* </p>
*/
@Override
final int countNulls(A array) {
return 0;
}
/**
* {@inheritDoc}
*/
@Override
public final byte[] toBinaryRepresentation(BaseConnection connection, A array, int oid)
throws SQLException, SQLFeatureNotSupportedException {
assert oid == arrayOid;
final int arrayLength = Array.getLength(array);
final int length = 20 + ((fieldSize + 4) * arrayLength);
final byte[] bytes = new byte[length];
// 1 dimension
ByteConverter.int4(bytes, 0, 1);
// no null
ByteConverter.int4(bytes, 4, 0);
// oid
ByteConverter.int4(bytes, 8, getTypeOID(oid));
// length
ByteConverter.int4(bytes, 12, arrayLength);
// postgresql uses 1 base by default
ByteConverter.int4(bytes, 16, 1);
write(array, bytes, 20);
return bytes;
}
/**
* {@inheritDoc}
*/
@Override
final byte[] toSingleDimensionBinaryRepresentation(BaseConnection connection, A array)
throws SQLException, SQLFeatureNotSupportedException {
final int length = ((fieldSize + 4) * Array.getLength(array));
final byte[] bytes = new byte[length];
write(array, bytes, 0);
return bytes;
}
/**
* Write the entire contents of <i>array</i> to <i>bytes</i> starting at
* <i>offset</i> without metadata describing type or length.
*
* @param array
* The array to write.
* @param bytes
* The {@code byte[]} to write to.
* @param offset
* The offset into <i>bytes</i> to start writing.
*/
protected abstract void write(A array, byte[] bytes, int offset);
}
private static final AbstractArrayEncoder<long[]> LONG_ARRAY = new FixedSizePrimitiveArrayEncoder<long[]>(8, Oid.INT8,
Oid.INT8_ARRAY) {
/**
* {@inheritDoc}
*/
@Override
public void appendArray(StringBuilder sb, char delim, long[] array) {
sb.append('{');
for (int i = 0; i < array.length; ++i) {
if (i > 0) {
sb.append(delim);
}
sb.append(array[i]);
}
sb.append('}');
}
/**
* {@inheritDoc}
*/
@Override
protected void write(long[] array, byte[] bytes, int offset) {
int idx = offset;
for (int i = 0; i < array.length; ++i) {
bytes[idx + 3] = 8;
ByteConverter.int8(bytes, idx + 4, array[i]);
idx += 12;
}
}
};
private static final AbstractArrayEncoder<Long[]> LONG_OBJ_ARRAY = new NumberArrayEncoder<Long>(8, Oid.INT8,
Oid.INT8_ARRAY) {
@Override
protected void write(Long number, byte[] bytes, int offset) {
ByteConverter.int8(bytes, offset, number.longValue());
}
};
private static final AbstractArrayEncoder<int[]> INT_ARRAY = new FixedSizePrimitiveArrayEncoder<int[]>(4, Oid.INT4,
Oid.INT4_ARRAY) {
/**
* {@inheritDoc}
*/
@Override
public void appendArray(StringBuilder sb, char delim, int[] array) {
sb.append('{');
for (int i = 0; i < array.length; ++i) {
if (i > 0) {
sb.append(delim);
}
sb.append(array[i]);
}
sb.append('}');
}
/**
* {@inheritDoc}
*/
@Override
protected void write(int[] array, byte[] bytes, int offset) {
int idx = offset;
for (int i = 0; i < array.length; ++i) {
bytes[idx + 3] = 4;
ByteConverter.int4(bytes, idx + 4, array[i]);
idx += 8;
}
}
};
private static final AbstractArrayEncoder<Integer[]> INT_OBJ_ARRAY = new NumberArrayEncoder<Integer>(4, Oid.INT4,
Oid.INT4_ARRAY) {
@Override
protected void write(Integer number, byte[] bytes, int offset) {
ByteConverter.int4(bytes, offset, number.intValue());
}
};
private static final AbstractArrayEncoder<short[]> SHORT_ARRAY = new FixedSizePrimitiveArrayEncoder<short[]>(2,
Oid.INT2, Oid.INT2_ARRAY) {
/**
* {@inheritDoc}
*/
@Override
public void appendArray(StringBuilder sb, char delim, short[] array) {
sb.append('{');
for (int i = 0; i < array.length; ++i) {
if (i > 0) {
sb.append(delim);
}
sb.append(array[i]);
}
sb.append('}');
}
/**
* {@inheritDoc}
*/
@Override
protected void write(short[] array, byte[] bytes, int offset) {
int idx = offset;
for (int i = 0; i < array.length; ++i) {
bytes[idx + 3] = 2;
ByteConverter.int2(bytes, idx + 4, array[i]);
idx += 6;
}
}
};
private static final AbstractArrayEncoder<Short[]> SHORT_OBJ_ARRAY = new NumberArrayEncoder<Short>(2, Oid.INT2,
Oid.INT2_ARRAY) {
/**
* {@inheritDoc}
*/
@Override
protected void write(Short number, byte[] bytes, int offset) {
ByteConverter.int2(bytes, offset, number.shortValue());
}
};
private static final AbstractArrayEncoder<double[]> DOUBLE_ARRAY = new FixedSizePrimitiveArrayEncoder<double[]>(8,
Oid.FLOAT8, Oid.FLOAT8_ARRAY) {
/**
* {@inheritDoc}
*/
@Override
public void appendArray(StringBuilder sb, char delim, double[] array) {
sb.append('{');
for (int i = 0; i < array.length; ++i) {
if (i > 0) {
sb.append(delim);
}
// use quotes to account for any issues with scientific notation
sb.append('"');
sb.append(array[i]);
sb.append('"');
}
sb.append('}');
}
/**
* {@inheritDoc}
*/
@Override
protected void write(double[] array, byte[] bytes, int offset) {
int idx = offset;
for (int i = 0; i < array.length; ++i) {
bytes[idx + 3] = 8;
ByteConverter.float8(bytes, idx + 4, array[i]);
idx += 12;
}
}
};
private static final AbstractArrayEncoder<Double[]> DOUBLE_OBJ_ARRAY = new NumberArrayEncoder<Double>(8, Oid.FLOAT8,
Oid.FLOAT8_ARRAY) {
/**
* {@inheritDoc}
*/
@Override
protected void write(Double number, byte[] bytes, int offset) {
ByteConverter.float8(bytes, offset, number.doubleValue());
}
};
private static final AbstractArrayEncoder<float[]> FLOAT_ARRAY = new FixedSizePrimitiveArrayEncoder<float[]>(4,
Oid.FLOAT4, Oid.FLOAT4_ARRAY) {
/**
* {@inheritDoc}
*/
@Override
public void appendArray(StringBuilder sb, char delim, float[] array) {
sb.append('{');
for (int i = 0; i < array.length; ++i) {
if (i > 0) {
sb.append(delim);
}
// use quotes to account for any issues with scientific notation
sb.append('"');
sb.append(array[i]);
sb.append('"');
}
sb.append('}');
}
/**
* {@inheritDoc}
*/
@Override
protected void write(float[] array, byte[] bytes, int offset) {
int idx = offset;
for (int i = 0; i < array.length; ++i) {
bytes[idx + 3] = 4;
ByteConverter.float4(bytes, idx + 4, array[i]);
idx += 8;
}
}
};
private static final AbstractArrayEncoder<Float[]> FLOAT_OBJ_ARRAY = new NumberArrayEncoder<Float>(4, Oid.FLOAT4,
Oid.FLOAT4_ARRAY) {
/**
* {@inheritDoc}
*/
@Override
protected void write(Float number, byte[] bytes, int offset) {
ByteConverter.float4(bytes, offset, number.floatValue());
}
};
private static final AbstractArrayEncoder<boolean[]> BOOLEAN_ARRAY = new FixedSizePrimitiveArrayEncoder<boolean[]>(1,
Oid.BOOL, Oid.BOOL_ARRAY) {
/**
* {@inheritDoc}
*/
@Override
public void appendArray(StringBuilder sb, char delim, boolean[] array) {
sb.append('{');
for (int i = 0; i < array.length; ++i) {
if (i > 0) {
sb.append(delim);
}
sb.append(array[i] ? '1' : '0');
}
sb.append('}');
}
/**
* {@inheritDoc}
*/
@Override
protected void write(boolean[] array, byte[] bytes, int offset) {
int idx = offset;
for (int i = 0; i < array.length; ++i) {
bytes[idx + 3] = 1;
ByteConverter.bool(bytes, idx + 4, array[i]);
idx += 5;
}
}
};
private static final AbstractArrayEncoder<Boolean[]> BOOLEAN_OBJ_ARRAY = new AbstractArrayEncoder<Boolean[]>(Oid.BOOL,
Oid.BOOL_ARRAY) {
/**
* {@inheritDoc}
*/
@Override
public byte[] toBinaryRepresentation(BaseConnection connection, Boolean[] array, int oid)
throws SQLException, SQLFeatureNotSupportedException {
assert oid == arrayOid;
final int nullCount = countNulls(array);
final byte[] bytes = writeBytes(array, nullCount, 20);
// 1 dimension
ByteConverter.int4(bytes, 0, 1);
// no null
ByteConverter.int4(bytes, 4, nullCount == 0 ? 0 : 1);
// oid
ByteConverter.int4(bytes, 8, getTypeOID(oid));
// length
ByteConverter.int4(bytes, 12, array.length);
// postgresql uses 1 base by default
ByteConverter.int4(bytes, 16, 1);
return bytes;
}
private byte[] writeBytes(final Boolean[] array, final int nullCount, final int offset) {
final int length = offset + (4 * array.length) + (array.length - nullCount);
final byte[] bytes = new byte[length];
int idx = offset;
for (int i = 0; i < array.length; ++i) {
if (array[i] == null) {
ByteConverter.int4(bytes, idx, -1);
idx += 4;
} else {
ByteConverter.int4(bytes, idx, 1);
idx += 4;
write(array[i], bytes, idx);
++idx;
}
}
return bytes;
}
private void write(Boolean bool, byte[] bytes, int idx) {
ByteConverter.bool(bytes, idx, bool.booleanValue());
}
/**
* {@inheritDoc}
*/
@Override
byte[] toSingleDimensionBinaryRepresentation(BaseConnection connection, Boolean[] array)
throws SQLException, SQLFeatureNotSupportedException {
final int nullCount = countNulls(array);
return writeBytes(array, nullCount, 0);
}
/**
* {@inheritDoc}
*/
@Override
public void appendArray(StringBuilder sb, char delim, Boolean[] array) {
sb.append('{');
for (int i = 0; i < array.length; ++i) {
if (i != 0) {
sb.append(delim);
}
if (array[i] == null) {
sb.append('N').append('U').append('L').append('L');
} else {
sb.append(array[i].booleanValue() ? '1' : '0');
}
}
sb.append('}');
}
};
private static final AbstractArrayEncoder<String[]> STRING_ARRAY = new AbstractArrayEncoder<String[]>(Oid.VARCHAR,
Oid.VARCHAR_ARRAY) {
/**
* {@inheritDoc}
*/
@Override
int countNulls(String[] array) {
int count = 0;
for (int i = 0; i < array.length; ++i) {
if (array[i] == null) {
++count;
}
}
return count;
}
/**
* {@inheritDoc}
*/
@Override
public boolean supportBinaryRepresentation(int oid) {
return oid == Oid.VARCHAR_ARRAY || oid == Oid.TEXT_ARRAY;
}
/**
* {@inheritDoc}
*/
@Override
int getTypeOID(int arrayOid) {
if (arrayOid == Oid.VARCHAR_ARRAY) {
return Oid.VARCHAR;
}
if (arrayOid == Oid.TEXT_ARRAY) {
return Oid.TEXT;
}
// this should not be possible based on supportBinaryRepresentation returning
// false for all other types
throw new IllegalStateException("Invalid array oid: " + arrayOid);
}
/**
* {@inheritDoc}
*/
@Override
public void appendArray(StringBuilder sb, char delim, String[] array) {
sb.append('{');
for (int i = 0; i < array.length; ++i) {
if (i > 0) {
sb.append(delim);
}
if (array[i] == null) {
sb.append('N').append('U').append('L').append('L');
} else {
PgArray.escapeArrayElement(sb, array[i]);
}
}
sb.append('}');
}
/**
* {@inheritDoc}
*/
@Override
public byte[] toBinaryRepresentation(BaseConnection connection, String[] array, int oid) throws SQLException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(Math.min(1024, (array.length * 32) + 20));
assert supportBinaryRepresentation(oid);
final byte[] buffer = new byte[4];
try {
// 1 dimension
ByteConverter.int4(buffer, 0, 1);
baos.write(buffer);
// null
ByteConverter.int4(buffer, 0, countNulls(array) > 0 ? 1 : 0);
baos.write(buffer);
// oid
ByteConverter.int4(buffer, 0, getTypeOID(oid));
baos.write(buffer);
// length
ByteConverter.int4(buffer, 0, array.length);
baos.write(buffer);
// postgresql uses 1 base by default
ByteConverter.int4(buffer, 0, 1);
baos.write(buffer);
final Encoding encoding = connection.getEncoding();
for (int i = 0; i < array.length; ++i) {
final String string = array[i];
if (string != null) {
final byte[] encoded;
try {
encoded = encoding.encode(string);
} catch (IOException e) {
throw new PSQLException(GT.tr("Unable to translate data into the desired encoding."),
PSQLState.DATA_ERROR, e);
}
ByteConverter.int4(buffer, 0, encoded.length);
baos.write(buffer);
baos.write(encoded);
} else {
ByteConverter.int4(buffer, 0, -1);
baos.write(buffer);
}
}
return baos.toByteArray();
} catch (IOException e) {
// this IO exception is from writing to baos, which will never throw an
// IOException
throw new java.lang.AssertionError(e);
}
}
/**
* {@inheritDoc}
*/
@Override
byte[] toSingleDimensionBinaryRepresentation(BaseConnection connection, String[] array)
throws SQLException, SQLFeatureNotSupportedException {
try {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(Math.min(1024, (array.length * 32) + 20));
final byte[] buffer = new byte[4];
final Encoding encoding = connection.getEncoding();
for (int i = 0; i < array.length; ++i) {
final String string = array[i];
if (string != null) {
final byte[] encoded;
try {
encoded = encoding.encode(string);
} catch (IOException e) {
throw new PSQLException(GT.tr("Unable to translate data into the desired encoding."),
PSQLState.DATA_ERROR, e);
}
ByteConverter.int4(buffer, 0, encoded.length);
baos.write(buffer);
baos.write(encoded);
} else {
ByteConverter.int4(buffer, 0, -1);
baos.write(buffer);
}
}
return baos.toByteArray();
} catch (IOException e) {
// this IO exception is from writing to baos, which will never throw an
// IOException
throw new java.lang.AssertionError(e);
}
}
};
private static final AbstractArrayEncoder<byte[][]> BYTEA_ARRAY = new AbstractArrayEncoder<byte[][]>(Oid.BYTEA,
Oid.BYTEA_ARRAY) {
/**
* The possible characters to use for representing hex binary data.
*/
private final char[] hexDigits = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f' };
/**
* {@inheritDoc}
*/
@Override
public byte[] toBinaryRepresentation(BaseConnection connection, byte[][] array, int oid)
throws SQLException, SQLFeatureNotSupportedException {
assert oid == arrayOid;
int length = 20;
for (int i = 0; i < array.length; ++i) {
length += 4;
if (array[i] != null) {
length += array[i].length;
}
}
final byte[] bytes = new byte[length];
// 1 dimension
ByteConverter.int4(bytes, 0, 1);
// no null
ByteConverter.int4(bytes, 4, 0);
// oid
ByteConverter.int4(bytes, 8, getTypeOID(oid));
// length
ByteConverter.int4(bytes, 12, array.length);
// postgresql uses 1 base by default
ByteConverter.int4(bytes, 16, 1);
write(array, bytes, 20);
return bytes;
}
/**
* {@inheritDoc}
*/
@Override
byte[] toSingleDimensionBinaryRepresentation(BaseConnection connection, byte[][] array)
throws SQLException, SQLFeatureNotSupportedException {
int length = 0;
for (int i = 0; i < array.length; ++i) {
length += 4;
if (array[i] != null) {
length += array[i].length;
}
}
final byte[] bytes = new byte[length];
write(array, bytes, 0);
return bytes;
}
/**
* {@inheritDoc}
*/
@Override
int countNulls(byte[][] array) {
int nulls = 0;
for (int i = 0; i < array.length; ++i) {
if (array[i] == null) {
++nulls;
}
}
return nulls;
}
private void write(byte[][] array, byte[] bytes, int offset) {