Overwriting a file

I’ve brought a binary stream into a Memory Block, Encrypted it then used:
bs.Write(mb.StringValue(0, mb.Size))
This adds the Encrypted version to the end of the file which is not what I’m attempting to do. I’m obviously missing something

Do I need to delete the original file?
Do I need to close the binary stream then reopen it?
Do I need to use bs.Position = 0?
D) none of the above.
TIA

Code ?

bs = BinaryStream.Create ( f , true )

my code is simple
Open, Read, Encrypt, Write
I’ll try
Open Read Close Encrypt Create Write

Thanks Jeff.

You can save a step by just setting bs.Length to 0 before writing back to it.

Open, Read, Encrypt, Length = 0, Write

Hi Kem,
So to understand that properly; (simple example)
File is 10 bytes, the Memory Block is 8 bytes
Setting the Length = 0 then using Write will end up with a File that is now 8 bytes? deleting the 2 extra bytes in the file. OR does Length = 0 delete all the data immediately ?

I’m not sure I see the difference in your alternatives as you will end up with a file of 8 bytes either way. If you just want to erase the contents, set the Length to 0 and close the BinaryStream immediately.

@Kem Tekinay That’s what I was asking, does length = 0 erase the contents of the file. Thanks.

Just remember that “erase” does not mean “overwrite” where the OS is concerned. The original data in your file will probably be somewhere on the disk for someone determined to find it. That’s true in all cases.

If you want to mitigate that, I suppose you could read the file, set the Position to 0, write back an equal length of gibberish, then close the BinaryStream. Then open the file again and write over it. But I doubt that’s guaranteed either. It’s one of the reasons Apple got rid of their “Secure Erase” option.

Thanks, the encrypted is always at least the same length (or 31 bytes longer) than the original and is ‘gibberish’ so it essentially does the same job as the above?

Unless it’s the exact same length, I’d say “no”, but either way it’s not guaranteed.

http://documentation.xojo.com/index.php/BinaryStream.Create

Kem’s right. Set length = 0. No need to close, delete, and create a new file.

Yes. It truncates the file immediately. Setting the length larger writes null bytes (0x00) to the file. So you can quickly erase the bytes in the file with

n = bs.Length
bs.Length = 0
bs.Length = n

I have done something wrong.
Using Jeff’s way, works fine and the file encrypts and decrypts as expected
Using Kem’s way I get a file of null bytes.

Am I supposed to set the length to n before I write the MemoryBlock?

There is another, possibly simpler way: delete the file before creating it again.

If the file is deleted and something goes wrong, e.g. power cut, then the data is lost, so it may be better to put it in the trash or rename it then after the new file is created then delete it.

yeah, always rename the file, create the new file and the delete the old file…

@Tim Hare @Kem Tekinay
Just playing with Text files this works so I’m wondering if this also an answer?

test.txt = “This is my test file”

[code] Dim f As FolderItem = GetFolderItem("…\test.txt")
Dim bs As BinaryStream = BinaryStream.Open(f, True)
Dim s As String = bs.Read(bs.Length)
bs.Length = 5
bs.Position = 0
bs.Write(“Hello”)

bs.Close[/code]

test.txt = “Hello”

You don’t need to set the length to 5. Set Length and Position to 0, then write your data.