RandomAccessFile library, read object issue

sktanmoy

Active Member
Licensed User
Longtime User
Let me paste the code first

B4X:
'Class module
Sub Class_Globals
   Private UserName As String
   Private UserPin As String
   Private EncPass As String
   
   Dim raf As RandomAccessFile
   
   Dim Dat As String
   Dat = "ptmc.dat"
   
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
   
   
   If File.Exists(File.DirRootExternal, Dat) Then
      File.Copy(File.DirAssets, Dat, File.DirRootExternal, Dat)
   End If
   
   raf.Initialize(File.DirRootExternal, Dat, False)
   
End Sub

Public Sub Write (User As String, Pin As String, Enc As String)

   UserName = User
   UserPin = Pin
   EncPass = Enc
   
   raf.WriteEncryptedObject(User, Enc, raf.CurrentPosition)
   raf.WriteEncryptedObject(Pin, Enc, raf.CurrentPosition)
   
   raf.Close
End Sub

Public Sub ReadUser(EncPIN As String)
   EncPass = EncPIN
   
   Dim user As String
   
   raf.ReadEncryptedObject(EncPIN, raf.CurrentPosition)
   raf.Close
   
   Return user
   
End Sub

Public Sub ReadPIN(EncPIN As String)
   EncPass = EncPIN
   
   Dim pin As String
   
   raf.ReadEncryptedObject(EncPIN, raf.CurrentPosition)
   pin = raf.ReadEncryptedObject(EncPIN, raf.CurrentPosition)
   raf.Close
   
   Return pin
   
End Sub

Write is working but not ReadUser/ReadPIN.

Error is in line raf.ReadEncryptedObject(EncPIN, raf.CurrentPosition). I need your guideline to solve this.
 

sktanmoy

Active Member
Licensed User
Longtime User
Which error do you get?

I recommend you to move raf.Initialize(File.DirRootExternal, Dat, False) into each sub that works with the file. Currently your code can easily close raf and not open it.

Okay, I've separated this part from main app and attached with reply. You can what actually I did.
 
Last edited:
Upvote 0

sktanmoy

Active Member
Licensed User
Longtime User
Error Message

Which error do you get? You should check it in the logs tab (in the right side of the IDE).

The Error in my device
B4X:
"Program paused on line: 47, user = raf.ReadEncryptedObject(EncPIN, raf.CurrentPosition)"

From log, as far I could recover, error message was
B4X:
 "sending message to waiting queue (CallSubDelayed - UpdateStatus)
filehandler_readuser (B4A line: 47)
user = raf.ReadEncryptedObject(EncPIN, raf.CurrentPosition)
java.nio.BufferUnderflowException
   at java.nio.DirectByteBuffer.getInt(DirectByteBuffer.java:152)
   at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.ReadInt(RandomAccessFile.java:138)
   at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readHelper(RandomAccessFile.java:373)
   at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.ReadEncryptedObject(RandomAccessFile.java:370)
   at b4a.example.filehandler._readuser(filehandler.java:135)
   at b4a.example.main._read_click(main.java:354)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:165)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:153)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:149)
   at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:55)
   at android.view.View.performClick(View.java:4084)
   at android.view.View$PerformClick.run(View.java:16966)
   at android.os.Handler.handleCallback(Handler.java:615)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4745)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
   at dalvik.system.NativeStart.main(Native Method)
java.nio.BufferUnderflowException"
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I don't see the problem in your code. This code however seems to be strange:
B4X:
If File.Exists(File.DirRootExternal, Dat) Then
      File.Copy(File.DirAssets, Dat, File.DirRootExternal, Dat)
   End If
You are copying the file if it already exists.

Make sure that you are not using a malformed file. Start with a new empty file.
 
Upvote 0

sktanmoy

Active Member
Licensed User
Longtime User
Ops! That should be
B4X:
If File.Exists(Location, Filename) = False Then

And I'm solved ;)

Thanks for your time.
 
Last edited:
Upvote 0

sktanmoy

Active Member
Licensed User
Longtime User
Actually that was copying file if the file was already there. So no file was there and while I was tried to read, app was broken.
I spent about 5 hours but failed the catch the reason.
Anyway, thanks again.
 
Upvote 0
Top