Android Question Reading File and sending it via Socket

nobbi59

Active Member
Licensed User
Longtime User
Hello,

I've got a problem reading a file in chunks and sending it via socket.

This is my code:

B4X:
Sub ReadFile(Dir As String, FileName As String)
   Try
   Dir = Dir & "/"
   Log("Getting file:" & Dir & FileName)
   Dim size As Long = File.Size(Dir,FileName)
  
   Dim f As RandomAccessFile
   f.Initialize(Dir,FileName,True)
   Log(f.Size)
  
   Do While f.CurrentPosition < size
         Dim part() As Byte
      f.ReadBytes(part,0,1024,f.CurrentPosition)
      astream2.Write(part)
   Loop
  
   f.Close
   astream2.Write("EOFEOFEOFEOFEOFX".GetBytes("UTF-8"))
   Catch
          Dim err As String = "notfound " & LastException.Message
          astream2.Write(err.GetBytes("UTF-8"))
   End Try
End Sub

It always throws this error: (ArrayIndexOutOfBoundsException) java.lang.ArrayIndexOutOfBoundsException: length=0; regionStart=0; regionLength=1024

The file exists, so thats not the problem.

I hope someone can help me.

Best regards
 

nobbi59

Active Member
Licensed User
Longtime User
Hi Erel,

The file size is different each time i send it. I use this app for debugging and wrote a server programm to access the filesystem. Everything works fine but not uploading a file.

Im sending it to my server via Socket. On the server there is a python program running. But anyway the problem Seems to be reading the file, not sending it.
 
Upvote 0

nobbi59

Active Member
Licensed User
Longtime User
I dont want to read it into memory because that takes time and i dont want that.

Im splitting it, so i can directly send the packages and do not have to wait until its fully loaded into memory.

On the server, i receive the "notfount" and the error mentioned in the first post.
 
Upvote 0
Top