UriData.fromString constructor

UriData.fromString(String content, { String mimeType, Encoding encoding, Map<String, String> parameters, bool base64: false })

Creates a data: URI containing the content string.

Equivalent to new Uri.dataFromString(...).data, but may be more efficient if the uri itself isn't used.

Implementation

factory UriData.fromString(String content,
    {String mimeType,
    Encoding encoding,
    Map<String, String> parameters,
    bool base64: false}) {
  StringBuffer buffer = new StringBuffer();
  List<int> indices = [_noScheme];
  String charsetName;
  String encodingName;
  if (parameters != null) charsetName = parameters["charset"];
  if (encoding == null) {
    if (charsetName != null) {
      encoding = Encoding.getByName(charsetName);
    }
  } else if (charsetName == null) {
    // Non-null only if parameters does not contain "charset".
    encodingName = encoding.name;
  }
  encoding ??= ascii;
  _writeUri(mimeType, encodingName, parameters, buffer, indices);
  indices.add(buffer.length);
  if (base64) {
    buffer.write(';base64,');
    indices.add(buffer.length - 1);
    buffer.write(encoding.fuse(_base64).encode(content));
  } else {
    buffer.write(',');
    _uriEncodeBytes(_uricTable, encoding.encode(content), buffer);
  }
  return new UriData._(buffer.toString(), indices, null);
}