Skip to content

Commit 0caa4d2

Browse files
authored
Merge pull request #404 from drewnoakes/pool-indexed-capturing-reader-chunks
Pool buffers in IndexedCapturingReader
2 parents 1837bff + 9eb2302 commit 0caa4d2

File tree

7 files changed

+21
-4
lines changed

7 files changed

+21
-4
lines changed

MetadataExtractor/Formats/Png/PngMetadataReader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ private static IEnumerable<Directory> ProcessChunk(PngChunk chunk)
170170
try
171171
{
172172
using var inflaterStream = new DeflateStream(new MemoryStream(compressedProfile), CompressionMode.Decompress);
173-
iccDirectory = new IccReader().Extract(new IndexedCapturingReader(inflaterStream));
173+
using var iccReader = new IndexedCapturingReader(inflaterStream);
174+
iccDirectory = new IccReader().Extract(iccReader);
174175
iccDirectory.Parent = directory;
175176
}
176177
catch (Exception e)

MetadataExtractor/Formats/Tiff/TiffMetadataReader.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public static IReadOnlyList<Directory> ReadMetadata(Stream stream)
4141
var directories = new List<Directory>();
4242

4343
var handler = new ExifTiffHandler(directories, exifStartOffset: 0);
44-
TiffReader.ProcessTiff(new IndexedCapturingReader(stream), handler);
44+
45+
using var reader = new IndexedCapturingReader(stream);
46+
TiffReader.ProcessTiff(reader, handler);
4547

4648
return directories;
4749
}

MetadataExtractor/IO/IndexedCapturingReader.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
22

3+
using System.Buffers;
4+
35
namespace MetadataExtractor.IO
46
{
57
/// <author>Drew Noakes https://drewnoakes.com</author>
6-
public sealed class IndexedCapturingReader : IndexedReader
8+
public sealed class IndexedCapturingReader : IndexedReader, IDisposable
79
{
810
private const int DefaultChunkLength = 2 * 1024;
911

@@ -99,7 +101,7 @@ private bool IsValidIndex(int index, int bytesRequested)
99101
{
100102
Debug.Assert(!_isStreamFinished);
101103

102-
var chunk = new byte[_chunkLength];
104+
var chunk = ArrayPool<byte>.Shared.Rent(_chunkLength);
103105
var totalBytesRead = 0;
104106
while (!_isStreamFinished && totalBytesRead != _chunkLength)
105107
{
@@ -129,6 +131,14 @@ private bool IsValidIndex(int index, int bytesRequested)
129131
return true;
130132
}
131133

134+
public void Dispose()
135+
{
136+
foreach (var chunk in _chunks)
137+
{
138+
ArrayPool<byte>.Shared.Return(chunk);
139+
}
140+
}
141+
132142
public override int ToUnshiftedOffset(int localOffset) => localOffset;
133143

134144
private void GetPosition(int index, out int chunkIndex, out int innerIndex)

MetadataExtractor/PublicAPI/net462/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ MetadataExtractor.Formats.Png.PngDirectory.GetPngChunkType() -> MetadataExtracto
111111
MetadataExtractor.Formats.Png.PngDirectory.PngDirectory(MetadataExtractor.Formats.Png.PngChunkType pngChunkType) -> void
112112
MetadataExtractor.GeoLocation.Equals(MetadataExtractor.GeoLocation other) -> bool
113113
MetadataExtractor.GeoLocation.GeoLocation() -> void
114+
MetadataExtractor.IO.IndexedCapturingReader.Dispose() -> void
114115
MetadataExtractor.IO.IndexedReader.GetByte(int index) -> byte
115116
MetadataExtractor.IO.IndexedReader.GetBytes(int index, int count) -> byte[]!
116117
MetadataExtractor.Util.FileType.Avif = 28 -> MetadataExtractor.Util.FileType

MetadataExtractor/PublicAPI/net8.0/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ MetadataExtractor.Formats.Png.PngDirectory.GetPngChunkType() -> MetadataExtracto
110110
MetadataExtractor.Formats.Png.PngDirectory.PngDirectory(MetadataExtractor.Formats.Png.PngChunkType pngChunkType) -> void
111111
MetadataExtractor.GeoLocation.Equals(MetadataExtractor.GeoLocation other) -> bool
112112
MetadataExtractor.GeoLocation.GeoLocation() -> void
113+
MetadataExtractor.IO.IndexedCapturingReader.Dispose() -> void
113114
MetadataExtractor.IO.IndexedReader.GetByte(int index) -> byte
114115
MetadataExtractor.IO.IndexedReader.GetBytes(int index, int count) -> byte[]!
115116
MetadataExtractor.Util.FileType.Avif = 28 -> MetadataExtractor.Util.FileType

MetadataExtractor/PublicAPI/netstandard1.3/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ MetadataExtractor.Formats.Png.PngDirectory.GetPngChunkType() -> MetadataExtracto
111111
MetadataExtractor.Formats.Png.PngDirectory.PngDirectory(MetadataExtractor.Formats.Png.PngChunkType pngChunkType) -> void
112112
MetadataExtractor.GeoLocation.Equals(MetadataExtractor.GeoLocation other) -> bool
113113
MetadataExtractor.GeoLocation.GeoLocation() -> void
114+
MetadataExtractor.IO.IndexedCapturingReader.Dispose() -> void
114115
MetadataExtractor.IO.IndexedReader.GetByte(int index) -> byte
115116
MetadataExtractor.IO.IndexedReader.GetBytes(int index, int count) -> byte[]!
116117
MetadataExtractor.Util.FileType.Avif = 28 -> MetadataExtractor.Util.FileType

MetadataExtractor/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ MetadataExtractor.Formats.Png.PngDirectory.GetPngChunkType() -> MetadataExtracto
110110
MetadataExtractor.Formats.Png.PngDirectory.PngDirectory(MetadataExtractor.Formats.Png.PngChunkType pngChunkType) -> void
111111
MetadataExtractor.GeoLocation.Equals(MetadataExtractor.GeoLocation other) -> bool
112112
MetadataExtractor.GeoLocation.GeoLocation() -> void
113+
MetadataExtractor.IO.IndexedCapturingReader.Dispose() -> void
113114
MetadataExtractor.IO.IndexedReader.GetByte(int index) -> byte
114115
MetadataExtractor.IO.IndexedReader.GetBytes(int index, int count) -> byte[]!
115116
MetadataExtractor.Util.FileType.Avif = 28 -> MetadataExtractor.Util.FileType

0 commit comments

Comments
 (0)