-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathBaseBytes.java
More file actions
4248 lines (3816 loc) · 159 KB
/
BaseBytes.java
File metadata and controls
4248 lines (3816 loc) · 159 KB
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
package org.python.core;
import java.util.AbstractList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
/**
* Base class for Jython {@code bytearray} (and {@code bytes} in due course) that provides most of
* the Java API, including Java {@link List} behaviour. Attempts to modify the contents through this
* API will throw a {@code TypeError} if the actual type of the object is not mutable. It is
* possible for a Java client to treat this class as a {@code List<PyInteger>}, obtaining equivalent
* functionality to the Python interface in a Java paradigm.
* <p>
* Subclasses must define (from {@link PySequence}):
* <ul>
* <li>{@link #getslice(int, int, int)}</li>
* <li>{@link #repeat(int)}</li>
* </ul>
* each returning an appropriate concrete type. Mutable subclasses should override:
* <ul>
* <li>{@link #pyset(int, PyObject)}</li>
* <li>{@link #setslice(int, int, int, PyObject)}</li>
* <li>{@link #del(int)}</li>
* <li>{@link #delRange(int, int)}</li>
* </ul>
* since the default implementations will otherwise throw an exception.
* <p>
* Many of the methods implemented here are inherited or thinly wrapped by {@link PyByteArray},
* which offers them as Java API, or exposes them as Python methods. These prototype Python methods
* mostly accept a {@link PyObject} as argument, where you might have expected a {@code byte[]} or
* {@code BaseBytes}, in order to accommodate the full range of types accepted by the Python
* equivalent: usually, any {@code PyObject} that implements {@link BufferProtocol}, providing a
* one-dimensional array of bytes, is an acceptable argument. In the documentation, the reader will
* often see the terms "byte array" or "object viewable as bytes" instead of {@code BaseBytes} when
* this broader scope is intended.
* <p>
* Where the methods return a {@code BaseBytes}, this is will normally be an instance of the class
* of the object on which the method was actually called. For example {@link #capitalize()}, defined
* in {@code BaseBytes} to return a BaseBytes, actually returns a {@link PyByteArray} when applied
* to a {@code bytearray}. Or it may be that the method returns a {@code PyList} of instances of the
* target type, for example {@link #rpartition(PyObject)}. This is achieved by the sub-class
* defining {@link #getslice(int, int, int)} and {@link #getResult(Builder)} to return instances of
* its own type. See the documentation of the particular methods for more information.
*/
@Untraversable
public abstract class BaseBytes extends PySequence implements List<PyInteger> {
/**
* Constructs a zero-length {@code BaseBytes} of explicitly-specified sub-type.
*
* @param type explicit Jython type
*/
public BaseBytes(PyType type) {
super(type, null);
delegator = new IndexDelegate();
setStorage(emptyStorage);
}
/**
* Constructs a zero-filled array of defined size and type.
*
* @param size required
* @param type explicit Jython type
*/
public BaseBytes(PyType type, int size) {
super(type, null);
delegator = new IndexDelegate();
newStorage(size);
}
/**
* Constructs a byte array of defined type by copying values from int[].
*
* @param type explicit Jython type
* @param value source of values (and size)
*/
public BaseBytes(PyType type, int[] value) {
super(type, null);
delegator = new IndexDelegate();
int n = value.length;
newStorage(n);
for (int i = offset, j = 0; j < n; i++, j++) {
storage[i] = byteCheck(value[j]);
}
}
/**
* Constructs a byte array of defined type by copying character values from a String. These
* values have to be in the Python byte range 0 to 255.
*
* @param type explicit Jython type
* @param value source of characters
* @throws PyException {@code ValueError} if any {@code value[i] > 255}
*/
protected BaseBytes(PyType type, String value) throws PyException {
super(type, null);
delegator = new IndexDelegate();
int n = value.length();
newStorage(n);
for (int i = offset, j = 0; j < n; j++) {
storage[i++] = byteCheck(value.charAt(j));
}
}
/**
* Helper for constructors and methods that manipulate the storage in mutable subclasses. It
* also permits shared storage between objects, which in general is unsafe if the storage is
* subject to modification independent of the object now being created. Immutable types may
* share storage (safely).
*
* @param storage byte array allocated by client
* @param size number of bytes actually used
* @param offset index of first byte used
* @throws IllegalArgumentException if the range {@code [offset:offset+size]} is not within the
* array bounds of storage or {@code size<0}.
*/
protected void setStorage(byte[] storage, int size, int offset)
throws IllegalArgumentException {
if (size < 0 || offset < 0 || offset + size > storage.length) {
throw new IllegalArgumentException();
} else {
this.storage = storage;
this.size = size;
this.offset = offset;
}
}
/**
* Helper for constructors and methods that manipulate the storage in mutable subclasses in the
* case where the storage should consist of the first part of the given array.
*
* @param storage byte array allocated by client
* @param size number of bytes actually used
* @throws IllegalArgumentException if the range {@code [0:size]} is not within the array bounds
* of storage.
*/
protected void setStorage(byte[] storage, int size) throws IllegalArgumentException {
if (size < 0 || size > storage.length) {
throw new IllegalArgumentException();
} else {
this.storage = storage;
this.size = size;
this.offset = 0;
}
}
/**
* Helper for constructors and methods that manipulate the storage in mutable subclasses in the
* case where the storage should consist of exactly the whole of the given array.
*
* @param storage byte array allocated by client
*/
protected void setStorage(byte[] storage) {
this.storage = storage;
this.size = storage.length;
this.offset = 0;
}
/*
* ============================================================================================
* Support for construction and initialisation
* ============================================================================================
*
* Methods here help subclasses set the initial state. They are designed with bytearray in mind,
* but note that from Python 3, bytes() has the same set of calls and behaviours. In Peterson's
* "sort of backport" to Python 2.x, bytes is effectively an alias for str and it shows.
*/
/**
* Helper for {@code __new__} and {@code __init__} and the Java API constructor from PyObject in
* subclasses.
*
* @see org.python.core.PyByteArray#bytearray___init__(PyObject[], String[])
* @see org.python.core.PyByteArray#PyByteArray(PyObject)
* @param arg primary argument from which value is taken
*/
protected void init(PyObject arg) {
if (arg == null) {
// bytearray() Construct a zero-length bytearray.
setStorage(emptyStorage);
} else if (arg instanceof PyString) { // or PyUnicode
/*
* bytearray(string) Construct from a text string by default encoding and error policy.
* Cases where encoding and error policy are specified explicitly are dealt with
* elsewhere.
*/
init((PyString) arg, (String) null, (String) null); // Casts select right init()
} else if (arg.isIndex()) {
// bytearray(int) Construct a zero-initialised bytearray of the given length.
init(arg.asIndex(Py.OverflowError)); // overflow if too big to be Java int
} else if (arg instanceof BaseBytes) {
// bytearray copy of bytearray (or bytes) -- do efficiently
init((BaseBytes) arg);
} else if (initFromBuffer(arg)) {
// arg supports Jython implementation of PEP 3118. (We're done.)
} else {
// The remaining alternative is an iterable returning (we hope) right-sized ints.
init(arg.asIterable());
}
}
/**
* Helper for {@code __new__} and {@code __init__} and the Java API constructor from a text
* string with the specified encoding in subclasses.
*
* @see PyByteArray#bytearray___init__(PyObject[], String[])
* @see PyByteArray#PyByteArray(PyString, String, String)
* @param arg primary argument from which value is taken
* @param encoding name of optional encoding (must be a string type)
* @param errors name of optional errors policy (must be a string type)
*/
protected void init(PyString arg, PyObject encoding, PyObject errors) {
String enc = encoding == null ? null : encoding.asString();
String err = errors == null ? null : errors.asString();
init(arg, enc, err);
}
/**
* Helper for {@code __new__} and {@code __init__} and the Java API constructor from a text
* string with the specified encoding in subclasses.
*
* @see PyByteArray#bytearray___init__(PyObject[], String[])
* @see PyByteArray#PyByteArray(PyString, String, String)
* @param arg primary argument from which value is taken
* @param encoding name of optional encoding
* @param errors name of optional errors policy
*/
protected void init(PyString arg, String encoding, String errors) {
// Jython encode emits a String (not byte[])
String encoded = encode(arg, encoding, errors);
newStorage(encoded.length());
setBytes(0, encoded);
}
/**
* Helper for {@link #setslice(int, int, int, PyObject)}, for {@code __new__} and
* {@code __init__} and the Java API constructor from a text string with the specified encoding
* in subclasses. This method thinly wraps a call to the codecs module and deals with checking
* for PyUnicode (where the encoding argument is mandatory).
*
* @see PyByteArray#PyByteArray(PyString, String, String)
* @param arg primary argument from which value is taken
* @param encoding name of optional encoding
* @param errors name of optional errors policy
* @return encoded string
* @throws PyException {@code TypeError} if the {@code PyString} is actually a {@link PyUnicode}
* and encoding is {@code null}
*/
protected static String encode(PyString arg, String encoding, String errors)
throws PyException {
// Jython encode emits a String (not byte[])
String encoded;
if (arg instanceof PyUnicode) {
if (encoding != null) {
encoded = codecs.encode(arg, encoding, errors);
} else {
throw Py.TypeError("unicode argument without an encoding");
}
} else {
if (encoding != null) {
encoded = codecs.encode(arg, encoding, errors);
} else {
encoded = arg.getString();
}
}
return encoded;
}
/**
* Fill a defined section of a byte array by copying character values from a String. These
* values have to be in the Python byte range 0 to 255.
*
* @param start index in this byte array at which the first character code lands
* @param value source of characters
* @throws PyException {@code ValueError} if any {@code value[i] > 255}
*/
protected void setBytes(int start, String value) throws PyException {
int n = value.length();
int io = offset + start;
for (int j = 0; j < n; j++) {
storage[io++] = byteCheck(value.charAt(j));
}
}
/**
* Fill a strided slice of a byte array by copying character values from a String. These values
* have to be in the Python byte range 0 to 255.
*
* @param start index in this byte array at which the first character code lands
* @param value source of characters
* @throws PyException {@code ValueError} if any {@code value[i] > 255}
*/
protected void setBytes(int start, int step, String value) throws PyException {
int n = value.length();
int io = offset + start;
for (int j = 0; j < n; j++) {
storage[io] = byteCheck(value.charAt(j));
io += step;
}
}
/**
* Helper for {@code __new__} and {@code __init__} and the Java API constructor from int in
* subclasses. Construct zero-filled byte array of specified size.
*
* @param n size of zero-filled array
*/
protected void init(int n) {
if (n < 0) {
throw Py.ValueError("negative count");
}
newStorage(n);
}
/**
* Helper for {@code __new__} and {@code __init__} and the Java API constructor from objects
* supporting the Jython implementation of PEP 3118 (Buffer API) in subclasses.
*
* @param value an object bearing the Buffer API and consistent with the slice assignment
*/
protected void init(BufferProtocol value) throws PyException, ClassCastException {
// Get the buffer view
try (PyBuffer view = value.getBuffer(PyBUF.SIMPLE)) {
// Create storage for the bytes and have the view drop them in
newStorage(view.getLen());
view.copyTo(storage, offset);
}
}
/**
* Helper for {@code __new__} and {@code __init__} from objects that <b>might</b> support the
* Jython Buffer API.
*
* @param value an object possibly bearing the Buffer API
* @return {@code true} iff {@code value} allows the {@code getBuffer}
*/
private boolean initFromBuffer(PyObject value) throws PyException {
if (value instanceof BufferProtocol) {
try {
init((BufferProtocol) value);
return true;
} catch (ClassCastException e) { /* fall through to false */ }
}
return false;
}
/**
* Helper for {@code __new__} and {@code __init__} and the Java API constructor from
* {@code bytearray} or {@code bytes} in subclasses.
*
* @param source {@code bytearray} (or {@code bytes}) to copy
*/
protected void init(BaseBytes source) {
newStorage(source.size);
System.arraycopy(source.storage, source.offset, storage, offset, size);
}
/**
* Helper for {@code __new__} and {@code __init__} and the Java API constructor from an
* arbitrary iterable Python type in subclasses. This will include generators and lists.
*
* @param iter iterable source of values to enter in the array
*/
protected void init(Iterable<? extends PyObject> iter) {
/*
* Different strategy is needed from that suited to "random append" operations. We shall
* have a stream of append operations, and it might be long.
*/
FragmentList fragList = new FragmentList();
fragList.loadFrom(iter);
// Now, aggregate all those fragments.
//
if (fragList.totalCount > 0) {
if (fragList.size() == 1) {
// Note that the first fragment is small: negligible waste if stolen directly.
Fragment frag = fragList.getFirst();
setStorage(frag.storage, frag.count);
} else {
// Stitch the fragments together in new storage of sufficient size
newStorage(fragList.totalCount);
fragList.emptyInto(storage, offset);
}
} else {
// Nothing in the iterator
setStorage(emptyStorage);
}
}
/**
* Intended as a fragment of temporary storage for use we do not know how many bytes of
* allocate, and we are reading in some kind of iterable stream.
*/
protected static class Fragment {
static final int MINSIZE = 8;
static final int MAXSIZE = 1024;
byte[] storage;
int count = 0;
Fragment(int size) {
storage = new byte[size];
}
// Convert to byte and add to buffer
boolean isFilledBy(PyObject value) {
storage[count++] = byteCheck(value);
return count == storage.length;
}
}
/**
* A container of temporary storage when we do not know how many bytes to allocate, and we are
* reading in some kind of iterable stream.
*/
protected static class FragmentList extends LinkedList<Fragment> {
/**
* Total number of bytes being held.
*/
int totalCount = 0;
/**
* Load bytes into the container from the given iterable
*
* @param iter iterable source of values to enter into the container
* @throws PyException {@code TypeError} if any value not acceptable type
* @throws PyException {@code ValueError} if any value<0 or value>255 or string
* length!=1
*/
void loadFrom(Iterable<? extends PyObject> iter) throws PyException {
int fragSize = Fragment.MINSIZE;
Fragment curr = null;
// Allocate series of fragments as needed, while the iterator runs to completion
try {
for (PyObject value : iter) {
if (curr == null) {
// Need a new Fragment
curr = new Fragment(fragSize);
add(curr);
if (fragSize < Fragment.MAXSIZE) {
fragSize <<= 1;
}
}
// Insert next item from iterator.
if (curr.isFilledBy(value)) {
// Fragment is now full: signal a new one will be needed
totalCount += curr.count;
curr = null;
}
}
} catch (OutOfMemoryError e) {
throw Py.MemoryError(e.getMessage());
}
// Don't forget the bytes in the final Fragment
if (curr != null) {
totalCount += curr.count;
}
}
/**
* Move the contents of this container to the given byte array at the specified index. This
* method leaves this container empty.
*
* @param target destination array
* @param p position to write first byte
*/
void emptyInto(byte[] target, int p) {
for (Fragment frag : this) {
System.arraycopy(frag.storage, 0, target, p, frag.count);
p += frag.count;
}
clear(); // Encourage recycling
totalCount = 0;
}
/**
* Move the contents of this container to a strided subset of the given byte array at the
* specified index. Bytes are assigned at start, start+step, start+2*step, and so on until
* we run out. (You must have checked beforehand that the destination is big enough.) This
* method leaves this container empty. If the step size is one, it would be much quicker to
* call {@link BaseBytes#emptyInto(byte[], int)}
*
* @param target destination array
* @param start position to write first byte
* @param step amount to advance index with each byte
*/
void emptyInto(byte[] target, int start, int step) {
int p = start;
for (Fragment frag : this) {
for (int i = 0; i < frag.count; i++) {
target[p] = frag.storage[i];
p += step;
}
}
clear(); // Encourage recycling
totalCount = 0;
}
}
/*
* ============================================================================================
* Sharable storage
* ============================================================================================
*
* The storage is provided by a byte array that may be somewhat larger than the number of bytes
* actually stored, and these bytes may begin at an offset within the storage. Immutable
* subclasses of BaseBytes may exploit this to share storage when constructed from a slice of
* another immutable subclass. Mutable subclasses may exploit it to provide efficient insertions
* near the start of the array.
*/
/** Empty storage constant */
protected static final byte[] emptyStorage = new byte[0];
/** Storage array. */
protected byte[] storage = emptyStorage;
/** Number of bytes actually used in storage array. */
protected int size = 0;
/** Index of first byte used in storage array. */
protected int offset = 0;
/**
* Check that an index is within the range of the array, that is <tt>0<=index<size</tt>.
*
* @param index to check
* @throws PyException {@code IndexError} if the index is outside the array bounds
*/
protected final void indexCheck(int index) throws PyException {
if (index < 0 || index >= size) {
throw Py.IndexError(getType().fastGetName() + " index out of range");
}
}
/**
* Allocate fresh, zero-filled storage for the requested number of bytes and make that the size.
* If the size needed is zero, the "storage" allocated is the shared emptyStorage array. The
* allocated size may be bigger than needed, and the method chooses a value for offset.
*
* @param needed becomes the new value of this.size
*/
protected void newStorage(int needed) {
// The implementation for immutable arrays allocates only as many bytes as needed.
if (needed > 0) {
try {
setStorage(new byte[needed]); // guaranteed zero (by JLS 2ed para 4.5.5)
} catch (OutOfMemoryError e) {
throw Py.MemoryError(e.getMessage());
}
} else {
setStorage(emptyStorage);
}
}
/**
* Check that an integer is suitable for storage in a (Python) byte array, and convert it to the
* Java byte value that can be stored there. (Java bytes run -128..127 whereas Python bytes run
* 0..255.)
*
* @param value to convert.
* @throws PyException {@code ValueError} if value<0 or value>255
*/
protected static final byte byteCheck(int value) throws PyException {
if (value < 0 || value > 255) {
throw Py.ValueError("byte must be in range(0, 256)");
}
return (byte) value;
}
/**
* Check that the value of an PyInteger is suitable for storage in a (Python) byte array, and
* convert it to the Java byte value that can be stored there. (Java bytes run -128..127 whereas
* Python bytes run 0..255.)
*
* @param value to convert.
* @throws PyException {@code ValueError} if value<0 or value>255
*/
protected static final byte byteCheck(PyInteger value) throws PyException {
return byteCheck(value.asInt());
}
/**
* Check that the type and value of a PyObject is suitable for storage in a (Python) byte array,
* and convert it to the Java byte value that can be stored there. (Java bytes run -128..127
* whereas Python bytes run 0..255.) Acceptable types are:
* <ul>
* <li>PyInteger in range 0 to 255 inclusive</li>
* <li>PyLong in range 0 to 255 inclusive</li>
* <li>Any type having an __index__() method, in range 0 to 255 inclusive</li>
* <li>PyString of length 1</li>
* </ul>
*
* @param value to convert.
* @throws PyException {@code TypeError} if not acceptable type
* @throws PyException {@code ValueError} if value<0 or value>255 or string length!=1
*/
protected static final byte byteCheck(PyObject value) throws PyException {
if (value.isIndex()) {
// This will possibly produce Py.OverflowError("long int too large to convert")
return byteCheck(value.asIndex());
} else if (value.getType() == PyString.TYPE) {
// Exactly PyString (not PyUnicode)
String strValue = ((PyString) value).getString();
if (strValue.length() != 1) {
throw Py.ValueError("string must be of size 1");
}
return byteCheck(strValue.charAt(0));
} else {
throw Py.TypeError("an integer or string of size 1 is required");
}
}
/**
* Return a buffer exported by the argument, or return {@code null} if it does not bear the
* buffer API. The caller is responsible for calling {@link PyBuffer#release()} on the buffer,
* if the return value is not {@code null}.
*
* @param b object to wrap
* @return byte-oriented view or {@code null}
*/
protected static PyBuffer getView(PyObject b) {
if (b == null) {
return null;
} else if (b instanceof PyUnicode) {
/*
* PyUnicode has the BufferProtocol interface as it extends PyString. (It would bring
* you 0xff&charAt(i) in practice.) However, in CPython the unicode string does not have
* the buffer API.
*/
return null;
} else if (b instanceof BufferProtocol) {
return ((BufferProtocol) b).getBuffer(PyBUF.FULL_RO);
} else {
return null;
}
}
/**
* Return a buffer exported by the argument or raise an exception if it does not bear the buffer
* API. The caller is responsible for calling {@link PyBuffer#release()} on the buffer. The
* return value is never {@code null}.
*
* @param b object to wrap
* @return byte-oriented view
*/
protected static PyBuffer getViewOrError(PyObject b) {
PyBuffer buffer = getView(b);
if (buffer != null) {
return buffer;
} else {
String fmt = "Type %s doesn't support the buffer API";
throw Py.TypeError(String.format(fmt, b.getType().fastGetName()));
}
}
/*
* ============================================================================================
* API for org.python.core.PySequence
* ============================================================================================
*/
@Override
protected PyInteger pyget(int index) {
return new PyInteger(intAt(index));
}
/*
* We're not implementing these here, but we can give a stronger guarantee about the return type
* and save some casting and type anxiety.
*/
@Override
protected abstract BaseBytes getslice(int start, int stop, int step);
@Override
protected abstract BaseBytes repeat(int count);
/*
* And this extension point should be overridden in mutable subclasses
*/
/**
* Insert the element (interpreted as a Python byte value) at the given index. The default
* implementation produces a Python TypeError, for the benefit of immutable types. Mutable types
* must override it.
*
* @param index to insert at
* @param element to insert (by value)
* @throws PyException {@code IndexError} if the index is outside the array bounds
* @throws PyException {@code ValueError} if element<0 or element>255
* @throws PyException {@code TypeError} if the subclass is immutable
*/
public void pyinsert(int index, PyObject element) {
// This won't succeed: it just produces the right error.
// storageReplace(index, 0, 1);
pyset(index, element);
}
/**
* Specialisation of {@link #getslice(int, int, int)} to contiguous slices (of step size 1) for
* brevity and efficiency. The default implementation is {@code getslice(start, stop, 1)} but it
* is worth overriding.
*
* @param start the position of the first element.
* @param stop one more than the position of the last element.
* @return a subclass instance of BaseBytes corresponding the the given range of elements.
*/
protected BaseBytes getslice(int start, int stop) {
return getslice(start, stop, 1);
}
/**
* Class defining the behaviour of {@code bytearray} with respect to slice assignment, etc.,
* which differs from the default (list) behaviour in small ways.
*/
private class IndexDelegate extends PySequence.DefaultIndexDelegate {
/**
* bytearray treats assignment of a zero-length object to a slice as equivalent to deletion,
* unlike list, even for an extended slice.
*/
@Override
public void checkIdxAndSetSlice(PySlice slice, PyObject value) {
if (value.__len__() != 0) {
// Proceed as default
super.checkIdxAndSetSlice(slice, value);
} else {
// Treat as deletion
checkIdxAndDelItem(slice);
}
}
@Override
protected void delSlice(int[] indices) {
delslice(indices[0], indices[1], indices[2], indices[3]);
}
};
/*
* ============================================================================================
* Support for Python API common to mutable and immutable subclasses
* ============================================================================================
*/
@Override
public int __len__() {
return size;
}
/**
* Comparison function between a byte array and a buffer of bytes exported by some other object,
* such as a String, presented as a {@code PyBuffer}, returning 1, 0 or -1 as a>b, a==b, or
* a<b respectively. The comparison is by value, using Python unsigned byte conventions,
* left-to-right (low to high index). Zero bytes are significant, even at the end of the array:
* {@code [65,66,67]<"ABC\u0000"}, for example and {@code []} is less than every non-empty b,
* while {@code []==""}.
*
* @param a left-hand array in the comparison
* @param b right-hand wrapped object in the comparison
* @return 1, 0 or -1 as a>b, a==b, or a<b respectively
*/
private static int compare(BaseBytes a, PyBuffer b) {
// Compare elements one by one in these ranges:
int ap = a.offset;
int aEnd = ap + a.size;
int bp = 0;
int bEnd = b.getLen();
while (ap < aEnd) {
if (bp >= bEnd) {
// a is longer than b
return 1;
} else {
// Compare the corresponding bytes
int aVal = 0xff & a.storage[ap++];
int bVal = b.intAt(bp++);
int diff = aVal - bVal;
if (diff != 0) {
return (diff < 0) ? -1 : 1;
}
}
}
// All the bytes matched and we reached the end of a
if (bp < bEnd) {
// But we didn't reach the end of b
return -1;
} else {
// And the end of b at the same time, so they're equal
return 0;
}
}
/**
* Comparison function between byte array types and any other object. The six "rich comparison"
* operators are based on this.
*
* @param b
* @return 1, 0 or -1 as this>b, this==b, or this<b respectively, or -2 if the comparison is
* not implemented
*/
private synchronized int basebytes_cmp(PyObject b) {
// This is not exposed as bytearray and bytes have no __cmp__.
if (this == b) {
// Same object: quick result
return 0;
} else {
// Try to get a byte-oriented view
try (PyBuffer bv = getView(b)) {
if (bv == null) {
// Signifies a type mis-match. See PyObject._cmp_unsafe() and related code.
return -2;
} else {
// Compare this with other object viewed as a buffer
return compare(this, bv);
}
}
}
}
/**
* Fail-fast comparison function between byte array types and any other object, for when the
* test is only for equality. The "rich comparison" operators {@code __eq__} and {@code __ne__}
* are based on this.
*
* @param b
* @return 0 if this==b, or +1 or -1 if this!=b, or -2 if the comparison is not implemented
*/
private synchronized int basebytes_cmpeq(PyObject b) {
if (this == b) {
// Same object: quick result
return 0;
} else {
// Try to get a byte-oriented view
try (PyBuffer bv = getView(b)) {
if (bv == null) {
// Signifies a type mis-match. See PyObject._cmp_unsafe() and related code.
return -2;
} else {
if (bv.getLen() != size) {
// Different size: can't be equal, and we don't care which is bigger
return 1;
} else {
// Compare this with other object viewed as a buffer
return compare(this, bv);
}
}
}
}
}
/**
* Implementation of __eq__ (equality) operator, capable of comparison with another byte array.
* Comparison with an invalid type returns {@code null}.
*
* @param other Python object to compare with
* @return Python boolean result or {@code null} if not implemented for the other type.
*/
final PyObject basebytes___eq__(PyObject other) {
int cmp = basebytes_cmpeq(other);
if (cmp == 0) {
return Py.True;
} else if (cmp > -2) {
return Py.False;
} else {
return null;
}
}
/**
* Implementation of __ne__ (not equals) operator, capable of comparison with another byte
* array. Comparison with an invalid type returns {@code null}.
*
* @param other Python object to compare with
* @return Python boolean result or {@code null} if not implemented for the other type.
*/
final PyObject basebytes___ne__(PyObject other) {
int cmp = basebytes_cmpeq(other);
if (cmp == 0) {
return Py.False;
} else if (cmp > -2) {
return Py.True;
} else {
return null;
}
}
/**
* Implementation of __lt__ (less than) operator, capable of comparison with another byte array.
* Comparison with an invalid type returns {@code null}.
*
* @param other Python object to compare with
* @return Python boolean result or {@code null} if not implemented for the other type.
*/
final PyObject basebytes___lt__(PyObject other) {
int cmp = basebytes_cmp(other);
if (cmp >= 0) {
return Py.False;
} else if (cmp > -2) {
return Py.True;
} else {
return null;
}
}
/**
* Implementation of __le__ (less than or equal to) operator, capable of comparison with another
* byte array. Comparison with an invalid type returns {@code null}.
*
* @param other Python object to compare with
* @return Python boolean result or {@code null} if not implemented for the other type.
*/
final PyObject basebytes___le__(PyObject other) {
int cmp = basebytes_cmp(other);
if (cmp > 0) {
return Py.False;
} else if (cmp > -2) {
return Py.True;
} else {
return null;
}
}
/**
* Implementation of __ge__ (greater than or equal to) operator, capable of comparison with
* another byte array. Comparison with an invalid type returns {@code null}.
*
* @param other Python object to compare with
* @return Python boolean result or {@code null} if not implemented for the other type.
*/
final PyObject basebytes___ge__(PyObject other) {
int cmp = basebytes_cmp(other);
if (cmp >= 0) {
return Py.True;
} else if (cmp > -2) {
return Py.False;
} else {
return null;
}
}
/**
* Implementation of __gt__ (greater than) operator, capable of comparison with another byte
* array. Comparison with an invalid type returns {@code null}.
*
* @param other Python object to compare with
* @return Python boolean result or {@code null} if not implemented for the other type.
*/
final PyObject basebytes___gt__(PyObject other) {
int cmp = basebytes_cmp(other);
if (cmp > 0) {
return Py.True;
} else if (cmp > -2) {
return Py.False;
} else {
return null;
}
}
/**
* Search for the target in this byte array, returning true if found and false if not. The
* target must either convertible to an integer in the Python byte range, or capable of being
* viewed as a byte array.
*