ByteArrayResource class

A resource over bytes held in memory.

Use it where an API wants a Resource but the content is already in hand: in tests, for text built at run time, or for a download kept in memory. It always exists and can be read synchronously. The bytes are copied on creation and on every read, so the content cannot change.

// Properties built in code, read like a file.
final resource = ByteArrayResource.fromString(
  'app.name=demo',
  description: 'inline config',
);
final props = PropertiesLoaderUtils.loadPropertiesSync(resource);

Spring (Java):

Resource resource = new ByteArrayResource(
    "app.name=demo".getBytes(StandardCharsets.UTF_8), "inline config");
Properties props = PropertiesLoaderUtils.loadProperties(resource);
Inheritance

Constructors

ByteArrayResource(Uint8List bytes, {String? description})
Creates a resource over a copy of bytes, described by description (Byte array resource unless given).
ByteArrayResource.fromString(String text, {String? description})
Creates a resource whose content is text, encoded as UTF-8.
factory

Properties

description → String
A text that tells people which resource this is, for error messages, such as file [/app/config/application.properties]. It is also the default name of a property source read from it, and two resources of the same type with the same description are equal.
final
filename → String?
The last part of the path, such as application.yml, or null when the resource has no file name (bytes in memory).
no setterinherited
hashCode → int
The hash code for this object.
no setteroverride
isReadable → bool
Whether the content can be read; usually the same as exists, but false for a directory.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
supportsSyncRead → bool
Whether readAsBytesSync and readAsStringSync work. True for files and the embedded classpath; false for URLs and Flutter assets.
no setteroverride
uri → Uri?
The URI of the resource (file:, https:, ...), or null when it has none.
no setterinherited

Methods

createRelative(String relativePath) → Resource
The resource at relativePath from this one, such as a sibling file next to a config file. It is not checked to exist. Throws UnsupportedError for resources without a location (bytes in memory).
inherited
exists() → bool
Whether the content is there: the file exists, the classpath entry is embedded. Check it before reading a resource that may be missing. A UrlResource cannot ask the server synchronously and always says true.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
readAsBytes() → Future<Uint8List>
Reads the whole content as bytes. Works for every resource that exists; fails for one that does not.
override
readAsBytesSync() → Uint8List
Reads the whole content as bytes without waiting, for code that cannot be asynchronous. Throws UnsupportedError unless supportsSyncRead.
override
readAsString({Encoding encoding = utf8}) → Future<String>
Reads the whole content as text, decoded with encoding (UTF-8 unless given).
inherited
readAsStringSync({Encoding encoding = utf8}) → String
Reads the whole content as text without waiting, decoded with encoding. Throws UnsupportedError unless supportsSyncRead.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
Whether other is a byte array resource with the same description and the same bytes.
override