ApplicationAsset class is a container that refers to an asset in the system and allows you to stream the contents. When you use
System.assetsAvailable, you will be passed a
ApplicationAsset objects. You will typically iterate this vector and process each asset in the asset bundle.
You can read the data byte-by-byte or by buffer. Because it inherits from
Stream class, you can use those methods as well.
// PROTOTYPE String name() const;
Returns the asset filename as a
String. This is relative to the asset directory and does not include the name of the asset directory when you generated the bundle.
// PROTOTYPE AssetHash hash() const;
Returns the SHA-256 hash of the asset. See
AssetHash for more information.
// PROTOTYPE size_t size() const;
Returns the size of the asset in bytes. This is unaffected by the current read position.
// PROTOTYPE size_t storageSize() const;
Returns how many bytes the asset takes on the device storage. This is typically smaller than
size() since assets are stored compressed.
// PROTOTYPE bool isValid() const;
true if the asset appears to be valid (has a name and a hash).
// PROTOTYPE bool isReadable();
true if there are bytes to read (asset not empty, not at end of file).
// PROTOTYPE int available() override;
Returns the number of bytes available to read, or 0 if at end of the asset or an error occurred.
available() may not return the total size of the asset on the first call. For that, use
// PROTOTYPES int read() override; virtual int read(char* buffer, size_t size);
Read and return a single byte, or read multiple bytes into your buffer.
Even though the buffer is a
char * it can be binary data, and is not null terminated. The return value for reading a buffer is the number of bytes read, or a negative system error code.
// PROTOTYPES int peek() override; virtual int peek(char* buffer, size_t size);
Read data into a buffer without consuming it, so the next read will re-read the data. Streams are not seekable or rewindable.
// PROTOTYPE virtual int skip(size_t size);
Skip the specified number of bytes so the next
readBuffer(), etc. will read from that point. Streams are not backward seekable, however you can
reset() the stream to rewind it back to the beginning.
// PROTOTYPE virtual void reset();
Resets the internal state of the
ApplicationAsset. You can use this to rewind the stream back to the beginning and process it over again from the first byte.
A common use case is when your external peripheral did not complete the update successfully and you want to try again immediately.