James Newton-King %!s(int64=5) %!d(string=hai) anos
pai
achega
e36163deed
Modificáronse 1 ficheiros con 11 adicións e 4 borrados
  1. 11 4
      csharp/src/Google.Protobuf/ParsingPrimitives.cs

+ 11 - 4
csharp/src/Google.Protobuf/ParsingPrimitives.cs

@@ -438,8 +438,6 @@ namespace Google.Protobuf
                 throw InvalidProtocolBufferException.NegativeSize();
                 throw InvalidProtocolBufferException.NegativeSize();
             }
             }
 
 
-            ValidateCurrentLimit(ref buffer, ref state, size);
-
             if (size <= state.bufferSize - state.bufferPos)
             if (size <= state.bufferSize - state.bufferPos)
             {
             {
                 // We have all the bytes we need already.
                 // We have all the bytes we need already.
@@ -448,7 +446,16 @@ namespace Google.Protobuf
                 state.bufferPos += size;
                 state.bufferPos += size;
                 return bytes;
                 return bytes;
             }
             }
-            else if (IsDataAvailableInSource(ref state, size))
+
+            return ReadRawBytesSlow(ref buffer, ref state, size);
+        }
+
+        private static byte[] ReadRawBytesSlow(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state, int size)
+        {
+            ValidateCurrentLimit(ref buffer, ref state, size);
+
+            if ((!state.segmentedBufferHelper.TotalLength.HasValue && size < buffer.Length) ||
+                IsDataAvailableInSource(ref state, size))
             {
             {
                 // Reading more bytes than are in the buffer, but not an excessive number
                 // Reading more bytes than are in the buffer, but not an excessive number
                 // of bytes.  We can safely allocate the resulting array ahead of time.
                 // of bytes.  We can safely allocate the resulting array ahead of time.
@@ -490,7 +497,7 @@ namespace Google.Protobuf
                 }
                 }
 
 
                 // OK, got everything.  Now concatenate it all into one buffer.
                 // OK, got everything.  Now concatenate it all into one buffer.
-                byte[] bytes = new byte[size];          
+                byte[] bytes = new byte[size];
                 int newPos = 0;
                 int newPos = 0;
                 foreach (byte[] chunk in chunks)
                 foreach (byte[] chunk in chunks)
                 {
                 {