Prepare consumer for new HTTP payload


OK = initialize(consumer)


OK = initialize(consumer) prepares a ContentConsumer for use with a new HTTP payload.

MATLAB® calls initialize after receipt of the header of a ResponseMessage that might contain a payload, to prepare the consumer for that payload. It is not invoked for messages not expected to contain a payload, such as those with an explicit Content-Length of 0, or in error cases where a complete header was not received.

The delegateTo method in a delegate consumer also calls initialize.

If you subclass a ContentConsumer, then you should at least check the ContentType property to verify that the response is of the type you are prepared to handle. You can override this method to, for example:

  • Initialize your own properties

  • Determine if you want to process the payload

  • Process a payload that has a Response.StatusCode other than OK

It is up to you whether to perform any subsequent initializations in this method or delay them until the start method.

Even if initialize is called, MATLAB might not call the consumer's start method if the message has no payload.

The default implementation returns true if the Response.StatusCode is OK and false otherwise. Subclasses that override this method should invoke this superclass method first and check the return value, unless they want to process messages with a status other than OK. Subclasses that invoke putData in this class must call this method.

Consumer subclasses should be prepared to be reused for subsequent messages. MATLAB calls initialize before each message and then start for each message that has a nonempty payload. Once MATLAB calls start, it does not call initialize until the message has ended, an exception was thrown, or an interrupt occurred during message processing. All these cases are indicated by a call to putData(uint8.empty).

Input Arguments

expand all

Content consumer, specified as a object.

Output Arguments

expand all

Indicate whether the consumer accepts or rejects the payload of the message, returned as true or false.

  • If the status code of the ResponseMessage is StatusCode.OK, then the default is true.

  • If OK is true, then the consumer accepted the message and processes the payload, if any. MATLAB then calls the consumer's start method when the first byte of the payload arrives, followed by one or more calls to putData, passing a buffer of data on each call.

  • If OK is false, then the consumer does not want to process the message, in which case MATLAB processes the payload as if no consumer had been specified (which might mean default conversion of payload to data).

If you override the initialize method and reject the message and you want to abort receipt of the message instead of processing it in a default manner, then throw an error from this method instead of returning false.



Introduced in R2018a