Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jchristn/slidingwindow
SlidingWindow provides an interface to retrieve chunks from a byte array using a sliding window.
https://github.com/jchristn/slidingwindow
bytes data processing sliding-window slidingwindow stream
Last synced: 23 days ago
JSON representation
SlidingWindow provides an interface to retrieve chunks from a byte array using a sliding window.
- Host: GitHub
- URL: https://github.com/jchristn/slidingwindow
- Owner: jchristn
- License: mit
- Created: 2019-07-12T06:13:34.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-01-07T16:27:51.000Z (almost 3 years ago)
- Last Synced: 2024-12-08T15:04:21.153Z (28 days ago)
- Topics: bytes, data, processing, sliding-window, slidingwindow, stream
- Language: C#
- Size: 1.83 MB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# 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, and ```out finalChunk``` will be set to true, indicating the end of the byte array.
## 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:
```csharp
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
int 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 chunkchunk = slidingWindow.GetNextChunk(out position, out newData, out finalChunk);
// position : 0
// chunk : 'The quick brown fox jump'
// newData : 'The quick brown fox jump'
// finalChunk : falsechunk = slidingWindow.GetNextChunk(out position, out newData, out finalChunk);
// position : 8
// chunk : 'k brown fox jumped over '
// newData : 'ed over '
// finalChunk : falsechunk = slidingWindow.GetNextChunk(out position, out newData, out finalChunk);
// position : 16
// chunk : 'fox jumped over the lazy'
// newData : 'the lazy'
// finalChunk : falsechunk = 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 less than the length of the supplied data, and ```shiftSize``` must be greater than zero and less than ```chunkSize```.
## New in v1.0.x
- Initial release with support for byte arrays.
- Added support for net452.
- Added support for streams where the content length is known.
- Added out param for position at which the chunk starts from the source byte array or stream.
- Added out param for byte array containing the new data from the previous chunk included in the returned chunk.
## Version HistoryPlease refer to CHANGELOG.md.