@@ -1504,7 +1504,10 @@ public String readString() throws IOException {
15041504 // TODO(anuraaga): It might be possible to share the optimized loop with
15051505 // readStringRequireUtf8 by implementing Java replacement logic there.
15061506 // The same as readBytes' logic
1507- byte [] bytes = new byte [size ];
1507+ byte [] bytes =
1508+ UnsafeUtil .hasUnsafeAllocateArrayOperation ()
1509+ ? UnsafeUtil .allocateUninitializedArray (size )
1510+ : new byte [size ];
15081511 UnsafeUtil .copyMemory (pos , bytes , 0 , size );
15091512 String result = new String (bytes , UTF_8 );
15101513 pos += size ;
@@ -1621,7 +1624,10 @@ public ByteString readBytes() throws IOException {
16211624 return ByteString .wrap (result );
16221625 } else {
16231626 // Use UnsafeUtil to copy the memory to bytes instead of using ByteBuffer ways.
1624- byte [] bytes = new byte [size ];
1627+ byte [] bytes =
1628+ UnsafeUtil .hasUnsafeAllocateArrayOperation ()
1629+ ? UnsafeUtil .allocateUninitializedArray (size )
1630+ : new byte [size ];
16251631 UnsafeUtil .copyMemory (pos , bytes , 0 , size );
16261632 pos += size ;
16271633 return ByteString .wrap (bytes );
@@ -1655,7 +1661,10 @@ public ByteBuffer readByteBuffer() throws IOException {
16551661 return result ;
16561662 } else {
16571663 // The same as readBytes' logic
1658- byte [] bytes = new byte [size ];
1664+ byte [] bytes =
1665+ UnsafeUtil .hasUnsafeAllocateArrayOperation ()
1666+ ? UnsafeUtil .allocateUninitializedArray (size )
1667+ : new byte [size ];
16591668 UnsafeUtil .copyMemory (pos , bytes , 0 , size );
16601669 pos += size ;
16611670 return ByteBuffer .wrap (bytes );
@@ -3348,7 +3357,10 @@ public boolean readBool() throws IOException {
33483357 public String readString () throws IOException {
33493358 final int size = readRawVarint32 ();
33503359 if (size > 0 && size <= currentByteBufferLimit - currentByteBufferPos ) {
3351- byte [] bytes = new byte [size ];
3360+ byte [] bytes =
3361+ UnsafeUtil .hasUnsafeAllocateArrayOperation ()
3362+ ? UnsafeUtil .allocateUninitializedArray (size )
3363+ : new byte [size ];
33523364 UnsafeUtil .copyMemory (currentByteBufferPos , bytes , 0 , size );
33533365 String result = new String (bytes , UTF_8 );
33543366 currentByteBufferPos += size ;
@@ -3476,8 +3488,10 @@ public ByteString readBytes() throws IOException {
34763488 currentByteBufferPos += size ;
34773489 return result ;
34783490 } else {
3479- byte [] bytes ;
3480- bytes = new byte [size ];
3491+ byte [] bytes =
3492+ UnsafeUtil .hasUnsafeAllocateArrayOperation ()
3493+ ? UnsafeUtil .allocateUninitializedArray (size )
3494+ : new byte [size ];
34813495 UnsafeUtil .copyMemory (currentByteBufferPos , bytes , 0 , size );
34823496 currentByteBufferPos += size ;
34833497 return ByteString .wrap (bytes );
@@ -3512,7 +3526,10 @@ public ByteBuffer readByteBuffer() throws IOException {
35123526 (int ) (currentByteBufferPos - currentAddress - size ),
35133527 (int ) (currentByteBufferPos - currentAddress ));
35143528 } else {
3515- byte [] bytes = new byte [size ];
3529+ byte [] bytes =
3530+ UnsafeUtil .hasUnsafeAllocateArrayOperation ()
3531+ ? UnsafeUtil .allocateUninitializedArray (size )
3532+ : new byte [size ];
35163533 UnsafeUtil .copyMemory (currentByteBufferPos , bytes , 0 , size );
35173534 currentByteBufferPos += size ;
35183535 return ByteBuffer .wrap (bytes );
@@ -3793,7 +3810,10 @@ public byte readRawByte() throws IOException {
37933810 @ Override
37943811 public byte [] readRawBytes (final int length ) throws IOException {
37953812 if (length >= 0 && length <= currentRemaining ()) {
3796- byte [] bytes = new byte [length ];
3813+ byte [] bytes =
3814+ UnsafeUtil .hasUnsafeAllocateArrayOperation ()
3815+ ? UnsafeUtil .allocateUninitializedArray (length )
3816+ : new byte [length ];
37973817 UnsafeUtil .copyMemory (currentByteBufferPos , bytes , 0 , length );
37983818 currentByteBufferPos += length ;
37993819 return bytes ;
0 commit comments