putData
Class: matlab.net.http.io.StringConsumer
Namespace: matlab.net.http.io
Append next buffer of string data to response
Syntax
[len,stop] = putData(consumer,data)
Description
[
converts len
,stop
] = putData(consumer
,data
)data
to a Unicode® string using the current value of the Charset
property and appends the results to Response.Body.Data
. During this
process the currently converted string is at Response.Body.Data
. If
the TextType
property is 'char'
, then only characters up to
CurrenLength
are valid.
If data
is []
, it indicates the end of the
message. On return, Response.Body.Data
contains the entire
converted string or character vector.
For multibyte encodings such as UTF-8, it is possible that a given buffer of
data
ends with a partial multibyte character. In that case
Response.Body.Data
might be missing that last character, until
the next call to putData
completes it.
This method is an overridden method of putData
. If you implement a subclass of this consumer and
want to examine the raw bytes before the charset conversion, then override this method,
examine data
, change the Charset
property if
necessary, and then pass data
to this superclass method for
conversion and storage in Response.Body.Data
. If you change
Charset
after putData
has already been called
to process previous buffers, be aware that a partial multibyte character at the end of
the previous buffer that has not yet been converted could be lost. This would not occur
if all characters previously received are single-byte (for example, US-ASCII or the
ASCII subset of UTF-8).
A more likely scenario is that you want to examine each buffer of data as it arrives
after charset conversion. To do so, override this method as follows (this works whether
TextType
is char
or
string
):
function [len, stop] = putData(obj, data) oldLength = obj.CurrentLength; % send raw bytes to StringConsumer for conversion [len, stop] = obj.putData@matlab.net.http.io.StringConsumer(data); newData = obj.Response.Body.Data.extractAfter(oldLength); % ...process newData...
Now newData
contains the most recently added data, after
conversion. Note that the above pattern still stores the resulting string in
Response.Body.Data
.
If your subclass wants to stream its own results into the response after processing
the string, use the convert
method to convert your data based on the TextType
and
Charset
in this object. In that case, call this
putData
method only at the end of the data, with an empty
argument.
Input Arguments
Output Arguments
Attributes
Access | public |
Examples
For an example subclassing this method, see the PricesStreamer
class putData
method in
Display Streamed Data in Figure Window.
Version History
Introduced in R2018a