make more use of stream.CopyTo on (de)compressors, plus several cosmetics

This commit is contained in:
UbitUmarov
2025-06-19 20:11:42 +01:00
parent 6180fe5340
commit 4b1df46f6a
14 changed files with 47 additions and 66 deletions

View File

@@ -78,9 +78,8 @@ namespace OpenSim.Framework.AssetLoader.Filesystem
if (fInfo.Exists)
{
FileStream fStream = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] idata = new byte[numBytes];
BinaryReader br = new BinaryReader(fStream);
idata = br.ReadBytes((int)numBytes);
byte[] idata = br.ReadBytes((int)numBytes);
br.Close();
fStream.Close();
info.Data = idata;

View File

@@ -794,8 +794,8 @@ namespace OpenSim.Framework
public static byte[] ulongToByteArray(ulong uLongValue)
{
return new byte[8]
{
return
[
(byte)(uLongValue >> 56),
(byte)(uLongValue >> 48),
(byte)(uLongValue >> 40),
@@ -804,18 +804,18 @@ namespace OpenSim.Framework
(byte)(uLongValue >> 16),
(byte)(uLongValue >> 8),
(byte)uLongValue
};
];
}
public static byte[] uintToByteArray(uint value)
{
return new byte[4]
{
return
[
(byte)(value >> 24),
(byte)(value >> 16),
(byte)(value >> 8),
(byte)value
};
];
}
static readonly char[] base64Chars = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',

View File

@@ -119,7 +119,7 @@ namespace OpenSim.Framework
// Copy the temporary stream to the network stream
formDataStream.Seek(0, SeekOrigin.Begin);
using (Stream requestStream = request.GetRequestStream())
formDataStream.CopyStream(requestStream, (int)formDataStream.Length);
formDataStream.CopyTo(requestStream, (int)formDataStream.Length);
}
#endregion Stream Writing

View File

@@ -1508,7 +1508,7 @@ namespace OpenSim.Framework
return null;
if (RenderMaterials.overrides is null || RenderMaterials.overrides.Length == 0)
return new byte[] { 0 }; // store so outdated viewer caches can be updated
return [0]; // store so outdated viewer caches can be updated
int nentries = 0;
for (int i = 0; i < RenderMaterials.overrides.Length; i++)
@@ -1517,7 +1517,7 @@ namespace OpenSim.Framework
nentries++;
}
if(nentries == 0)
return new byte[] { 0 };
return [0];
osUTF8 sb = OSUTF8Cached.Acquire();
sb.Append((byte)nentries);

View File

@@ -630,37 +630,34 @@ namespace OpenSim.Framework
Array ret = null;
try
{
using (MemoryStream inp = new MemoryStream((2 * sizeof(Int32)) + (SizeX * SizeY * sizeof(float))))
using (MemoryStream inp = new MemoryStream((2 * sizeof(int)) + (SizeX * SizeY * sizeof(float))))
{
using (BinaryWriter bw = new BinaryWriter(inp))
{
bw.Write((Int32)SizeX);
bw.Write((Int32)SizeY);
bw.Write(SizeX);
bw.Write(SizeY);
for (int yy = 0; yy < SizeY; yy++)
for (int xx = 0; xx < SizeX; xx++)
{
//bw.Write((float)m_heightmap[xx, yy]);
bw.Write(MathF.Round(m_heightmap[xx, yy], 3, MidpointRounding.AwayFromZero));
}
bw.Flush();
inp.Seek(0, SeekOrigin.Begin);
using (MemoryStream outputStream = new MemoryStream())
{
using (GZipStream compressionStream = new GZipStream(outputStream, CompressionMode.Compress))
{
inp.CopyStream(compressionStream, int.MaxValue);
compressionStream.Close();
ret = outputStream.ToArray();
}
}
using MemoryStream outputStream = new MemoryStream();
using GZipStream compressionStream = new(outputStream, CompressionMode.Compress);
inp.CopyTo(compressionStream);
compressionStream.Flush();
ret = outputStream.ToArray();
}
}
m_log.Debug($"{LogHeader} V2DGzip {ret.Length} bytes");
}
catch (Exception ex)
{
m_log.Error($"{LogHeader} V2DGzip error: {ex.Message}");
}
catch {}
m_log.DebugFormat("{0} V2DGzip {1} bytes", LogHeader, ret.Length);
return ret;
}
@@ -787,7 +784,6 @@ namespace OpenSim.Framework
{
using (GZipStream decompressionStream = new GZipStream(inputStream, CompressionMode.Decompress))
{
decompressionStream.Flush();
decompressionStream.CopyTo(outputStream);
}
}

View File

@@ -26,6 +26,7 @@
*/
using System;
using System.Buffers;
using System.Collections;
using System.Collections.Specialized;
using System.Globalization;
@@ -804,18 +805,18 @@ namespace OpenSim.Framework
/// </remarks>
public static int CopyStream(this Stream copyFrom, Stream copyTo, int maximumBytesToCopy)
{
byte[] buffer = new byte[4096];
byte[] buffer = ArrayPool<byte>.Shared.Rent(8196);
int readBytes;
int totalCopiedBytes = 0;
while ((readBytes = copyFrom.Read(buffer, 0, Math.Min(4096, maximumBytesToCopy))) > 0)
while ((readBytes = copyFrom.Read(buffer, 0, Math.Min(8196, maximumBytesToCopy))) > 0)
{
int writeBytes = Math.Min(maximumBytesToCopy, readBytes);
copyTo.Write(buffer, 0, writeBytes);
totalCopiedBytes += writeBytes;
maximumBytesToCopy -= writeBytes;
}
ArrayPool<byte>.Shared.Return(buffer);
return totalCopiedBytes;
}