B4J Question Move the bytes of a file

SMOOTSARA

Active Member
Licensed User
Longtime User
Hello ?
I have a simple photo file

I want to replace the first 5 bytes with the last 5 bytes

I need your guidance to write a sub?

I have progressed to this point

B4X:
Sub Bcoder(fPath As String,fName As String)

    If File.Exists(fPath,fName) Then

        Dim InputStream2 As InputStream
        InputStream2 = File.OpenInput(fPath,fName)    '  "File.DirInternal"  name_file
        
        Dim buffer(5) As Byte
        InputStream2.ReadBytes(buffer,0,buffer.Length)
        InputStream2.Close
 
  ..
  ...
  ....
  .....
 

    
    
End Sub
 

emexes

Expert
Licensed User
You're on the home stretch.

open the file for output
position to file length - 5
write the 5-byte buffer
close the file

One other observation is that if the file is 5 bytes or fewer long, then you don't need to do anything.
 
Upvote 0

SMOOTSARA

Active Member
Licensed User
Longtime User
You're on the home stretch.

open the file for output
position to file length - 5
write the 5-byte buffer
close the file

One other observation is that if the file is 5 bytes or fewer long, then you don't need to do anything.

Hello
I am thankful with your guidance
I don't know how to write and delete bytes in position 5
Can you guide me with the codes?
Thanks
 
Upvote 0

emexes

Expert
Licensed User
and delete bytes in position 5
Good point. And I read your original requirement back-to-front anyway. ?

Do you just want to copy the last 5 bytes of the file, over the top of (ie, overwriting) the first five bytes of the file?

So that then you will have the same 5 bytes both at the start of the file and at the end of the file?
 
Upvote 0

SMOOTSARA

Active Member
Licensed User
Longtime User
This is the story:

I have a photo that I want to hide from the user:rolleyes:

I decided to move the first 5 files with the last 5 files and put in the file folder:cool:

So that it not opened by clicking in file folder;)

Instead, whenever I need it in the program, I change the first and last 5 bytes and the photo opens in aplication
 
Upvote 0

emexes

Expert
Licensed User
So you want to swap the first 5 bytes of the file with the last 5 bytes?

That's even easier, because it means the middle (thousands of) bytes don't need to be moved.

I'll fire up B4J in a moment to check, but broadly speaking:

Pseudocode:
open the file for read-write
if the file is at least 10 bytes long then
    read the first 5 bytes into Buffer1
    read the last 5 bytes into Buffer2
    write Buffer1 into the last 5 bytes
    write Buffer2 into the first 5 bytes
end if
close the file
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Give this a burl ?
B4X:
Sub Bcoder(fPath As String,fName As String)
  
    If File.Exists(fPath, fName) Then
        Dim raf As RandomAccessFile    'tick [j]RandomAccessFile in IDE Library Manager tab
      
        raf.Initialize(fPath, fName, False)   'read-only = false, means read-write
        If raf.Size >= 10 Then
            Dim Buffer1(5) As Byte
            Dim Buffer2(5) As Byte
          
            If raf.ReadBytes(Buffer1, 0, 5, 0) <> 5 Then
                Log("wtf - error reading first 5 bytes")
            else if raf.ReadBytes(Buffer2, 0, 5, raf.Size - 5) <> 5 Then
                Log("wtf - error reading last 5 bytes")
            Else if raf.WriteBytes(Buffer1, 0, 5, raf.Size - 5) <> 5 Then
                Log("wtf - error writing last 5 bytes")
            else if raf.WriteBytes(Buffer2, 0, 5, 0) <> 5 Then
                Log("wtf - error writing first 5 bytes")
            Else
                Log("looking good so far")
            End If
        End If
        raf.Close
    End If

End Sub
 
Last edited:
Upvote 0

SMOOTSARA

Active Member
Licensed User
Longtime User
Give this a burl ?
B4X:
Sub Bcoder(fPath As String,fName As String)
 
    If File.Exists(fPath, fName) Then
        Dim raf As RandomAccessFile    'tick [j]RandomAccessFile in IDE Library Manager tab
     
        raf.Initialize(fPath, fName, False)   'read-only = false, means read-write
        If raf.Size >= 10 Then
            Dim Buffer1(5) As Byte
            Dim Buffer2(5) As Byte
         
            If raf.ReadBytes(Buffer1, 0, 5, 0) <> 5 Then
                Log("wtf - error reading first 5 bytes")
            else if raf.ReadBytes(Buffer2, 0, 5, raf.Size - 5) <> 5 Then
                Log("wtf - error reading last 5 bytes")
            Else if raf.WriteBytes(Buffer1, 0, 5, raf.Size - 5) <> 5 Then
                Log("wtf - error writing last 5 bytes")
            else if raf.WriteBytes(Buffer2, 0, 5, 0) <> 5 Then
                Log("wtf - error writing first 5 bytes")
            Else
                Log("looking good so far")
            End If
        End If
        raf.Close
    End If

End Sub


Dear emexes, thank you for your guidance
It is very accurate and excellent
As a final question, how can I replace only 2 bytes?
For example, byte 6 with byte 95 ?
 
Upvote 0

emexes

Expert
Licensed User
For example, byte 6 with byte 95 ?
Just change the Length parameters to 1 (ie single byte) instead of 5, and the Position parameters to 6 - 1 and 95 - 1 instead of 0 and raf.Size - 5.

Assuming that by eg byte 6, you mean the 6th byte in the file. Unix file bytes are referred to by offset rather than ordinal position, and thus numbered 0, 1, 2, 3, 4, 5 rather than 1, 2, 3, 4, 5, 6.

Also perhaps change the file size check to >= 95 instead of >= 10.

Apparently there is a method to write a single byte to a file, but not a matching method to read a single byte. Odd. Anyway, you could replace the .WriteBytes with .WriteByte to make the code slightly clearer but slightly less general.

1594392580082.png
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Apparently there is a method to write a single byte to a file, but not a matching method to read a single byte. Odd.
Correction: there is a matching method, but hidden further down. Would have thought it would be .ReadByte and the other one .ReadByteUnsigned, but... I'm sure there's a reason why Erel has done it the way it is.

1594393317182.png
 
Upvote 0
Top