@@ -739,21 +739,33 @@ private void WriteTextChunks(Stream stream, PngMetadata meta)
739739 byte [ ] translatedKeyword = PngConstants . TranslatedEncoding . GetBytes ( textData . TranslatedKeyword ) ;
740740 byte [ ] languageTag = PngConstants . LanguageEncoding . GetBytes ( textData . LanguageTag ) ;
741741
742- Span < byte > outputBytes = new byte [ keywordBytes . Length + textBytes . Length +
743- translatedKeyword . Length + languageTag . Length + 5 ] ;
744- keywordBytes . CopyTo ( outputBytes ) ;
745- if ( textData . Value . Length > this . options . TextCompressionThreshold )
742+ int payloadLength = keywordBytes . Length + textBytes . Length + translatedKeyword . Length + languageTag . Length + 5 ;
743+ using ( IMemoryOwner < byte > owner = this . memoryAllocator . Allocate < byte > ( payloadLength ) )
746744 {
747- // Indicate that the text is compressed.
748- outputBytes [ keywordBytes . Length + 1 ] = 1 ;
749- }
745+ Span < byte > outputBytes = owner . GetSpan ( ) ;
746+ keywordBytes . CopyTo ( outputBytes ) ;
747+ int bytesWritten = keywordBytes . Length ;
748+ outputBytes [ bytesWritten ++ ] = 0 ;
749+ if ( textData . Value . Length > this . options . TextCompressionThreshold )
750+ {
751+ // Indicate that the text is compressed.
752+ outputBytes [ bytesWritten ++ ] = 1 ;
753+ }
754+ else
755+ {
756+ outputBytes [ bytesWritten ++ ] = 0 ;
757+ }
750758
751- int keywordStart = keywordBytes . Length + 3 ;
752- languageTag . CopyTo ( outputBytes . Slice ( keywordStart ) ) ;
753- int translatedKeywordStart = keywordStart + languageTag . Length + 1 ;
754- translatedKeyword . CopyTo ( outputBytes . Slice ( translatedKeywordStart ) ) ;
755- textBytes . CopyTo ( outputBytes . Slice ( translatedKeywordStart + translatedKeyword . Length + 1 ) ) ;
756- this . WriteChunk ( stream , PngChunkType . InternationalText , outputBytes . ToArray ( ) ) ;
759+ outputBytes [ bytesWritten ++ ] = 0 ;
760+ languageTag . CopyTo ( outputBytes . Slice ( bytesWritten ) ) ;
761+ bytesWritten += languageTag . Length ;
762+ outputBytes [ bytesWritten ++ ] = 0 ;
763+ translatedKeyword . CopyTo ( outputBytes . Slice ( bytesWritten ) ) ;
764+ bytesWritten += translatedKeyword . Length ;
765+ outputBytes [ bytesWritten ++ ] = 0 ;
766+ textBytes . CopyTo ( outputBytes . Slice ( bytesWritten ) ) ;
767+ this . WriteChunk ( stream , PngChunkType . InternationalText , outputBytes ) ;
768+ }
757769 }
758770 else
759771 {
@@ -762,19 +774,32 @@ private void WriteTextChunks(Stream stream, PngMetadata meta)
762774 // Write zTXt chunk.
763775 byte [ ] compressedData =
764776 this . GetCompressedTextBytes ( PngConstants . Encoding . GetBytes ( textData . Value ) ) ;
765- Span < byte > outputBytes = new byte [ textData . Keyword . Length + compressedData . Length + 2 ] ;
766- PngConstants . Encoding . GetBytes ( textData . Keyword ) . CopyTo ( outputBytes ) ;
767- compressedData . CopyTo ( outputBytes . Slice ( textData . Keyword . Length + 2 ) ) ;
768- this . WriteChunk ( stream , PngChunkType . CompressedText , outputBytes . ToArray ( ) ) ;
777+ int payloadLength = textData . Keyword . Length + compressedData . Length + 2 ;
778+ using ( IMemoryOwner < byte > owner = this . memoryAllocator . Allocate < byte > ( payloadLength ) )
779+ {
780+ Span < byte > outputBytes = owner . GetSpan ( ) ;
781+ PngConstants . Encoding . GetBytes ( textData . Keyword ) . CopyTo ( outputBytes ) ;
782+ int bytesWritten = textData . Keyword . Length ;
783+ outputBytes [ bytesWritten ++ ] = 0 ;
784+ outputBytes [ bytesWritten ++ ] = 0 ;
785+ compressedData . CopyTo ( outputBytes . Slice ( bytesWritten ) ) ;
786+ this . WriteChunk ( stream , PngChunkType . CompressedText , outputBytes . ToArray ( ) ) ;
787+ }
769788 }
770789 else
771790 {
772791 // Write tEXt chunk.
773- Span < byte > outputBytes = new byte [ textData . Keyword . Length + textData . Value . Length + 1 ] ;
774- PngConstants . Encoding . GetBytes ( textData . Keyword ) . CopyTo ( outputBytes ) ;
775- PngConstants . Encoding . GetBytes ( textData . Value )
776- . CopyTo ( outputBytes . Slice ( textData . Keyword . Length + 1 ) ) ;
777- this . WriteChunk ( stream , PngChunkType . Text , outputBytes . ToArray ( ) ) ;
792+ int payloadLength = textData . Keyword . Length + textData . Value . Length + 1 ;
793+ using ( IMemoryOwner < byte > owner = this . memoryAllocator . Allocate < byte > ( payloadLength ) )
794+ {
795+ Span < byte > outputBytes = owner . GetSpan ( ) ;
796+ PngConstants . Encoding . GetBytes ( textData . Keyword ) . CopyTo ( outputBytes ) ;
797+ int bytesWritten = textData . Keyword . Length ;
798+ outputBytes [ bytesWritten ++ ] = 0 ;
799+ PngConstants . Encoding . GetBytes ( textData . Value )
800+ . CopyTo ( outputBytes . Slice ( bytesWritten ) ) ;
801+ this . WriteChunk ( stream , PngChunkType . Text , outputBytes . ToArray ( ) ) ;
802+ }
778803 }
779804 }
780805 }
0 commit comments