mirror of
https://github.com/Caraxi/mare.server.git
synced 2025-12-12 13:47:23 +01:00
adjust stream buffersize based on file size
This commit is contained in:
parent
9caf90406c
commit
152ee81038
1 changed files with 24 additions and 1 deletions
|
|
@ -12,10 +12,33 @@ public sealed class BlockFileDataSubstream : IDisposable
|
|||
|
||||
public BlockFileDataSubstream(FileInfo file)
|
||||
{
|
||||
_dataStreamLazy = new(() => File.Open(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Inheritable));
|
||||
_dataStreamLazy = new(() => File.Open(file.FullName, GetFileStreamOptions(file.Length)));
|
||||
_headerStream = new MemoryStream(Encoding.ASCII.GetBytes("#" + file.Name + ":" + file.Length.ToString(CultureInfo.InvariantCulture) + "#"));
|
||||
}
|
||||
|
||||
private static FileStreamOptions GetFileStreamOptions(long fileSize)
|
||||
{
|
||||
int bufferSize = fileSize switch
|
||||
{
|
||||
<= 128 * 1024 => 0,
|
||||
<= 512 * 1024 => 4096,
|
||||
<= 1 * 1024 * 1024 => 65536,
|
||||
<= 10 * 1024 * 1024 => 131072,
|
||||
<= 100 * 1024 * 1024 => 524288,
|
||||
_ => 1048576
|
||||
};
|
||||
|
||||
FileStreamOptions opts = new()
|
||||
{
|
||||
Mode = FileMode.Open,
|
||||
Access = FileAccess.Read,
|
||||
Share = FileShare.Read | FileShare.Inheritable,
|
||||
BufferSize = bufferSize
|
||||
};
|
||||
|
||||
return opts;
|
||||
}
|
||||
|
||||
public int Read(byte[] inputBuffer, int offset, int count)
|
||||
{
|
||||
int bytesRead = 0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue