Base64Pipes final
Pipes for base64 encoding and decoding.
Obtained via Pipes.base64 or Base64Pipes(). All operations are streaming — large inputs are processed chunk-by-chunk without buffering the entire input in memory.
final encoded = Rill.emits(['SGVsbG8='])
.through(Pipes.base64.decode);Constructors
Base64Pipes() factory
Returns the singleton Base64Pipes instance.
Implementation
factory Base64Pipes() => _singleton;Properties
decode no setter
Decodes base64-encoded strings to raw bytes using the standard alphabet.
Implementation
Pipe<String, int> get decode => decodeWithAlphabet(Alphabets.base64);encode no setter
Encodes raw bytes to base64 strings using the standard alphabet.
Implementation
Pipe<int, String> get encode => encodeWithAlphabet(Alphabets.base64);hashCode no setter inherited
The hash code for this object.
A hash code is a single integer which represents the state of the object that affects operator == comparisons.
All objects have hash codes. The default hash code implemented by Object represents only the identity of the object, the same way as the default operator == implementation only considers objects equal if they are identical (see identityHashCode).
If operator == is overridden to use the object state instead, the hash code must also be changed to represent that state, otherwise the object cannot be used in hash based data structures like the default Set and Map implementations.
Hash codes must be the same for objects that are equal to each other according to operator ==. The hash code of an object should only change if the object changes in a way that affects equality. There are no further requirements for the hash codes. They need not be consistent between executions of the same program and there are no distribution guarantees.
Objects that are not equal are allowed to have the same hash code. It is even technically allowed that all instances have the same hash code, but if clashes happen too often, it may reduce the efficiency of hash-based data structures like HashSet or HashMap.
If a subclass overrides hashCode, it should override the operator == operator as well to maintain consistency.
Inherited from Object.
Implementation
external int get hashCode;runtimeType no setter inherited
A representation of the runtime type of the object.
Inherited from Object.
Implementation
external Type get runtimeType;Methods
decodeWithAlphabet()
Decodes base64-encoded strings to raw bytes using alphabet.
Implementation
Pipe<String, int> decodeWithAlphabet(Base64Alphabet alphabet) {
const paddingError =
'Malformed padding - final quantum may optionally be padded with one or two padding characters such that the quantum is completed';
Either<String, (_State, Chunk<int>)> decode(_State state, String str) {
var buffer = state.buffer;
var mod = state.mod;
var padding = state.padding;
var idx = 0;
var bidx = 0;
final acc = List.filled((str.length + 3) ~/ 4 * 3, 0);
while (idx < str.length) {
final c = str[idx];
if (!alphabet.ignore(c)) {
late int cidx;
if (padding == 0) {
if (c == alphabet.pad) {
if (mod == 2 || mod == 3) {
padding += 1;
cidx = 0;
} else {
return paddingError.asLeft();
}
} else {
try {
cidx = alphabet.toIndex(c);
} catch (e) {
return "Invalid base 64 character '$c' at index $idx".asLeft();
}
}
} else if (c == alphabet.pad) {
if (padding == 1 && mod == 3) {
padding += 1;
cidx = 0;
} else {
return paddingError.asLeft();
}
} else {
return "Unexpected character '$c' at index $idx after padding character; only '=' and whitespace characters allowed after first padding character"
.asLeft();
}
if (mod == 0) {
buffer = cidx & 0x3f;
mod += 1;
} else if (mod == 1 || mod == 2) {
buffer = (buffer << 6) | (cidx & 0x3f);
mod += 1;
} else if (mod == 3) {
buffer = (buffer << 6) | (cidx & 0x3f);
mod = 0;
acc[bidx] = (buffer >> 16) & 0xff;
acc[bidx + 1] = (buffer >> 8) & 0xff;
acc[bidx + 2] = buffer & 0xff;
bidx += 3;
}
}
idx += 1;
}
final paddingInBuffer = mod == 0 ? padding : 0;
final out = Chunk.fromList(acc.toIList().take(bidx - paddingInBuffer).toList());
final carry = _State(buffer, mod, padding);
return (carry, out).asRight();
}
Either<String, Chunk<int>> finish(_State state) {
if (state.padding != 0 && state.mod != 0) {
return paddingError.asLeft();
} else {
return switch (state.mod) {
0 => Chunk.empty<int>().asRight(),
1 => 'Final base 64 quantum had only 1 digit - must have at least 2 digits'.asLeft(),
2 => chunk([(state.buffer >> 4) & 0xff]).asRight(),
3 =>
chunk([
(state.buffer >> 10) & 0xff,
(state.buffer >> 2) & 0xff,
]).asRight(),
_ => 'base64 decode bad mod: ${state.mod}'.asLeft(),
};
}
}
Pull<int, Unit> go(_State state, Rill<String> rill) {
return rill.pull.uncons1.flatMap((hdtl) {
return hdtl.foldN(
() => finish(state).fold(
(err) => Pull.raiseError(err),
(out) => Pull.output(out),
),
(hd, tl) => decode(state, hd).foldN(
(err) => Pull.raiseError(err),
(newState, out) => Pull.output(out).append(() => go(newState, tl)),
),
);
});
}
return (rill) => go(_State(0, 0, 0), rill).rillNoScope;
}encodeWithAlphabet()
Encodes raw bytes to base64 strings using alphabet.
Implementation
Pipe<int, String> encodeWithAlphabet(Base64Alphabet alphabet) {
(String, ByteVector) encode(ByteVector c) {
final bytes = c.toByteArray();
final bldr = StringBuffer();
var idx = 0;
final mod = bytes.length % 3;
while (idx < bytes.length - mod) {
var buffer =
((bytes[idx] & 0xff) << 16) | ((bytes[idx + 1] & 0xff) << 8) | (bytes[idx + 2] & 0xff);
final fourth = buffer & 0x3f;
buffer = buffer >> 6;
final third = buffer & 0x3f;
buffer = buffer >> 6;
final second = buffer & 0x3f;
buffer = buffer >> 6;
final first = buffer;
bldr
..write(alphabet.toChar(first))
..write(alphabet.toChar(second))
..write(alphabet.toChar(third))
..write(alphabet.toChar(fourth));
idx += 3;
}
final out = bldr.toString().split('').join();
if (mod == 0) {
return (out, ByteVector.empty);
} else if (mod == 1) {
return (out, ByteVector.of(bytes[idx]));
} else {
return (out, ByteVector.fromDart([bytes[idx], bytes[idx + 1]]));
}
}
Pull<String, Unit> go(ByteVector carry, Rill<int> rill) {
return rill.pull.uncons.flatMap((hdtl) {
return hdtl.foldN(
() {
switch (carry.size) {
case 0:
return Pull.done;
case 1:
var buffer = (carry[0] & 0xff) << 4;
final second = buffer & 0x3f;
buffer = buffer >> 6;
final first = buffer;
final out =
alphabet.toChar(first) + alphabet.toChar(second) + alphabet.pad + alphabet.pad;
return Pull.output1(out);
case 2:
var buffer = ((carry[0] & 0xff) << 10) | ((carry[1] & 0xff) << 2);
final third = buffer & 0x3f;
buffer = buffer >> 6;
final second = buffer & 0x3f;
buffer = buffer >> 6;
final first = buffer;
final out =
alphabet.toChar(first) +
alphabet.toChar(second) +
alphabet.toChar(third) +
alphabet.pad;
return Pull.output1(out);
default:
return Pull.raiseError('carry must be size 0, 1 or 2 but was: ${carry.size}');
}
},
(hd, tl) {
final (out, newCarry) = encode(carry.concat(ByteVector.from(hd)));
return Pull.output1(out).append(() => go(newCarry, tl));
},
);
});
}
return (rill) => go(ByteVector.empty, rill).rillNoScope;
}noSuchMethod() inherited
Invoked when a nonexistent method or property is accessed.
A dynamic member invocation can attempt to call a member which doesn't exist on the receiving object. Example:
dynamic object = 1;
object.add(42); // Statically allowed, run-time errorThis invalid code will invoke the noSuchMethod method of the integer 1 with an Invocation representing the .add(42) call and arguments (which then throws).
Classes can override noSuchMethod to provide custom behavior for such invalid dynamic invocations.
A class with a non-default noSuchMethod invocation can also omit implementations for members of its interface. Example:
class MockList<T> implements List<T> {
noSuchMethod(Invocation invocation) {
log(invocation);
super.noSuchMethod(invocation); // Will throw.
}
}
void main() {
MockList().add(42);
}This code has no compile-time warnings or errors even though the MockList class has no concrete implementation of any of the List interface methods. Calls to List methods are forwarded to noSuchMethod, so this code will log an invocation similar to Invocation.method(#add, [42]) and then throw.
If a value is returned from noSuchMethod, it becomes the result of the original invocation. If the value is not of a type that can be returned by the original invocation, a type error occurs at the invocation.
The default behavior is to throw a NoSuchMethodError.
Inherited from Object.
Implementation
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);toString() inherited
A string representation of this object.
Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.
Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.
Inherited from Object.
Implementation
external String toString();Operators
operator ==() inherited
The equality operator.
The default behavior for all Objects is to return true if and only if this object and other are the same object.
Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:
Total: It must return a boolean for all arguments. It should never throw.
Reflexive: For all objects
o,o == omust be true.Symmetric: For all objects
o1ando2,o1 == o2ando2 == o1must either both be true, or both be false.Transitive: For all objects
o1,o2, ando3, ifo1 == o2ando2 == o3are true, theno1 == o3must be true.
The method should also be consistent over time, so whether two objects are equal should only change if at least one of the objects was modified.
If a subclass overrides the equality operator, it should override the hashCode method as well to maintain consistency.
Inherited from Object.
Implementation
external bool operator ==(Object other);