Android Question aSyncStream flush and sync

Rubens Jr.

Member
Licensed User
Hi !
Sorry, english is not my language ..

Reading the post "aSyncStream and flush" by Star-dust in Mar, 9, 23 (I dont know how to put a link here) he post a way to flush data on a asyncstream :
B4X:
Private socket As Socket
Private astream As AsyncStreams
Private r As Reflector
Private aout As JavaObject

Sub Button1_Click
    socket.Initialize("socket")
    s.Connect(Addr,Port,3000)
End Sub

Private Sub socket_Connected (Successful As Boolean)
    If Successful Then
        astream.Initialize(socket.InputStream,socket.OutputStream,"astream")
            
        r.Target=astream
        r.Target=r.GetField("aout")
        'Log(r.GetField("working"))
        aout=r.GetField("out")

        astream.Write("MyText".GetBytes("UTF8"))
        aout.RunMethod("flush",Null) ' force send
    End If
End Sub

Private Sub astream_NewData (Buffer() As Byte)
    Log("* " & Buffer.Length)
End Sub

Private Sub astream_Terminated
 
End Sub

Private Sub astream_Error
 
End Sub

How can I add a sync after flush ?
Example (part) in Java from https://www.tutorialspoint.com/java/io/filedescriptor_sync.htm
B4X:
       try {
         fos = new FileOutputStream("test.txt");
         fd = fos.getFD();
// writes byte to file output stream
         fos.write(b);
// flush data from the stream into the buffer
         fos.flush();
// confirms data to be written to the disk
         fd.sync();

I supose that have to get the File descritor from aout and call method sync ... but I dont know how to do that ...

In Time : Thanks Star-dust :)

Thanks in advance

Rubens Jr.
 

Rubens Jr.

Member
Licensed User
I don't recommend calling flush like this. You will cause a race condition. Which problem are you trying to solve?
Hi Erel

Thanks to respond :)

I have a tablet with no battery. It is powered by AC adaptor/5V. Android thinks it is allways 100% battery.
When I type some data and save it to disk ... if the power is down : no data was writen to disk or garbage data was writen ...
So I have to ensure that Android effectively had written the data to disk ... no buffer at all.

I 'solved' this problem with JAVA inline :

B4X:
Sub Process_Globals
    
    Private NativeMe As JavaObject
    
End Sub

Sub Activity_Create(FirstTime As Boolean)

    NativeMe.InitializeContext
    
End Sub

Public Sub gravar_sync (arquivo As String, dados_txt As String)

    Dim    nome_completo As String
    
    nome_completo = File.Combine (File.DirInternal, arquivo)
    NativeMe.Runmethod ("java_gravar_com_sync", Array (nome_completo, dados_txt.GetBytes ("UTF8")))
    
End Sub

#If JAVA

public void java_gravar_com_sync (String nome_arquivo, byte [] b)

import java.io.FileDescriptor;
import java.io.FileOutputStream;

    {
          FileOutputStream fos = null;
          FileDescriptor fd = null;    
          try {
            fos = new FileOutputStream (nome_arquivo);
              fd = fos.getFD ();
             fos.write (b);               
               fos.flush ();               
             fd.sync ();            
             fos.close ();               
            }
        catch (Exception e)
            {
            BA.Log ("Erro no java ...");
             e.printStackTrace ();    
             }
        BA.Log ("Arquivo " + nome_arquivo + " gravado ok.");
      }
  
#End If

I tested this code several times : write some data and power off immediately.
When I power up again : the data was written with no error or garbage.
I dont know if this have 'colateral effects' but it is working ... I think :)

Rubens Jr
 
Upvote 0

Rubens Jr.

Member
Licensed User
Why are you using AsyncStreams to write to a file?

Just use the standard File methods.
Hi Erel
Thanks again to respond.

I used :
B4X:
Public Sub gravar_dados

    Dim Writer As TextWriter

     Writer.Initialize (File.OpenOutput (File.DirInternal, "xxx.txt", False))
     Writer.WriteLine ("some data")
     Writer.Flush
     Writer.Close
End Sub

and when I power down tablet, some time data was not written, some time written nonsense data
I know that my problem is to power down the tablet with no chance to Android real write to disk.
If I write data and press the power down button (shutdown or restart), Android write to disk and no problem, but press button all time is not desired.
I have very litle knowledge in C, and the sync command 'forces' the OS to real write data to disk, even though this use time to do.
So, I searched internet to 'Android and sync' and found the example using Java and Outputstream and worked. No special reason to use it.

Rubens Jr.
 
Upvote 0
Top