Home > Language Reference > Classes > StreamCipher

Flush Method

Flushes all internal buffers and resets the encoding/decoding algorithm.

Syntax

Public Sub Flush()

Remarks

The Flush method is used to synchronize block operations. It flushes all internal buffers, and reset the encoding/decoding algorithm. Calling this method on an uninitialized object have no effect, and notably does not raise any runtime error.

The exact behavior of the Flush method depends on whether the StreamCipher object it is applied to is in write or read mode.

In write mode, it must be called to signal the StreamCipher object that no more data will be sent. It forces encoding and writing of any pending data in the internal buffers, and for some encoding schemes, writes the necessary termination characters.

Only encoding schemes that operate on blocks need this method to be called (such as Bas64). However, it is a good programming practice to always call it when you are done with a stream.

The following example is a simple routine that attaches a database to an email. It accepts a reference to a stream that should already contain the message body, a string that will be used to mark multipart boundaries, and a DatabaseInfo object properly initialized to point on the database to be joined:

Private Sub Attachment(ByRef f As Stream, ByRef sBoundary As String, ByRef db As DatabaseInfo)
  Dim c as New StreamCipher
  Dim s as String
  
  s=sBoundary & "\r\n"
  f.Write s, Len(s)
  
  s="Content-Type: application/vnd.palm\r\n"
  f.Write s, Len(s)
  
  s="Content-Transfer-Encoding: base64\r\n"
  f.Write s, Len(s)

  s="Content-Disposition: attachment; filename=""" & db.Name
  If db.AttrResourceDB Then s=s & ".prc" Else s=s & ".pdb"
  s=s & """\r\n\r\n"
  f.Write s, Len(s)
  
  Set c.Source=f
  c.InitBase64 "\r\n"
  
  c.Write db
  c.Flush
  
  s="\r\n\r\n" & sBoundary & "\r\n"
  f.Write s, Len(s)
End Sub

In this sample, the Flush method ensures the Base64 part of the message is fully written before adding the final carriage returns and separators. The following is a sample dump of the data stream written by this routine:

------Multipart-Boundary------
Content-Type: application/vnd.palm
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="AddressDB.pdb"

QWRkcmVzc0RCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAtt0Ldrht+aMAAAAAAAAAAQAA
AGAAAAAAREFUQWFkZHIAAAAAAAAAAAACAAAC3kAAAAIAAAQCQAAAAwAAAA9Ob24gY2xhc3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SBXZWI6IHd3dy5wYWxtLmNvbQBQb3VyIG9idGVuaXIgZGVz
IGFjY2Vzc29pcmVzIG91IGRlcyBhcHBsaWNhdGlvbnMgdGllcmNlcyB1dGlsZXMsIHZpc2l0
ZXogbm90cmUgc2l0ZSBXZWIg4CBsJ2FkcmVzc2Ugc3VpdmFudGUgOiB3d3cucGFsbS5jb20A
AAQyVQAEAN0TU3VwcG9ydCB0ZWNobmlxdWUAUGFsbSwgSW5jLgA4NDctMjYyLVBBTE0gKDcy
NTYpICjJdGF0cy1VbmlzKQBBdXRyZXMgcGF5cyA6IHZvaXIgbGEgQ2FydGUgYXNzaXN0YW5j
ZSB1dGlsaXNhdGV1ciBpbnRlcm5hdGlvbmFsAFNpdGUgV2ViOiB3d3cucGFsbS5jb20Ac3Vw
cG9ydEBjb3JwLnBhbG0uY29tAFBvdXIgb2J0ZW5pciBkZSBsJ2Fzc2lzdGFuY2UgdGVjaG5p
cXVlIGVuIGRlaG9ycyBkZXMgRXRhdHMtVW5pcywgdXRpbGlzZXogbGVzIG51belyb3MgZmln
dXJhbnQgc3VyIGxhIENhcnRlIGFzc2lzdGFuY2UgdXRpbGlzYXRldXIgaW50ZXJuYXRpb25h
bC4gUG91ciBvYnRlbmlyIGxlcyBkZXJuaehyZXMgaW5mb3JtYXRpb25zIHN1ciBsZXMgcHJv
ZHVpdHMgZXQgbGVzIG1pc2VzIOAgbml2ZWF1LCB2aXNpdGV6IHLpZ3VsaehyZW1lbnQgbGUg
c2l0ZSBXZWIgd3d3LnBhbG0uY29tLgoA

------Multipart-Boundary------

For more information on the format of internet message bodies, you can refer to the relevant documentation.

In read mode, the Flush method can be called to reset the decoding algorithm to its initial state. This is typically used when you have to read some data from the raw source stream and from the encoded source stream alternatively. The following example is a simple client application, where some parts of the protocol are encrypted and some others not:

Private sSock As New StreamSocket
Private sCipher As New StreamCipher

Private Sub Init()
  sSock.Connect "MyUrl", MyPort
  Set sCipher.Source = sSock
  sCipher.InitRC4 "My Private Key"
End Sub

Private Sub ReadCommand()
  Dim cmd As Long
  Dim param As String

  sSock.Read cmd     ' Read a command code directly from the socket
  sCipher.Flush       ' Re-synchronize after direct reading
  sCipher.Read param ' Read an encrypted string from the socket

  ' Use cmd and param
  ...

End Sub

Most of the time, you won't need to call this method while reading a stream.

Note: Unlike other stream objects, data are not automatically flushed when the StreamCipher object is destroyed by the garbage collector.

System requirements

SystemMinimal versionRemarks
Palm OSPalm OS 3.0N/A