Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace SharpCompress.IO
{
internal class ReadOnlyAppendingStream : Stream
{
private readonly Queue<Stream> streams;
private Stream current;
public ReadOnlyAppendingStream(IEnumerable<Stream> streams)
{
this.streams = new Queue<Stream>(streams);
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return false; }
}
public override void Flush()
{
throw new NotImplementedException();
}
public override long Length
{
get { throw new NotImplementedException(); }
}
public override long Position
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public override int Read(byte[] buffer, int offset, int count)
{
if (current == null && streams.Count == 0)
{
return -1;
}
if (current == null)
{
current = streams.Dequeue();
}
int totalRead = 0;
while (totalRead < count)
{
int read = current.Read(buffer, offset + totalRead, count - totalRead);
if (read <= 0)
{
if (streams.Count == 0)
{
return totalRead;
}
else
{
current = streams.Dequeue();
}
}
totalRead += read;
}
return totalRead;
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,68 @@
using System;
using System.IO;
namespace SharpCompress.IO
{
internal class CountingWritableSubStream : Stream
{
private Stream writableStream;
internal CountingWritableSubStream(Stream stream)
{
writableStream = stream;
}
public uint Count { get; private set; }
public override bool CanRead
{
get { return false; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return true; }
}
public override void Flush()
{
}
public override long Length
{
get { throw new NotImplementedException(); }
}
public override long Position
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
writableStream.Write(buffer, offset, count);
Count += (uint) count;
}
}
}

View File

@ -0,0 +1,81 @@
using System.IO;
using SharpCompress.Common;
namespace SharpCompress.IO
{
internal class ListeningStream : Stream
{
private long currentEntryTotalReadBytes;
private IExtractionListener listener;
public ListeningStream(IExtractionListener listener, Stream stream)
{
Stream = stream;
this.listener = listener;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
Stream.Dispose();
}
}
public Stream Stream { get; private set; }
public override bool CanRead
{
get { return Stream.CanRead; }
}
public override bool CanSeek
{
get { return Stream.CanSeek; }
}
public override bool CanWrite
{
get { return Stream.CanWrite; }
}
public override void Flush()
{
Stream.Flush();
}
public override long Length
{
get { return Stream.Length; }
}
public override long Position
{
get { return Stream.Position; }
set { Stream.Position = value; }
}
public override int Read(byte[] buffer, int offset, int count)
{
int read = Stream.Read(buffer, offset, count);
currentEntryTotalReadBytes += read;
listener.FireCompressedBytesRead(currentEntryTotalReadBytes, currentEntryTotalReadBytes);
return read;
}
public override long Seek(long offset, SeekOrigin origin)
{
return Stream.Seek(offset, origin);
}
public override void SetLength(long value)
{
Stream.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
Stream.Write(buffer, offset, count);
}
}
}

View File

@ -0,0 +1,136 @@
using System;
using System.IO;
using System.Linq;
namespace SharpCompress.IO
{
internal class MarkingBinaryReader : BinaryReader
{
public MarkingBinaryReader(Stream stream)
: base(stream)
{
}
public long CurrentReadByteCount { get; private set; }
public void Mark()
{
CurrentReadByteCount = 0;
}
public override int Read()
{
throw new NotImplementedException();
}
public override int Read(byte[] buffer, int index, int count)
{
throw new NotImplementedException();
}
public override int Read(char[] buffer, int index, int count)
{
throw new NotImplementedException();
}
public override bool ReadBoolean()
{
return BitConverter.ToBoolean(ReadBytes(1), 0);
}
public override byte ReadByte()
{
return ReadBytes(1).Single();
}
public override byte[] ReadBytes(int count)
{
CurrentReadByteCount += count;
var bytes = base.ReadBytes(count);
if (bytes.Length != count)
{
throw new EndOfStreamException(string.Format("Could not read the requested amount of bytes. End of stream reached. Requested: {0} Read: {1}", count, bytes.Length));
}
return bytes;
}
public override char ReadChar()
{
throw new NotImplementedException();
}
public override char[] ReadChars(int count)
{
throw new NotImplementedException();
}
#if !PORTABLE
public override decimal ReadDecimal()
{
return ByteArrayToDecimal(ReadBytes(16), 0);
}
private decimal ByteArrayToDecimal(byte[] src, int offset)
{
//http://stackoverflow.com/a/16984356/385387
var i1 = BitConverter.ToInt32(src, offset);
var i2 = BitConverter.ToInt32(src, offset + 4);
var i3 = BitConverter.ToInt32(src, offset + 8);
var i4 = BitConverter.ToInt32(src, offset + 12);
return new decimal(new[] { i1, i2, i3, i4 });
}
#endif
public override double ReadDouble()
{
return BitConverter.ToDouble(ReadBytes(8), 0);
}
public override short ReadInt16()
{
return BitConverter.ToInt16(ReadBytes(2), 0);
}
public override int ReadInt32()
{
return BitConverter.ToInt32(ReadBytes(4), 0);
}
public override long ReadInt64()
{
return BitConverter.ToInt64(ReadBytes(8), 0);
}
public override sbyte ReadSByte()
{
return (sbyte)ReadByte();
}
public override float ReadSingle()
{
return BitConverter.ToSingle(ReadBytes(4), 0);
}
public override string ReadString()
{
throw new NotImplementedException();
}
public override ushort ReadUInt16()
{
return BitConverter.ToUInt16(ReadBytes(2), 0);
}
public override uint ReadUInt32()
{
return BitConverter.ToUInt32(ReadBytes(4), 0);
}
public override ulong ReadUInt64()
{
return BitConverter.ToUInt64(ReadBytes(8), 0);
}
}
}

