@@ -130,34 +130,37 @@ void FileAccessCompressed::_close() {
130130 f->store_32 (0 ); // compressed sizes, will update later
131131 }
132132
133- Vector<int > block_sizes;
133+ uint32_t last_block_size = write_max % block_size;
134+
135+ // Temporary buffer for compressed data blocks.
136+ LocalVector<uint8_t > temp_cblock;
137+ temp_cblock.resize (Compression::get_max_compressed_buffer_size (bc == 1 ? last_block_size : block_size, cmode));
138+ uint8_t *temp_cblock_ptr = temp_cblock.ptr ();
139+
140+ // Compress and store the blocks.
141+ LocalVector<uint32_t > block_sizes;
134142 for (uint32_t i = 0 ; i < bc; i++) {
135- uint32_t bl = i == (bc - 1 ) ? write_max % block_size : block_size;
143+ uint32_t bl = i == (bc - 1 ) ? last_block_size : block_size;
136144 uint8_t *bp = &write_ptr[i * block_size];
137145
138- Vector<uint8_t > cblock;
139- cblock.resize (Compression::get_max_compressed_buffer_size (bl, cmode));
140- const int64_t compressed_size = Compression::compress (cblock.ptrw (), bp, bl, cmode);
146+ const int64_t compressed_size = Compression::compress (temp_cblock_ptr, bp, bl, cmode);
141147 ERR_FAIL_COND_MSG (compressed_size < 0 , " FileAccessCompressed: Error compressing data." );
142148
143- f->store_buffer (cblock. ptr () , (uint64_t )compressed_size);
149+ f->store_buffer (temp_cblock_ptr , (uint64_t )compressed_size);
144150 block_sizes.push_back (compressed_size);
145151 }
146152
147153 f->seek (16 ); // ok write block sizes
148154 for (uint32_t i = 0 ; i < bc; i++) {
149- f->store_32 (uint32_t ( block_sizes[i]) );
155+ f->store_32 (block_sizes[i]);
150156 }
151157 f->seek_end ();
152158 f->store_buffer ((const uint8_t *)mgc.get_data (), mgc.length ()); // magic at the end too
153-
154- buffer.clear ();
155-
156159 } else {
157160 comp_buffer.clear ();
158- buffer.clear ();
159161 read_blocks.clear ();
160162 }
163+ buffer.clear ();
161164 f.unref ();
162165}
163166
0 commit comments