Stream Lua Reference Documentation

chilkat.Stream

Current Version: 11.7.0

Markdown version of this page for AI tools (v 11.7.0) · all AI docs

Chilkat.Stream

Read, write, pipe, monitor, and control byte or text streams.

Chilkat.Stream is a general-purpose streaming object for moving data between sources and sinks without requiring the entire payload to be handled at once. A stream can read from files, byte arrays, strings, or other sources; write to files, memory, BinData, StringBuilder, or other sinks; handle binary and text data; apply character-set rules for strings; process file parts; track bytes sent and received; and support timeouts, polling, aborts, async task integration, and end-of-stream handling.

File and memory sources

Stream from a source file, byte array, string, or selected file part without manually loading or copying all data first.

File and object sinks

Write streamed data to a sink file, append to an existing file, or output to BinData, StringBuilder, bytes, or strings.

Binary reads and writes

Read or write raw bytes, fixed byte counts, encoded byte strings, individual bytes, or BinData content.

Text streaming

Read and write strings using StringCharset, optionally use a BOM, and read text until CRLF or a matching delimiter.

Timeouts and polling

Control read and write wait behavior, poll for immediately available data, and inspect read or write failure reasons.

Progress and lifecycle

Track bytes sent and received, detect end-of-stream or write-closed state, reset streams, close writing, and abort long-running operations.

Common pattern: Configure the stream's source and sink, set the character set or chunk size if needed, then read or write using the method that matches the data type: bytes, encoded bytes, strings, BinData, or StringBuilder. Check EndOfStream, ReadFailReason, WriteFailReason, NumReceived, NumSent, and LastErrorText to understand stream progress and failures.
Timeout note: For ReadTimeoutMs, a value of 0 means a polling read, not an infinite timeout. For WriteTimeoutMs, 0 means return immediately if the stream cannot be written to right away.

Object Creation

local chilkat = require("chilkat")

local stream = chilkat.Stream.new()
local stream = chilkat.Stream.new()

Creates the object (chilkat.Stream() does the same). Lua's garbage collector frees it when it is no longer referenced; stream:dispose() frees it at once, and in Lua 5.4 and later local stream <close> = chilkat.Stream.new() frees it when the block ends. Calling a method of a disposed object raises an error.

Errors

A method that fails returns nil followed by the error text, as io.open does: local body, err = stream:method(...), where err is the object's last_error_text. Wrap the call in assert() to raise an error with that text instead. Methods that answer a question return true or false, numbers are returned as they are (check last_method_success where the description says a value means failure), and property reads and writes never fail for Chilkat reasons.

