gmadsharp/src/addoncreator/Addon/SegmentedAddonFileInfo.cs

55 lines
1.5 KiB
C#
Raw Normal View History

2014-10-22 13:47:38 +00:00
using System;
using System.IO;
namespace GarrysMod.AddonCreator.Addon
2014-10-22 13:47:38 +00:00
{
public class SegmentedAddonFileInfo : AddonFileInfo
{
2014-10-22 17:48:30 +00:00
private readonly long _len;
private readonly long _pos;
private readonly Stream _stream;
2014-10-22 13:47:38 +00:00
private int _hash;
public SegmentedAddonFileInfo(Stream stream, long pos, long len, int fileHash)
{
_stream = stream;
_pos = pos;
_len = len;
_hash = fileHash;
}
public override long Size
{
get
{
return _len;
}
}
public override int Crc32Hash
{
get { return _hash; }
}
2014-10-22 13:47:38 +00:00
public override byte[] GetContents()
{
lock (_stream)
{
var output = new byte[_len];
var oldpos = _stream.Position;
_stream.Position = _pos;
for (long i = 0; i < _len; i += int.MaxValue) // for-loop for supporting long file sizes
2014-10-22 13:47:38 +00:00
{
var toRead = (int) Math.Min(int.MaxValue, _len);
var buffer = new byte[toRead];
var readReal = _stream.Read(buffer, 0, toRead);
buffer.CopyTo(output, i);
i -= (toRead - readReal); // make absolutely sure everything gets read
2014-10-22 13:47:38 +00:00
}
_stream.Position = oldpos;
2014-10-22 13:47:38 +00:00
return output;
}
}
}
}