B4A Library NamedPipe

This is a simple to use library that enables you to create a NamedPipe.
Your NamedPipe can be used to send and receive bytes from one process to another process.

  • AutoCloseInputStream
    • MarkSupported As Boolean
      Tests if this input stream supports the mark and reset methods.
    • Mark (ReadLimit As Int)
      Marks the current position in this input stream.
      The Readlimit argument tells this input stream to allow that many bytes to be read before the mark position gets invalidated.
    • IsInitialized As Boolean
    • Read2 (Buffer() As Byte, Offset As Int, Length As Int) As Int
      Reads up to Length bytes of data at Offset from this input stream into Buffer.
      Less than Length bytes will be read if the end of the data stream is reached or if Length exceeds the buffer size.
    • Read As Int
      Reads the next byte of data or -1 if the end of the input stream is reached.
      The value byte is returned as an Int in the range 0 to 255.
    • Skip (Number As Long) As Long
    • Close
      Closes this input stream and releases any system resources associated with the stream.
    • Reset
    • Available As Int [read only]
      Returns an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream
      without blocking by the next invocation of a method for the current underlying input stream.
  • AutoCloseOutputStream
    • Write (Bytes() As Byte)
      Writes Bytes.Length bytes from the specified byte array to this output stream.
    • Flush
      Flushes this output stream and forces any buffered output bytes to be written out.
    • Write3 (Byte1 As Int)
      Writes Byte1 to this output stream.
    • Close
      Closes this output stream and releases any system resources associated with this stream.
    • IsInitialized As Boolean
    • Write2 (Bytes() As Byte, Offset As Int, Length As Int)
      Writes Length bytes from the specified byte array starting at Offset to this output stream.
  • NamedPipe
    • Close
      Closes both InputStream and OutputStream
    • IsInitialized As Boolean
    • Initialize
    • InputStream As AutoCloseInputStream [read only]
    • OutputStream As AutoCloseOutputStream [read only]
    • FdOutput As Int [read only]
      Returns the native file descriptor (integer) for this Pipe's Output.
    • FdInput As Int [read only]
      Returns the native file descriptor (integer) for this Pipe's Input.


  • NamedPipe_v1.0.zip
    5.6 KB · Views: 156


Licensed User
Longtime User
A simple NamedPipe example which uses my ThreadExtras library.

#Region  Service Attributes
   #StartAtBoot: False
#End Region

'   ignore warnings that Subs ReadPipe and WritePipe are not used
#IgnoreWarnings: 12

Sub Process_Globals
   Private NamedPipe1 As NamedPipe
   Private PipeReader As Runnable
   Private PipeWriter As Runnable
End Sub

Sub Service_Create
   PipeReader.Initialize("PipeReader", Me, "ReadPipe", Array As Object())
   PipeWriter.Initialize("PipeWriter", Me, "WritePipe", Array As Object())
   PipeReader.Start   '   ReadPipe will be executed in a background thread
   PipeWriter.Start   '   WritePipe will be executed in a background thread
End Sub

Sub Service_Start (StartingIntent As Intent)
End Sub

Sub Service_Destroy
   Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub

Sub PipeReader_Complete(Result As Object)
End Sub

Sub PipeWriter_Complete(Result As Object)
End Sub

Sub ReadPipe()
   Dim AutoCloseInputStream1 As AutoCloseInputStream=NamedPipe1.InputStream
   '   a 32 byte buffer to read the pipe into
   Dim Bytes(32) As Byte   
   Do Until PipeReader.StopFlag Or PipeReader.IsInterrupted
       '   this method will block until the pipe contains bytes that it can read
       '   it's essential that you don't try to use this method on the UI thread
       '   your app would probably force close if you did so
       Dim BytesRead As Int=AutoCloseInputStream1.Read2(Bytes, 0, Bytes.Length)
       Log(BytesToString(Bytes, 0, BytesRead, "UTF8"))
End Sub

Sub WritePipe()
   Dim AutoCloseOutputStream1 As AutoCloseOutputStream=NamedPipe1.OutputStream
   Dim Thread1 As Thread
   Dim Bytes() As Byte="Hello from Sub WritePipe".GetBytes("UTF8")
   Do Until PipeWriter.StopFlag Or PipeWriter.IsInterrupted
       AutoCloseOutputStream1.Write2(Bytes, 0, Bytes.Length)
           '   an InterruptedException will occur if PipeWriter.Interrupt is executed while Sleep is executing
       End Try
End Sub

In this example i create and initialize a NamedPipe.
Then create and initialize two Runnables.
One Runnable will execute Sub ReadPipe in a background thread, the other will execute Sub WritePipe in a background thread.
Both Runnables are started and Sub WritePipe writes bytes to the named pipe while Sub ReadPipe reads and logs those bytes.

This code runs in a Service, when the Service is destroyed each Runnable's StopFlag property is set to True.
Additonally the Runnable method Interrupt is called on the PipeWriter Runnable.
This ensures that each background thread gracefully terminates.


  • NamedPipeExample.zip
    8.3 KB · Views: 165