66import it .unimi .dsi .fastutil .bytes .ByteArrays ;
77import it .unimi .dsi .fastutil .io .MeasurableStream ;
88import it .unimi .dsi .fastutil .io .RepositionableStream ;
9+ import jakarta .validation .constraints .Positive ;
910import jakarta .validation .constraints .PositiveOrZero ;
1011import lombok .val ;
1112
@@ -61,8 +62,7 @@ public class BAOS extends ByteArrayOutputStream implements RepositionableStream,
6162 public byte [] array (){ return buf ; }
6263
6364 public void array (byte [] array ) {
64- buf = array ;
65- count = 0 ; position = 0 ;
65+ buf = array ; count = 0 ; position = 0 ;
6666 }
6767
6868 /** The current writing position. */
@@ -107,14 +107,12 @@ public BAOS (ByteArrayOutputStream baos) {
107107 /// @see #resize(int)
108108 @ CanIgnoreReturnValue
109109 public byte [] trim () {
110- buf = ByteArrays .trim (buf , count );
111- return buf ;
110+ return buf = ByteArrays .trim (buf , count );
112111 }
113112
114113 @ Override
115114 public void write (int b ) {
116- if (position >= buf .length )
117- buf = ByteArrays .grow (buf , position + 1 , count );
115+ grow (1 );
118116 buf [position ++] = (byte )b ;
119117 if (count < position ) count = position ;
120118 }
@@ -123,19 +121,19 @@ public void write (int b) {
123121 public void write (byte [] b , @ PositiveOrZero int off , @ PositiveOrZero int len ) {
124122 //ByteArrays.ensureOffsetLength(b, off, len);
125123 Objects .checkFromIndexSize (off , len , b .length );
126- if (position + len > buf .length )
127- buf = ByteArrays .grow (buf , position + len , position );
128- System .arraycopy (b , off , buf , position , len );
124+ grow (len );
125+ System .arraycopy (b ,off , buf ,position , len );
129126 position += len ;
130127 if (count < position ) count = position ;
131128 }
132129
133130 @ Override
134131 public void position (@ PositiveOrZero long newPosition ) {
135- if (newPosition > Integer .MAX_VALUE ) throw new IllegalArgumentException ("Position too large: " + newPosition );
136- position = (int )newPosition ;
132+ position = (int ) Math .min (newPosition , buf .length );
133+ }
134+ public void writerIndex (@ PositiveOrZero int newPosition ) {
135+ position = Math .min (newPosition , buf .length );
137136 }
138- public void writerIndex (@ PositiveOrZero int newPosition ){ position = newPosition ; }
139137
140138 @ Override public @ PositiveOrZero long position (){ return position ; }
141139 public @ PositiveOrZero int writerIndex (){ return position ; }
@@ -164,13 +162,12 @@ public void resize (@PositiveOrZero int targetCapacity) {
164162 /// @param additionalCapacity the number of bytes to add to the current buffer size
165163 /// @see #size()
166164 /// @see org.springframework.util.ResizableByteArrayOutputStream#grow(int)
167- public void grow (@ PositiveOrZero int additionalCapacity ) {
168- //Assert.isTrue(additionalCapacity >= 0, "Additional capacity must be 0 or higher");
169- if (count < position ) additionalCapacity = additionalCapacity + position - count ;
170- int needed = count + additionalCapacity ;
171- if (needed > buf .length ){// size + new > capacity
172- int newCapacity = (int )Math .min (Math .max ((long )buf .length + (buf .length >> 1 ), needed ), Arrays .MAX_ARRAY_SIZE );
173- resize (newCapacity );
165+ public void grow (@ Positive int len ) {
166+ if (position + len > buf .length ){
167+ int newLength = (int )Math .max (Math .min ((long ) buf .length + ( buf .length >> 1 ), Arrays .MAX_ARRAY_SIZE ), position + len );
168+ val resizedBuffer = new byte [newLength ];
169+ System .arraycopy (buf ,0 , resizedBuffer ,0 , count );
170+ buf = resizedBuffer ;
174171 }
175172 }
176173
@@ -239,8 +236,7 @@ public void writeInt (int v) {
239236
240237 public void writeMedium (int v ) {
241238 write (v >> 16 );
242- write (v >> 8 );
243- write (v );
239+ writeShort (v );
244240 }
245241
246242 public void writeUUID (UUID uuid ) {
@@ -348,8 +344,7 @@ public BAOS append (CharSequence csq, int start, int end) {
348344 final int len = end - start ;
349345 Objects .checkFromIndexSize (start , len , csq .length ());
350346 final int len2 = len << 1 ;
351- if (position + len2 > buf .length )
352- buf = ByteArrays .grow (buf , position + len2 , position );
347+ grow (len2 );
353348
354349 JBytes .DirectByteArrayAccess .copyCharsToByteArray (csq , start , buf , position , len );
355350
0 commit comments