View File

@ -0,0 +1,70 @@
using System.IO;
namespace SharpCompress.IO
{
internal class NonDisposingStream : Stream
{
public NonDisposingStream(Stream stream)
{
Stream = stream;
}
protected override void Dispose(bool disposing)
{
//don't dispose anything
}
public Stream Stream { get; private set; }
public override bool CanRead
{
get { return Stream.CanRead; }
}
public override bool CanSeek
{
get { return Stream.CanSeek; }
}
public override bool CanWrite
{
get { return Stream.CanWrite; }
}
public override void Flush()
{
Stream.Flush();
}
public override long Length
{
get { return Stream.Length; }
}
public override long Position
{
get { return Stream.Position; }
set { Stream.Position = value; }
}
public override int Read(byte[] buffer, int offset, int count)
{
return Stream.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
return Stream.Seek(offset, origin);
}
public override void SetLength(long value)
{
Stream.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
Stream.Write(buffer, offset, count);
}
}
}

View File

@ -0,0 +1,94 @@
using System.IO;
namespace SharpCompress.IO
{
internal class ReadOnlySubStream : Stream
{
public ReadOnlySubStream(Stream stream, long bytesToRead)
: this(stream, null, bytesToRead)
{
}
public ReadOnlySubStream(Stream stream, long? origin, long bytesToRead)
{
Stream = stream;
if (origin != null)
{
stream.Position = origin.Value;
}
BytesLeftToRead = bytesToRead;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
//Stream.Dispose();
}
}
private long BytesLeftToRead { get; set; }
public Stream Stream { get; private set; }
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return false; }
}
public override void Flush()
{
throw new System.NotImplementedException();
}
public override long Length
{
get { throw new System.NotImplementedException(); }
}
public override long Position
{
get { throw new System.NotImplementedException(); }
set { throw new System.NotImplementedException(); }
}
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;
}
return read;
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new System.NotImplementedException();
}
public override void SetLength(long value)
{
throw new System.NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new System.NotImplementedException();
}
}
}

View File

@ -0,0 +1,163 @@
using System.IO;
namespace SharpCompress.IO
{
internal class RewindableStream : Stream
{
private readonly Stream stream;
private MemoryStream bufferStream = new MemoryStream();
private bool isRewound;
private bool isDisposed;
public RewindableStream(Stream stream)
{
this.stream = stream;
}
internal bool IsRecording { get; private set; }
protected override void Dispose(bool disposing)
{
if (isDisposed)
{
return;
}
isDisposed = true;
base.Dispose(disposing);
if (disposing)
{
stream.Dispose();
}
}
public void Rewind(bool stopRecording)
{
isRewound = true;
IsRecording = !stopRecording;
bufferStream.Position = 0;
}
public void Rewind(MemoryStream buffer)
{
if (bufferStream.Position >= buffer.Length)
{
bufferStream.Position -= buffer.Length;
}
else
{
bufferStream.TransferTo(buffer);
bufferStream = buffer;
bufferStream.Position = 0;
}
isRewound = true;
}
public void StartRecording()
{
//if (isRewound && bufferStream.Position != 0)
// throw new System.NotImplementedException();
if (bufferStream.Position != 0)
{
byte[] data = bufferStream.ToArray();
long position = bufferStream.Position;
bufferStream.SetLength(0);
bufferStream.Write(data, (int)position, data.Length - (int)position);
bufferStream.Position = 0;
}
IsRecording = true;
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return false; }
}
public override void Flush()
{
throw new System.NotImplementedException();
}
public override long Length
{
get { throw new System.NotImplementedException(); }
}
public override long Position
{
get { return stream.Position + bufferStream.Position - bufferStream.Length; }
set
{
if (!isRewound)
{
stream.Position = value;
}
else if (value < stream.Position - bufferStream.Length || value >= stream.Position)
{
stream.Position = value;
isRewound = false;
bufferStream.SetLength(0);
}
else
{
bufferStream.Position = value - stream.Position + bufferStream.Length;
}
}
}
public override int Read(byte[] buffer, int offset, int count)
{
int read;
if (isRewound && bufferStream.Position != bufferStream.Length)
{
read = bufferStream.Read(buffer, offset, count);
if (read < count)
{
int tempRead = stream.Read(buffer, offset + read, count - read);
if (IsRecording)
{
bufferStream.Write(buffer, offset + read, tempRead);
}
read += tempRead;
}
if (bufferStream.Position == bufferStream.Length && !IsRecording)
{
isRewound = false;
bufferStream.SetLength(0);
}
return read;
}
read = stream.Read(buffer, offset, count);
if (IsRecording)
{
bufferStream.Write(buffer, offset, read);
}
return read;
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new System.NotImplementedException();
}
public override void SetLength(long value)
{
throw new System.NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new System.NotImplementedException();
}
}
}

View File

@ -0,0 +1,8 @@
namespace SharpCompress.IO
{
internal enum StreamingMode
{
Streaming,
Seekable,
}
}