123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- using System;
- using System.IO;
- namespace SharpCompress.Common.Tar
- {
- internal class TarReadOnlySubStream : Stream
- {
- private bool _isDisposed;
- private long _amountRead;
- public TarReadOnlySubStream(Stream stream, long bytesToRead)
- {
- Stream = stream;
- BytesLeftToRead = bytesToRead;
- }
- protected override void Dispose(bool disposing)
- {
- if (_isDisposed)
- {
- return;
- }
- _isDisposed = true;
- if (disposing)
- {
- long skipBytes = _amountRead % 512;
- if (skipBytes == 0)
- {
- return;
- }
- skipBytes = 512 - skipBytes;
- if (skipBytes == 0)
- {
- return;
- }
- var buffer = new byte[skipBytes];
- Stream.ReadFully(buffer);
- }
- }
- private long BytesLeftToRead { get; set; }
- public Stream Stream { get; }
- public override bool CanRead => true;
- public override bool CanSeek => false;
- public override bool CanWrite => false;
- public override void Flush()
- {
- throw new NotSupportedException();
- }
- public override long Length => throw new NotSupportedException();
- public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
- public override int Read(byte[] buffer, int offset, int count)
- {
- if (BytesLeftToRead < count)
- {
- count = (int)BytesLeftToRead;
- }
- int read = Stream.Read(buffer, offset, count);
- if (read > 0)
- {
- BytesLeftToRead -= read;
- _amountRead += read;
- }
- return read;
- }
- public override int ReadByte()
- {
- if (BytesLeftToRead <= 0)
- {
- return -1;
- }
- int value = Stream.ReadByte();
- if (value != -1)
- {
- --BytesLeftToRead;
- ++_amountRead;
- }
- return value;
- }
- public override long Seek(long offset, SeekOrigin origin)
- {
- throw new NotSupportedException();
- }
- public override void SetLength(long value)
- {
- throw new NotSupportedException();
- }
- public override void Write(byte[] buffer, int offset, int count)
- {
- throw new NotSupportedException();
- }
- }
- }
|