Examples: Write method

  1. This agent demonstrates making an exact copy of a file using NotesStream. The agent associates one stream with an existing file and a second stream with a new file. The agent reads blocks of bytes from the first stream and writes them to the second stream until end of stream occurs on the first stream.
    Sub Initialize
    REM Set names of input and output files
      Const DIRECTORY = "c:\StreamFiles\"
      Const FILENAME = "domobj"
      Const EXT = ".tlb"
      Dim inPath As String, outPath As String
      inPath = DIRECTORY & FILENAME & EXT
      outPath = DIRECTORY & FILENAME & ".txt"
      
      Dim session As NotesSession
      Dim inStream As NotesStream, outStream As NotesStream
      Set session = New NotesSession
      
    REM Get the input file
      Set inStream = session.CreateStream
      If Not inStream.Open(inPath, "binary") Then
        Messagebox inPath,, "Open failed"
        Exit Sub
      End If
      If inStream.Bytes = 0 Then
        Messagebox inPath,, "File has no content"
        Exit Sub
      End If
      
    REM Get the output file
      Set outStream = session.CreateStream
      If Not outStream.Open(outPath, "binary") Then
        Messagebox outPath,, "Open failed"
        Exit Sub
      End If
      If outStream.Bytes <> 0 Then
        Messagebox outPath,, "File exists and has content"
        Exit Sub
      End If
      
    REM Transfer input file to output file
      Do
        buffer = inStream.Read(32767)
        Call outStream.Write(buffer)
      Loop Until inStream.IsEOS
      
      Call inStream.Close
      Call outStream.Close
    End Sub
  2. This agent adds some processing to the first example. It copies only letters and numbers from the first file to the new file, inserting a space whenever a sequence of non-alphanumeric bytes occurs.
    Sub Initialize
    REM Set names of input and output files
      Const DIRECTORY = "c:\StreamFiles\"
      Const FILENAME = "domobj"
      Const EXT = ".tlb"
      Dim inPath As String, outPath As String
      inPath = DIRECTORY & FILENAME & EXT
      outPath = DIRECTORY & FILENAME & ".txt"
      
      Dim session As NotesSession
      Dim inStream As NotesStream, outStream As NotesStream
      Dim inBuff As Variant, outBuff() As Byte
      Set session = New NotesSession
      
    REM Get the input file
      Set inStream = session.CreateStream
      If Not inStream.Open(inPath, "binary") Then
        Messagebox inPath,, "Open failed"
        Exit Sub
      End If
      If inStream.Bytes = 0 Then
        Messagebox inPath,, "File has no content"
        Exit Sub
      End If
      
    REM Get the output file
      Set outStream = session.CreateStream
      If Not outStream.Open(outPath, "binary") Then
        Messagebox outPath,, "Open failed"
        Exit Sub
      End If
      If outStream.Bytes <> 0 Then
        Messagebox outPath,, "File exists and has content"
        Exit Sub
      End If
      
    REM Transfer input file to output file
      prevChar = True
      Do
        inBuff = inStream.Read(32767)
        counter% = 0
    REM Put alphanumeric characters in write buffer
    REM First insert a space if the previous byte
    REM is not alphanumeric
        Forall b In inBuff
          If (b >= 49 And b <= 57) _
          Or (b >= 65 And b <= 90) _
          Or (b >= 97 And b <= 122) Then
            If Not prevChar Then
              Redim Preserve outBuff(0 To counter%)
              outBuff(counter%) = 32
              counter% = counter% + 1
              prevChar = True
            End If
            Redim Preserve outBuff(0 To counter%)
            outBuff(counter%) = b
            counter% = counter% + 1
          Else
            prevChar = False
          End If
        End Forall
        Call outStream.Write(outBuff)
      Loop Until inStream.IsEOS
      
      Call inStream.Close
      Call outStream.Close
    End Sub