例: Read method

  1. 次のエージェントは、NotesStream を使用したファイルの正確なコピーの作成を示します。エージェントは、1 つのストリームを既存のファイルに関連付け、第 2 のストリームを新規ファイルに関連付けます。エージェントは、最初のストリームからバイトのブロックを読み込み、最初のストリームでストリームが終了するまで、これらを第 2 のストリームに書き込みます。
    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. 次のエージェントは、最初の例に処理を追加します。文字と番号のみを最初のファイルから新規ファイルにコピーし、英数字以外の一連のバイトがある場合にはスペースを挿入します。
    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