SlidingWindow 1.1.2

dotnet add package SlidingWindow --version 1.1.2
                    
NuGet\Install-Package SlidingWindow -Version 1.1.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SlidingWindow" Version="1.1.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SlidingWindow" Version="1.1.2" />
                    
Directory.Packages.props
<PackageReference Include="SlidingWindow" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SlidingWindow --version 1.1.2
                    
#r "nuget: SlidingWindow, 1.1.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SlidingWindow@1.1.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SlidingWindow&version=1.1.2
                    
Install as a Cake Addin
#tool nuget:?package=SlidingWindow&version=1.1.2
                    
Install as a Cake Tool

SlidingWindow

SlidingWindow provides an interface to retrieve chunks from a byte array using a sliding window.

Use Cases

SlidingWindow is helpful for use cases that require examination of a sliding window of bytes within a byte array. For instance, SlidingWindow is useful for cryptographic and compression use cases.

How It Works

Initialize SlidingWindow with a byte array over which you would like to iterate using a SlidingWindow.

Specify the desired window size in chunkSize.

Call GetNextChunk() to retrieve the next window of data. As you retrieve chunks, the window will advance by the number of bytes defined in shiftSize.

On the last chunk, if fewer than chunkSize bytes remain, the remaining bytes will be returned. out finalChunk is set to true on the chunk that reaches the end of the data. Any further calls to GetNextChunk() return null, with position set to -1 and finalChunk set to true.

out newData contains the bytes in the returned chunk that were not part of any previously returned chunk. Concatenating newData from every chunk reproduces the original data.

Call AdvanceToNewChunk() to skip the overlap: the next chunk will start immediately after the end of the previous chunk, and all of its bytes will be returned in newData.

Example

Suppose you have the sentence The quick brown fox jumped over the lazy dog..., which is 47 bytes long.

         1         2         3         4         5
12345678901234567890123456789012345678901234567890
The quick brown fox jumped over the lazy dog...

Initializing the Bytes class with this byte array, a chunkSize of 24 and a sliding window of 8 would result in the following:

using SlidingWindow;

string strData = "The quick brown fox jumped over the lazy dog...";
byte[] byteData = Encoding.UTF8.GetBytes(strData);
Bytes slidingWindow = new Bytes(byteData, 24, 8);

int chunks = slidingWindow.ChunkCount();  // 4

long position = 0;        // zero-based index of current chunk
byte[] chunk = null;      // all of the data in the window
byte[] newData = null;    // new data retrieved from the previous chunk
bool finalChunk = false;  // whether or not it's the last chunk

chunk = slidingWindow.GetNextChunk(out position, out newData, out finalChunk);  
// position   : 0
// chunk      : 'The quick brown fox jump'
// newData    : 'The quick brown fox jump'
// finalChunk : false

chunk = slidingWindow.GetNextChunk(out position, out newData, out finalChunk);  
// position   : 8
// chunk      : 'k brown fox jumped over '
// newData    : 'ed over '
// finalChunk : false

chunk = slidingWindow.GetNextChunk(out position, out newData, out finalChunk);  
// position   : 16
// chunk      : 'fox jumped over the lazy'
// newData    : 'the lazy'
// finalChunk : false

chunk = slidingWindow.GetNextChunk(out position, out newData, out finalChunk);  
// position   : 24
// chunk      : 'ed over the lazy dog...'
// newData    : ' dog...'
// finalChunk : true

chunkSize must be greater than zero and must not exceed the length of the supplied data, and shiftSize must be greater than zero and must not exceed chunkSize. Invalid values throw ArgumentException.

Streams

Use the Streams class to apply the same sliding window to a readable stream when the number of bytes to read (contentLength) is known. Reading begins at the stream's current position, and only contentLength bytes are consumed.

using (FileStream fs = new FileStream("data.bin", FileMode.Open, FileAccess.Read))
{
    Streams slidingWindow = new Streams(fs, fs.Length, 24, 8);

    while (true)
    {
        byte[] chunk = slidingWindow.GetNextChunk(out long position, out byte[] newData, out bool finalChunk);
        if (chunk == null) break;
        // process chunk
        if (finalChunk) break;
    }
}

If the stream ends before contentLength bytes have been read, GetNextChunk() throws EndOfStreamException.

Testing

Tests are written once in Test.Shared using Touchstone and can be run with any of three runners:

# Console runner (optionally export results to JSON)
dotnet run --project Test.Automated -f net10.0
dotnet run --project Test.Automated -f net10.0 -- --results results.json

# xUnit
dotnet test Test.Xunit

# NUnit
dotnet test Test.Nunit

Test.Bytes and Test.Streams are interactive console applications for manually exploring the library.

New in v1.1.1

  • Test dependency updates: Touchstone 0.2.0, NUnit 5.0.0, NUnit3TestAdapter 6.3.0, NUnit.Analyzers 4.15.0, xunit.runner.visualstudio 4.0.0, Microsoft.NET.Test.Sdk 18.10.1, coverlet.collector 10.1.0
  • No changes to library behavior or public API

New in v1.1.0

  • Correct finalChunk reporting when the last chunk ends exactly at the end of the data
  • Bytes.GetNextChunk() returns null after the final chunk
  • Streams no longer zero-pads a partial final chunk, handles partial stream reads, and throws EndOfStreamException on truncated streams
  • AdvanceToNewChunk() fixes for newData, BytesRemaining, and dropped trailing data
  • Test infrastructure using Touchstone (console, xUnit, and NUnit runners)

Version History

Please refer to CHANGELOG.md.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SlidingWindow:

Package Downloads
WatsonDedupe

Standalone C# library for deduplication of data.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.2 106 11/18/2026
1.1.1 155 10/3/2026
1.1.0 119 10/3/2026
1.0.8 300 8/15/2026
1.0.7.1 16,450 11/12/2021
1.0.7 18,582 11/15/2020
1.0.6 1,235 9/21/2019
1.0.5 1,920 7/14/2019
1.0.4 1,026 7/13/2019
1.0.3 860 7/13/2019
1.0.2 836 7/12/2019
1.0.1 824 7/12/2019
1.0.0 906 7/12/2019

Test dependency updates. v1.1.0: fixes for final chunk detection, Streams zero padding, partial stream reads, truncated stream hangs, and AdvanceToNewChunk data loss. GetNextChunk returns null after the final chunk.