This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

matlab.net.http.io.StringConsumer class

Package: matlab.net.http.io
Superclasses: matlab.net.http.io.ContentConsumer

String consumer for HTTP payloads

Description

A StringConsumer stores character data in a response message, decoded according to the charset based on the Content-Type, in the response body. You can specify this consumer directly when sending a RequestMessage to specify a string conversion for the data with certain parameters.

For an example subclassing this consumer, see the PricesStreamer class in Display Streamed Data in Figure Window.

Construction

consumer = StringConsumer constructs a consumer that converts input to a scalar string using the character set specified in the Content-Type of the message.

consumer = StringConsumer(Name,Value) constructs a consumer with options specified by one or more Name,Value pair arguments. Name is the property name Charset or TextType and Value is the corresponding value. You can specify the name-value pair arguments in any order as Name1,Value1,Name2,Value2.

If you do not specify a Charset property, then this consumer tries to derive the charset from the ContentType property, which MATLAB® sets based on the Content-Type field in the Response. StringConsumer knows the charset if ContentType has an explicit charset parameter, or if it is one of the types for which MATLAB knows the default charset:

  • "text/*" - US-ASCII or UTF-8 depending on the subtype.

  • "application/*" - UTF-8 for subtypes: json, xml, javascript, css, x-www-form-urlencoded; unknown otherwise.

If this consumer cannot determine the charset from the ContentType in the message, then this consumer rejects the message and it is not converted. In that case, the ResponseMessage.Body contains only a uint8 payload. If you want to convert a message with an unknown charset, then set Charset in this consumer before applying it to a message (or, if you are a subclass author, before calling the initialize method). A good one to use is UTF-8 because that is a superset of US-ASCII and some other charsets.

Properties

expand all

The character set used to convert the data, specified as a string. This value is initially empty. If you leave it empty, then this value is set when a message is received, based on the specified or default charset in the Content-Type field of the message. If you want to force conversion using a different charset, then you can do so by specifying a charset in the StringConsumer constructor, or directly setting this property. Subclasses can set this property at any time, including in the middle of a message. If you change this value after calling putData, then the new value is used in subsequent calls to putData. The existing contents of Response.Body.Data is not changed.

When receipt of a response begins, if this property was initially left empty, then MATLAB sets this property to the chosen charset based on the Content-Type. If you reuse this consumer for a different message, then a new charset can be chosen. If you set this property to a nonempty value, MATLAB never changes it.

Attributes:

Dependenttrue
GetAccesspublic
SetAccesspublic

The type of data to return, specified as "string" or "char". The value is from the 'TextType' parameter to the constructor. Default is "string". If you change this value after data is already stored in Response.Body.Data, then that data is converted to the new type.

Attributes:

Dependenttrue
GetAccesspublic
SetAccesspublic

Inherited Properties Set by MATLAB or Subclasses

Suggested buffer size, specified as uint64. MATLAB sets AllocationLength to the anticipated size of buffers of data passed to putData. The actual size might be smaller or larger. To improve performance, the consumer can use this value to preallocate space to handle the data.

MATLAB sets this property before calling start for the convenience of subclasses.

Attributes:

GetAccesspublic
SetAccesspublic

Expected length of the payload, specified as uint64. The property normally is the Value property of the matlab.net.http.field.ContentLengthField in the Header property.

If ContentLength is empty, then the length is not known. The payload ends when putData(uint8.empty) is called.

MATLAB sets this property before calling initialize, for the convenience of subclasses that might benefit from knowing the length of the data.

If this ContentConsumer is a delegate of a top-level consumer, then the value of ContentLength might be different from the ContentLength value of the top-level consumer.

Attributes:

GetAccesspublic
SetAccesspublic

MediaType of payload, specified as a matlab.net.http.MediaType object. The property normally is the Value property of the matlab.net.http.field.ContentTypeField in the Header property. If the ContentType property is empty, then the ContentTypeField is empty or nonexistent.

MATLAB sets this property before calling initialize, for the convenience of subclasses that might want to examine the MediaType. Subclasses can set this property if they determine from the data that it is of a different MediaType.

At the end of the transfer, MATLAB copies this value into the Response.Body.ContentType property.

Attributes:

GetAccesspublic
SetAccesspublic

Header of the payload currently being processed, specified as a matlab.net.http.HeaderField object.

Consumers use this header to determine how to process the payload that is being sent to them. For a top-level consumer, this value is the same as Response.Header. For a delegate, the value might be different. For example, in a multipart message processed by a MultipartConsumer, it is the header of the part that this delegate is processing. The delegate can still examine Response.Header for headers of the original message.

MATLAB sets this property before calling initialize, for the convenience of subclasses.

Attributes:

GetAccesspublic
SetAccesspublic

The completed RequestMessage that was sent, specified as a matlab.net.http.RequestMessage object. This is the final RequestMessage after all redirections, which is the completedrequest return value from the matlab.net.http.RequestMessage.send method.

MATLAB sets this property before calling initialize, for the convenience of subclasses.

Attributes:

GetAccesspublic
SetAccesspublic

The ResponseMessage being processed, specified as a matlab.net.http.ResponseMessage object.

