I have this for reading data from SecureStorage.
How can I improve the read method to only load actual data, array size is variable?
Or can I dispose of the buffer? Because on second read I run into System.OutOfMemory
class Storage
{
private SecureStorageController configurationStorage;
public Storage()
{
configurationStorage = new SecureStorageController(SecureStorage.Configuration);
}
public void Erase() => configurationStorage.Erase();
public void Write(byte[] writeBuffer)
{
Erase();
int blocks = (int)Math.Ceiling((double)writeBuffer.Length / 32);
for (int i = 0; i < blocks; i++)
{
int copySize = writeBuffer.Length - (i * 32);
byte[] block = new byte[32];
Array.Copy(writeBuffer, i * 32, block, 0, (copySize > 32) ? 32 : copySize);
configurationStorage.Write((uint)i, block);
}
}
public byte[] Read()
{
byte[] readBuffer = new byte[131072];
for (int i = 0; i < 4096; i++)
{
byte[] readBlock = new byte[32];
configurationStorage.Read((uint)i, readBlock);
Array.Copy(readBlock, 0, readBuffer, i * 32, 32);
}
return readBuffer;
}
}