Misuse raises a Lua error: a wrong argument type or a missing argument (bad argument #1 to ...), an unknown property name, a write to a read-only property, an out-of-range integer, or an object of the wrong class.

Names are Chilkat's in snake_case: QuickGetStr is quick_get_str, LastErrorText is last_error_text. Properties are fields (stream.verbose_logging = true) and methods use the colon (stream:method(...)). Strings are UTF-8; binary data is a Lua string of bytes. Indexes are 0-based, as in every Chilkat API. Unlocking: call chilkat.unlock_bundle("...") once at startup.

Properties

AbortCurrent
-- boolVal is a boolean
local boolVal = stream.abort_current
stream.abort_current = boolVal
Introduced in version 9.5.0.58

When set to true, causes the currently running method to abort. Methods that always finish quickly (i.e.have no length file operations or network communications) are not affected. If no method is running, then this property is automatically reset to false when the next method is called. When the abort occurs, this property is reset to false. Both synchronous and asynchronous method calls can be aborted. (A synchronous method call could be aborted by setting this property from a separate thread.)

top
CanRead
-- boolVal is a boolean (read-only)
local boolVal = stream.can_read
Introduced in version 9.5.0.56

true if the stream supports reading. Otherwise false.

Note: A stream that supports reading, which has already reached the end-of-stream, will still have a CanRead value of true. This property indicates the stream's inherent ability, and not whether or not the stream can be read at a particular moment in time.

top
CanWrite
-- boolVal is a boolean (read-only)
local boolVal = stream.can_write
Introduced in version 9.5.0.56

true if the stream supports writing. Otherwise false.

Note: A stream that supports writing, which has already been closed for write, will still have a CanWrite value of true. This property indicates the stream's inherent ability, and not whether or not the stream can be written at a particular moment in time.

top
DataAvailable
-- boolVal is a boolean (read-only)
local boolVal = stream.data_available
Introduced in version 9.5.0.56

true if it is known for sure that data is ready and waiting to be read. false if it is not known for sure (it may be that data is immediately available, but reading the stream with a ReadTimeoutMs of 0, which is to poll the stream, is the way to find out).

top
DebugLogFilePath
-- strVal is a string
local strVal = stream.debug_log_file_path
stream.debug_log_file_path = strVal

If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.

Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.

Possible causes of hangs include:

  • A timeout property set to 0, indicating an infinite timeout.
  • A hang occurring within an event callback in the application code.
  • An internal bug in the Chilkat code causing the hang.

More Information and Examples
top
DefaultChunkSize
-- intVal is an integer
local intVal = stream.default_chunk_size
stream.default_chunk_size = intVal
Introduced in version 9.5.0.56

The default internal chunk size for reading or writing. The default value is 65536. If this property is set to 0, it will cause the default chunk size (65536) to be used. Note: The chunk size can have significant performance impact. If performance is an issue, be sure to experiment with different chunk sizes.

top
EndOfStream
-- boolVal is a boolean (read-only)
local boolVal = stream.end_of_stream
Introduced in version 9.5.0.56

true if the end-of-stream has already been reached. When the stream has already ended, all calls to Read* methods will return false with the ReadFailReason set to 3 (already at end-of-stream).

top
HeartbeatMs
-- intVal is an integer
local intVal = stream.heartbeat_ms
stream.heartbeat_ms = intVal
Introduced in version 9.5.0.97

The interval in milliseconds between each AbortCheck event callback, which enables an application to abort certain method calls before they complete. By default, HeartbeatMs is set to 0, meaning no AbortCheck event callbacks will trigger.

More Information and Examples
top
IsWriteClosed
-- boolVal is a boolean (read-only)
local boolVal = stream.is_write_closed
Introduced in version 9.5.0.56

true if the stream is closed for writing. Once closed, no more data may be written to the stream.

top
LastErrorHtml
-- strVal is a string (read-only)
local strVal = stream.last_error_html

Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorText
-- strVal is a string (read-only)
local strVal = stream.last_error_text

Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorXml
-- strVal is a string (read-only)
local strVal = stream.last_error_xml

Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastMethodSuccess
-- boolVal is a boolean
local boolVal = stream.last_method_success
stream.last_method_success = boolVal

Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.

top
Length
-- intVal is an integer
local intVal = stream.length
stream.length = intVal
Introduced in version 9.5.0.56

The length (in bytes) of the stream's source. If unknown, then this property will have a value of -1. This property may be set by the application if it knows in advance the length of the stream.

top
Length32
-- intVal is an integer
local intVal = stream.length32
stream.length32 = intVal
Introduced in version 9.5.0.58

The length (in bytes) of the stream's source. If unknown, then this property will have a value of -1. This property may be set by the application if it knows in advance the length of the stream.

Setting this property also sets the Length property (which is a 64-bit integer).

top
NumReceived
-- intVal is an integer (read-only)
local intVal = stream.num_received
Introduced in version 9.5.0.56

The number of bytes received by the stream.

top
NumSent
-- intVal is an integer (read-only)
local intVal = stream.num_sent
Introduced in version 9.5.0.56

The number of bytes sent by the stream.

top
ReadFailReason
-- intVal is an integer (read-only)
local intVal = stream.read_fail_reason
Introduced in version 9.5.0.56

This property is automatically set when a Read* method is called. It indicates the reason for failure. Possible values are:

    0>
  1. No failure (success)
  2. Timeout, or no data is immediately available for a polling read.
  3. Aborted by an application callback.
  4. Already at end-of-stream.
  5. Fatal stream error.
  6. Out-of-memory error (this is very unlikely).

top
ReadTimeoutMs
-- intVal is an integer
local intVal = stream.read_timeout_ms
stream.read_timeout_ms = intVal
Introduced in version 9.5.0.56

The maximum number of seconds to wait while reading. The default value is 30 seconds (i.e. 30000ms). A value of 0 indicates a poll. (A polling read is to return with a timeout if no data is immediately available.)

Important: For most Chilkat timeout related properties, a value of 0 indicates an infinite timeout. For this property, a value of 0 indicates a poll. If setting a timeout related property (or method argument) to zero, be sure to understand if 0 means wait forever or poll.

The timeout value is not a total timeout. It is the maximum time to wait while no additional data is forthcoming.

top
SinkFile
-- strVal is a string
local strVal = stream.sink_file
stream.sink_file = strVal
Introduced in version 9.5.0.56

Sets the sink to the path of a file. The file does not need to exist at the time of setting this property. The sink file will be automatically opened on demand, when the stream is first written.

Note: This property takes priority over other potential sinks. Make sure this property is set to an empty string if the stream's sink is to be something else.

top
SinkFileAppend
-- boolVal is a boolean
local boolVal = stream.sink_file_append
stream.sink_file_append = boolVal
Introduced in version 9.5.0.83

If true, the stream appends to the SinkFile rather than truncating and re-writing the sink file. The default value is false.

top
SourceFile
-- strVal is a string
local strVal = stream.source_file
stream.source_file = strVal
Introduced in version 9.5.0.56

Sets the source to the path of a file. The file does not need to exist at the time of setting this property. The source file will be automatically opened on demand, when the stream is first read.

Note: This property takes priority over other potential sources. Make sure this property is set to an empty string if the stream's source is to be something else.

top
SourceFilePart
-- intVal is an integer
local intVal = stream.source_file_part
stream.source_file_part = intVal
Introduced in version 9.5.0.59

If the source is a file, then this property can be used to stream one part of the file. The SourceFilePartSize property defines the size (in bytes) of each part. The SourceFilePart and SourceFilePartSize have default values of 0, which means the entire SourceFile is streamed.

This property is a 0-based index. For example, if the SourceFilePartSize is 1000, then part 0 is for bytes 0 to 999, part 1 is for bytes 1000 to 1999, etc.

More Information and Examples
top
SourceFilePartSize
-- intVal is an integer
local intVal = stream.source_file_part_size
stream.source_file_part_size = intVal
Introduced in version 9.5.0.59

If the source is a file, then this property, in conjunction with the SourceFilePart property, can be used to stream a single part of the file. This property defines the size (in bytes) of each part. The SourceFilePart and SourceFilePartSize have default values of 0, which means that by default, the entire SourceFile is streamed.

More Information and Examples
top
StringBom
-- boolVal is a boolean
local boolVal = stream.string_bom
stream.string_bom = boolVal
Introduced in version 9.5.0.56

If true, then include the BOM when creating a string source via SetSourceString where the charset is utf-8, utf-16, etc. (The term BOM stands for Byte Order Mark, also known as the preamble.) Also, if true, then include the BOM when writing a string via the WriteString method. The default value of this property is false.

top
StringCharset
-- strVal is a string
local strVal = stream.string_charset
stream.string_charset = strVal
Introduced in version 9.5.0.56

Indicates the expected character encoding, such as utf-8, windows-1256, utf-16, etc. for methods that read text such as: ReadString, ReadToCRLF, ReadUntilMatch. Also controls the character encoding when writing strings with the WriteString method. The supported charsets are indicated at the link below.

The default value is utf-8.

More Information and Examples
top
VerboseLogging
-- boolVal is a boolean
local boolVal = stream.verbose_logging
stream.verbose_logging = boolVal

If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
-- strVal is a string (read-only)
local strVal = stream.version

Version of the component/library, such as "10.1.0"

More Information and Examples
top
WriteFailReason
-- intVal is an integer (read-only)
local intVal = stream.write_fail_reason
Introduced in version 9.5.0.56

This property is automatically set when a Write* method is called. It indicates the reason for failure. Possible values are:

    0>
  1. No failure (success)
  2. Timeout, or unable to immediately write when the WriteTimeoutMs is 0.
  3. Aborted by an application callback.
  4. The stream has already ended.
  5. Fatal stream error.
  6. Out-of-memory error (this is very unlikely).

top
WriteTimeoutMs
-- intVal is an integer
local intVal = stream.write_timeout_ms
stream.write_timeout_ms = intVal
Introduced in version 9.5.0.56

The maximum number of seconds to wait while writing. The default value is 30 seconds (i.e. 30000ms). A value of 0 indicates to return immediately if it is not possible to write to the sink immediately.

top

Methods

ReadBd
-- bin_data is a chilkat.BinData
-- ok is true, or nil and err on failure
local ok, err = stream:read_bd(bin_data)
Introduced in version 9.5.0.67

Read as much data as is immediately available on the stream. If no data is immediately available, it waits up to ReadTimeoutMs milliseconds for data to arrive. The incoming data is appended to bin_data.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
ReadBytesENC
-- encoding is a string
-- str is a string, or nil and err on failure
local str, err = stream:read_bytes_enc(encoding)
Introduced in version 9.5.0.56

The same as ReadBytes, except returns the received bytes in encoded string form. The encoding argument indicates the encoding, which can be base64, hex, or any of the multitude of encodings indicated in the link below.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
ReadNBytesENC
-- num_bytes is an integer
-- encoding is a string
-- str is a string, or nil and err on failure
local str, err = stream:read_n_bytes_enc(num_bytes, encoding)
Introduced in version 9.5.0.56

The same as ReadNBytes, except returns the received bytes in encoded string form. The encoding argument indicates the encoding, which can be base64, hex, or any of the multitude of encodings indicated in the link below.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
ReadSb
-- sb is a chilkat.StringBuilder
-- ok is true, or nil and err on failure
local ok, err = stream:read_sb(sb)
Introduced in version 9.5.0.67

Read as much data as is immediately available on the stream. If no data is immediately available, it waits up to ReadTimeoutMs milliseconds for data to arrive. The data is appended to sb. The incoming bytes are interpreted according to the StringCharset property. For example, if utf-8 bytes are expected, then StringCharset should be set to utf-8 prior to calling ReadSb.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
ReadString
-- str is a string, or nil and err on failure
local str, err = stream:read_string()
Introduced in version 9.5.0.56

Read as much data as is immediately available on the stream. If no data is immediately available, it waits up to ReadTimeoutMs milliseconds for data to arrive. The data is returned as a string. The incoming bytes are interpreted according to the StringCharset property. For example, if utf-8 bytes are expected, then StringCharset should be set to utf-8 prior to calling ReadString.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
ReadToCRLF
-- str is a string, or nil and err on failure
local str, err = stream:read_to_crlf()
Introduced in version 9.5.0.56

Reads the stream until a CRLF is received. If no data is immediately available, it waits up to ReadTimeoutMs milliseconds for data to arrive. The data is returned as a string. The incoming bytes are interpreted according to the StringCharset property. For example, if utf-8 bytes are expected, then StringCharset should be set to utf-8 prior to calling ReadString.

Note: If the end-of-stream is reached prior to receiving the CRLF, then the remaining data is returned, and the ReadFailReason property will be set to 3 (to indicate end-of-file). This is the only case where as string would be returned that does not end with CRLF.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
ReadUntilMatch
-- match_str is a string
-- str is a string, or nil and err on failure
local str, err = stream:read_until_match(match_str)
Introduced in version 9.5.0.56

Reads the stream until the string indicated by match_str is received. If no data is immediately available, it waits up to ReadTimeoutMs milliseconds for data to arrive. The data is returned as a string. The incoming bytes are interpreted according to the StringCharset property. For example, if utf-8 bytes are expected, then StringCharset should be set to utf-8 prior to calling ReadString.

Note: If the end-of-stream is reached prior to receiving the match string, then the remaining data is returned, and the ReadFailReason property will be set to 3 (to indicate end-of-file). This is the only case where as string would be returned that does not end with the desired match string.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
Reset
stream:reset()
Introduced in version 9.5.0.56

Resets the stream. If a source or sink is open, then it is closed. Properties such as EndOfStream and IsWriteClose are reset to default values.

top
SetSourceString
-- src_str is a string
-- charset is a string
-- ok is true, or nil and err on failure
local ok, err = stream:set_source_string(src_str, charset)
Introduced in version 9.5.0.56

Sets the stream's source to the contents of src_str. The charset indicates the character encoding to be used for the byte representation of the src_str.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
WriteBd
-- bin_data is a chilkat.BinData
-- ok is true, or nil and err on failure
local ok, err = stream:write_bd(bin_data)
Introduced in version 9.5.0.67

Writes the contents of bin_data to the stream.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
WriteByte
-- byte_val is an integer
-- ok is true, or nil and err on failure
local ok, err = stream:write_byte(byte_val)
Introduced in version 9.5.0.56

Writes a single byte to the stream. The byte_val must have a value from 0 to 255.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
WriteBytes2
-- byte_data is a string of bytes
-- ok is true, or nil and err on failure
local ok, err = stream:write_bytes2(byte_data)
Introduced in version 9.5.0.82

Writes binary bytes to a stream.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
WriteBytesENC
-- byte_data is a string
-- encoding is a string
-- ok is true, or nil and err on failure
local ok, err = stream:write_bytes_enc(byte_data, encoding)
Introduced in version 9.5.0.56

Writes binary bytes to a stream. The byte data is passed in encoded string form, where the encoding can be base64, hex, or any of the supported binary encodings listed at the link below.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
WriteClose
-- ok is true, or nil and err on failure
local ok, err = stream:write_close()
Introduced in version 9.5.0.56

Indicates that no more data will be written to the stream.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
WriteSb
-- sb is a chilkat.StringBuilder
-- ok is true, or nil and err on failure
local ok, err = stream:write_sb(sb)
Introduced in version 9.5.0.67

Writes the contents of sb to the stream. The actual bytes written are the byte representation of the string as indicated by the StringCharset property. For example, to write utf-8 bytes, first set StringCharset equal to utf-8 and then call WriteSb.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
WriteString
-- str is a string
-- ok is true, or nil and err on failure
local ok, err = stream:write_string(str)
Introduced in version 9.5.0.56

Writes a string to a stream. The actual bytes written are the byte representation of the string as indicated by the StringCharset property. For example, to write utf-8 bytes, first set StringCharset equal to utf-8 and then call WriteString.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top

Events

All Chilkat methods are synchronous: the call returns when the work is done. During a long-running call, chilkat.Stream reports progress to the functions given to set_event_handler, in a table keyed by event name (plain functions, not methods: closures carry whatever state they need):

stream:set_event_handler({
    percent_done  = function(pct) print(pct .. "%") end,              -- return true to abort
    progress_info = function(name, value) print(name, value) end,
})
stream.heartbeat_ms = 250   -- abort_check is called 4 times per second
stream:set_event_handler({ event_name = function(...) ... end, ... })

Installs the handlers, replacing any installed before; stream:set_event_handler(nil) removes them. The event names are checked when the table is installed: a misspelled one raises an error.

The abort_check handler is called at the interval set by the heartbeat_ms property (0, the default, disables it); percent_done is called when an operation's completion percentage is known; progress_info delivers named progress values. A true return from abort_check or percent_done aborts the running method, which then fails in the usual way (nil, err). An error raised in a handler aborts the method and is raised again from the method call; the object remains usable.

AbortCheck
-- handler: function() ... return abort end -- a true return aborts the method in progress
stream:set_event_handler({ abort_check = function() ... end })

Enables a method call to be aborted by triggering the AbortCheck event at intervals defined by the HeartbeatMs property. If HeartbeatMs is set to its default value of 0, no events will occur. For instance, set HeartbeatMs to 200 to trigger 5 AbortCheck events per second.

More Information and Examples

Example

local deadline = os.time() + 30                  -- give up after 30 seconds
stream.heartbeat_ms = 250                      -- call the handler 4 times per second
stream:set_event_handler({ abort_check = function() return os.time() > deadline end })
top
PercentDone
-- handler: function(pct_done) ... return abort end -- a true return aborts
stream:set_event_handler({ percent_done = function(pct_done) ... end })

This provides the percentage completion for any method involving network communications or time-consuming processing, assuming the progress can be measured as a percentage. This event is triggered only when it's possible and logical to express the operation's progress as a percentage. The pctDone argument will range from 1 to 100. For methods that finish quickly, the number of PercentDone callbacks may vary, but the final callback will have pctDone equal to 100. For longer operations, callbacks will not exceed one per percentage point (e.g., 1, 2, 3, ..., 98, 99, 100).

The PercentDone callback also acts as an AbortCheck event. For fast methods where PercentDone fires, an AbortCheck event may not trigger since the PercentDone callback already provides an opportunity to abort. For longer operations, where time between PercentDone callbacks is extended, AbortCheck callbacks enable more responsive operation termination.

To abort the operation, set the abort output argument to true. This will cause the method to terminate and return a failure status or corresponding failure value.

More Information and Examples

Example

stream:set_event_handler({
    percent_done = function(pct_done)     -- 0 to 100
        print("Percent done: " .. pct_done)
        return false                       -- return true to abort the method in progress
    end,
})
top
ProgressInfo
-- handler: function(name, value) ... end
stream:set_event_handler({ progress_info = function(name, value) ... end })

This event callback provides tag name/value pairs that detail what occurs during a method call. To discover existing tag names, create code to handle the event, emit the pairs, and review them. Most tag names are self-explanatory.

Note: Some Chilkat methods don't fire any ProgressInfo events.

More Information and Examples

Example

stream:set_event_handler({
    progress_info = function(name, value) print(name .. ": " .. value) end,
})
top