1919
2020import org .apache .spark .unsafe .Platform ;
2121import org .apache .spark .unsafe .array .ByteArrayMethods ;
22+ import org .apache .spark .unsafe .memory .ByteArrayMemoryBlock ;
23+ import org .apache .spark .unsafe .memory .MemoryBlock ;
2224import org .apache .spark .unsafe .types .UTF8String ;
2325
2426/**
@@ -29,12 +31,12 @@ public class UTF8StringBuilder {
2931
3032 private static final int ARRAY_MAX = ByteArrayMethods .MAX_ROUNDED_ARRAY_LENGTH ;
3133
32- private byte [] buffer ;
33- private int cursor = Platform . BYTE_ARRAY_OFFSET ;
34+ private ByteArrayMemoryBlock buffer ;
35+ private int length = 0 ;
3436
3537 public UTF8StringBuilder () {
3638 // Since initial buffer size is 16 in `StringBuilder`, we set the same size here
37- this .buffer = new byte [ 16 ] ;
39+ this .buffer = new ByteArrayMemoryBlock ( 16 ) ;
3840 }
3941
4042 // Grows the buffer by at least `neededSize`
@@ -45,34 +47,29 @@ private void grow(int neededSize) {
4547 "exceeds size limitation " + ARRAY_MAX );
4648 }
4749 final int length = totalSize () + neededSize ;
48- if (buffer .length < length ) {
50+ if (buffer .size () < length ) {
4951 int newLength = length < ARRAY_MAX / 2 ? length * 2 : ARRAY_MAX ;
50- final byte [] tmp = new byte [newLength ];
51- Platform .copyMemory (
52- buffer ,
53- Platform .BYTE_ARRAY_OFFSET ,
54- tmp ,
55- Platform .BYTE_ARRAY_OFFSET ,
56- totalSize ());
52+ final ByteArrayMemoryBlock tmp = new ByteArrayMemoryBlock (newLength );
53+ MemoryBlock .copyMemory (buffer , tmp , totalSize ());
5754 buffer = tmp ;
5855 }
5956 }
6057
6158 private int totalSize () {
62- return cursor - Platform . BYTE_ARRAY_OFFSET ;
59+ return length ;
6360 }
6461
6562 public void append (UTF8String value ) {
6663 grow (value .numBytes ());
67- value .writeToMemory (buffer , cursor );
68- cursor += value .numBytes ();
64+ value .writeToMemory (buffer . getByteArray (), length + Platform . BYTE_ARRAY_OFFSET );
65+ length += value .numBytes ();
6966 }
7067
7168 public void append (String value ) {
7269 append (UTF8String .fromString (value ));
7370 }
7471
7572 public UTF8String build () {
76- return UTF8String .fromBytes (buffer , 0 , totalSize ());
73+ return UTF8String .fromBytes (buffer . getByteArray () , 0 , totalSize ());
7774 }
7875}
0 commit comments