MATLAB sets the Response property before calling initialize. The value is the ResponseMessage after headers have been received but before receiving any payload. At the start of the response message processing (or the start of a part for multipart messages), the ResponseMesssage.Body property is a MessageBody object with empty Data and Payload properties. To store received data, consumers can modify the Response and MessageBody.Data properties during data transfer. Usually, consumers that process and then store data set Response.Body.Data to their processed payload, though this is not required. At the completion of the transfer, MATLAB returns this Response to the caller of matlab.net.http.RequestMessage.send. Consumers should not modify other Response properties, such as Header or StatusLine, as those changes are returned to the caller of sendy.

The Response.Body.Payload property is empty during the transfer and consumers should not attempt to modify it. If the SavePayload property is set, then MATLAB sets Payload to the received payload at the end of the transfer of the message or the part (after the call to putData(uint8.empty)) or when an exception occurs.

If an exception occurs in the consumer during message processing, then MATLAB throws an HTTPException object. The History property contains this Response value.

If the consumer is a delegate that is processing part of a multipart message, then Response.Header contains the header of the whole message, and the Payload and Data properties of Response.Body are cleared before invoking the ContentConsumer for each part. At the conclusion of each part, a new ResponseMessage is added to the end of the array of ResponseMessage objects in the original response's Body.Data containing the Header from this object and the Body from this property. The next delegate sees a fresh Response with an empty MessageBody, not the previous delegate's MessageBody.

Attributes:

GetAccesspublic
SetAccesspublic

Destination of the request being processed, specified as a matlab.net.URI object. This value is the original destination URI as determined by matlab.net.http.RequestMessage.send. It is not the URI of a proxy or the final URI after redirections.

MATLAB sets this property before calling initialize, for the convenience of subclasses.

Attributes:

GetAccesspublic
SetAccesspublic

Protected, Set by MATLAB or Subclasses

Function, specified as a function handle, called by the putData method to append additional data. The putData method in this class calls the AppendFcn function to append data it receives in its data argument to existing data in the response message. The function must have the signature:

AppendFcn(consumer,newdata)

where newdata is the data to be appended to the array at consumer.Response.Body.Data. It is the responsibility of this method to update consumer.CurrentLength to reflect the new length of Data. If newdata is empty, which indicates the end of the stream, then the function should update Response.Body.Data to its final value.

The default behavior, if this property is empty, uses an internal function that treats Data as an array of arbitrary values supporting the horzcat function. It efficiently adds newdata by preallocating space, maintaining CurrentLength to be the actual length of data stored. At the end of the message, it truncates Response.Body.Data to CurrentLength.

If horzcat is not appropriate for the append process, then subclasses can change this property. For example, when a StringConsumer builds a scalar string, it would add to the string using the plus function instead of horzcat.

Subclasses that do not invoke ContentConsumer.putData to append data, or which are satisfied with horzcat behavior when appending data, can ignore this property.

Attributes:

GetAccessprotected
SetAccessprotected

The ContentConsumer to which this consumer is delegating, specified as a matlab.net.http.io.ContentConsumer object. The delegateTo of the calling consumer (the delegator) sets the CurrentDelegate property. If there is no current delegation, then the value is [].

Attributes:

GetAccessprotected
SetAccessprotected

Length of data currently in the Response.Body.Data property, specified as uint64.

This property is used when Response.Body.Data has been preallocated to a size larger than the actual amount of data currently stored, to indicate the length of that stored data. If this property is empty, then it means that all of Response.Body.Data contains the stored data or that a ContentConsumer subclass is disposing of the data in some way other than storing it in Response.Body.Data.

This property is used and set by the putData in this base class when the AppendFcn property is empty. It is for the benefit of subclasses that call putData and want to examine already-stored data, and/or any implementations of AppendFcn that maintain results in Response.Body.Data.

Subclasses that use putData also can modify this property to reset the position in the buffer where the data is stored. For example, when the default AppendFcn function is used, a subclass that processes all of Response.Body.Data on each call to putData might no longer have a use for the original data, so it can reset the CurrentLength property to 1 so that the next putData call overwrites the buffer with new data. There is no need to clear elements in the buffer past the end of the new data.

Subclasses that do not call putData can use this property to track their own data, or can leave it unset (empty). MATLAB does not place any constraints on the value that can be set here and does not use it for any purpose other than to determine where the default AppendFcn should store the next buffer of data, and where to truncate the data at the end of the message. Set this property to empty before the final call to putData(uint8.empty) to prevent truncation of the data.

MATLAB sets this property to empty before each call to initialize.

Attributes:

GetAccessprotected
SetAccessprotected

The ContentConsumer that delegated to this consumer, specified as a matlab.net.http.io.ContentConsumer object. If this consumer is a delegate that was invoked by another consumer, such as a GenericConsumer or MultipartConsumer, then this is the calling consumer. It is empty in a top-level consumer specified in the call to matlab.net.http.RequestMessage.send.

Delegates can use this property to access properties of their delegators, for example, to determine which consumer delegated to them.

Attributes:

GetAccesspublic
SetAccesspublic

Methods

convertConvert data to string in StringConsumer
initializePrepare StringConsumer for new HTTP message
putDataAppend next buffer of string data to response
startStart data transfer to StringConsumer

Inherited Methods

delegateToDelegate to another consumer
initializePrepare consumer for new HTTP payload
putDataProcess or save next buffer of data for ContentConsumer
startStart HTTP data transfer to ContentConsumer

Class Hierarchy

Introduced in R2018a