Skip to content

Pool buffers in IndexedCapturingReader #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion MetadataExtractor/Formats/Png/PngMetadataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ private static IEnumerable<Directory> ProcessChunk(PngChunk chunk)
try
{
using var inflaterStream = new DeflateStream(new MemoryStream(compressedProfile), CompressionMode.Decompress);
iccDirectory = new IccReader().Extract(new IndexedCapturingReader(inflaterStream));
using var iccReader = new IndexedCapturingReader(inflaterStream);
iccDirectory = new IccReader().Extract(iccReader);
iccDirectory.Parent = directory;
}
catch (Exception e)
Expand Down
4 changes: 3 additions & 1 deletion MetadataExtractor/Formats/Tiff/TiffMetadataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public static IReadOnlyList<Directory> ReadMetadata(Stream stream)
var directories = new List<Directory>();

var handler = new ExifTiffHandler(directories, exifStartOffset: 0);
TiffReader.ProcessTiff(new IndexedCapturingReader(stream), handler);

using var reader = new IndexedCapturingReader(stream);
TiffReader.ProcessTiff(reader, handler);

return directories;
}
Expand Down
14 changes: 12 additions & 2 deletions MetadataExtractor/IO/IndexedCapturingReader.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,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.

using System.Buffers;

namespace MetadataExtractor.IO
{
/// <author>Drew Noakes https://drewnoakes.com</author>
public sealed class IndexedCapturingReader : IndexedReader
public sealed class IndexedCapturingReader : IndexedReader, IDisposable
{
private const int DefaultChunkLength = 2 * 1024;

Expand Down Expand Up @@ -99,7 +101,7 @@ private bool IsValidIndex(int index, int bytesRequested)
{
Debug.Assert(!_isStreamFinished);

var chunk = new byte[_chunkLength];
var chunk = ArrayPool<byte>.Shared.Rent(_chunkLength);
var totalBytesRead = 0;
while (!_isStreamFinished && totalBytesRead != _chunkLength)
{
Expand Down Expand Up @@ -129,6 +131,14 @@ private bool IsValidIndex(int index, int bytesRequested)
return true;
}

public void Dispose()
{
foreach (var chunk in _chunks)
{
ArrayPool<byte>.Shared.Return(chunk);
}
}

public override int ToUnshiftedOffset(int localOffset) => localOffset;

private void GetPosition(int index, out int chunkIndex, out int innerIndex)
Expand Down
1 change: 1 addition & 0 deletions MetadataExtractor/PublicAPI/net462/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ MetadataExtractor.Formats.Png.PngDirectory.GetPngChunkType() -> MetadataExtracto
MetadataExtractor.Formats.Png.PngDirectory.PngDirectory(MetadataExtractor.Formats.Png.PngChunkType pngChunkType) -> void
MetadataExtractor.GeoLocation.Equals(MetadataExtractor.GeoLocation other) -> bool
MetadataExtractor.GeoLocation.GeoLocation() -> void
MetadataExtractor.IO.IndexedCapturingReader.Dispose() -> void
MetadataExtractor.IO.IndexedReader.GetByte(int index) -> byte
MetadataExtractor.IO.IndexedReader.GetBytes(int index, int count) -> byte[]!
MetadataExtractor.Util.FileType.Avif = 28 -> MetadataExtractor.Util.FileType
Expand Down
1 change: 1 addition & 0 deletions MetadataExtractor/PublicAPI/net8.0/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ MetadataExtractor.Formats.Png.PngDirectory.GetPngChunkType() -> MetadataExtracto
MetadataExtractor.Formats.Png.PngDirectory.PngDirectory(MetadataExtractor.Formats.Png.PngChunkType pngChunkType) -> void
MetadataExtractor.GeoLocation.Equals(MetadataExtractor.GeoLocation other) -> bool
MetadataExtractor.GeoLocation.GeoLocation() -> void
MetadataExtractor.IO.IndexedCapturingReader.Dispose() -> void
MetadataExtractor.IO.IndexedReader.GetByte(int index) -> byte
MetadataExtractor.IO.IndexedReader.GetBytes(int index, int count) -> byte[]!
MetadataExtractor.Util.FileType.Avif = 28 -> MetadataExtractor.Util.FileType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ MetadataExtractor.Formats.Png.PngDirectory.GetPngChunkType() -> MetadataExtracto
MetadataExtractor.Formats.Png.PngDirectory.PngDirectory(MetadataExtractor.Formats.Png.PngChunkType pngChunkType) -> void
MetadataExtractor.GeoLocation.Equals(MetadataExtractor.GeoLocation other) -> bool
MetadataExtractor.GeoLocation.GeoLocation() -> void
MetadataExtractor.IO.IndexedCapturingReader.Dispose() -> void
MetadataExtractor.IO.IndexedReader.GetByte(int index) -> byte
MetadataExtractor.IO.IndexedReader.GetBytes(int index, int count) -> byte[]!
MetadataExtractor.Util.FileType.Avif = 28 -> MetadataExtractor.Util.FileType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ MetadataExtractor.Formats.Png.PngDirectory.GetPngChunkType() -> MetadataExtracto
MetadataExtractor.Formats.Png.PngDirectory.PngDirectory(MetadataExtractor.Formats.Png.PngChunkType pngChunkType) -> void
MetadataExtractor.GeoLocation.Equals(MetadataExtractor.GeoLocation other) -> bool
MetadataExtractor.GeoLocation.GeoLocation() -> void
MetadataExtractor.IO.IndexedCapturingReader.Dispose() -> void
MetadataExtractor.IO.IndexedReader.GetByte(int index) -> byte
MetadataExtractor.IO.IndexedReader.GetBytes(int index, int count) -> byte[]!
MetadataExtractor.Util.FileType.Avif = 28 -> MetadataExtractor.Util.FileType
Expand Down