Android Question Why does this code fail?

hatzisn

Well-Known Member
Licensed User
Longtime User
Here is a code that fails in the second loop:

Code that fails in second loop:
    Dim ins As InputStream
    ins = File.OpenInput(FileFolder, FileName)

    Dim iCount As Int = 0
    Dim lngBytesAv As Long = ins.BytesAvailable
    Log ("Bytes Available:" & lngBytesAv)
   
   
    Dim iBLen As Int = 512
    Do While lngBytesAv >= iCount
        Log(lngBytesAv)
        Log(iCount)
        If lngBytesAv - iCount < 512 Then
            iBLen = lngBytesAv - iCount
            If iBLen = 0 Then
                Exit
            End If
        End If
        Log(iBLen)
       
        Dim bToSend(iBLen) As Byte
        ins.ReadBytes(bToSend, iCount, bToSend.Length)
       
        iCount = iCount + iBLen
        ProcessByteArray(bToSend)
    Loop

It fails with this error (line 306 is the line indicated as highlighed above):

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **

Folder: /storage/emulated/0/Android/data/b4a.example/files/Videos
File Name: trial.mp4
Bytes Available:7830408
7830408
0
512
7830408
512
512
Error occurred on line: 316 (FileLoader)
java.lang.IndexOutOfBoundsException
at java.io.BufferedInputStream.read(BufferedInputStream.java:340)
at anywheresoftware.b4a.objects.streams.File$InputStreamWrapper.ReadBytes(File.java:484)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
at b4a.example.fileloader._readfile(fileloader.java:438)
 
Last edited:

sfsameer

Expert
Licensed User
Longtime User
Here is a code that fails in the second loop:

Code that fails in second loop:
    Dim ins As InputStream
    ins = File.OpenInput(FileFolder, FileName)

    Dim iCount As Int = 0
    Dim lngBytesAv As Long = ins.BytesAvailable
    Log ("Bytes Available:" & lngBytesAv)
  
  
    Dim iBLen As Int = 512
    Do While lngBytesAv >= iCount
        Log(lngBytesAv)
        Log(iCount)
        If lngBytesAv - iCount < 512 Then
            iBLen = lngBytesAv - iCount
            If iBLen = 0 Then
                Exit
            End If
        End If
        Log(iBLen)
      
        Dim bToSend(iBLen) As Byte
        ins.ReadBytes(bToSend, iCount, bToSend.Length)
      
        iCount = iCount + iBLen
        ProcessByteArray(bToSend)
    Loop

It fails with this error (line 306 is the line indicated as highlighed above):

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **

Folder: /storage/emulated/0/Android/data/b4a.example/files/Videos
File Name: trial.mp4
Bytes Available:7830408
7830408
0
512
7830408
512
512
Error occurred on line: 316 (FileLoader)
java.lang.IndexOutOfBoundsException
at java.io.BufferedInputStream.read(BufferedInputStream.java:340)
at anywheresoftware.b4a.objects.streams.File$InputStreamWrapper.ReadBytes(File.java:484)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
at b4a.example.fileloader._sendfile(fileloader.java:438)
Try to Change it to and see if it solves it :
B4X:
Dim bToSend(iBLen) As Byte
ins.ReadBytes(bToSend, iCount, bToSend.Length -1)
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
The bToSend.Length is correct as it is, it's the "iCount" value should be 0 every time, should it not? I think the read buffer resets after every read, so you don't need to track the offset during the read (you may need to for your final buffer, I don't know how you're building it).
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Thank you @sfsameer and @Jeffrey Cameron. Indeed the problem was as @Jeffrey Cameron indicated that I was reading setting the Offset, but each time you read if you set it to 0 reads the correct data because as I can make out the input stream automatically shifts the cursor. If the data read now, is correct it is another issue which I am too tired to check it today.
 
Upvote 0

sfsameer

Expert
Licensed User
Longtime User
Thank you @sfsameer and @Jeffrey Cameron. Indeed the problem was as @Jeffrey Cameron indicated that I was reading setting the Offset, but each time you read if you set it to 0 reads the correct data because as I can make out the input stream automatically shifts the cursor. If the data read now, is correct it is another issue which I am too tired to check it today.
Glad you solved it :)
 
Upvote 0
Top