-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathbuffer.dart
61 lines (48 loc) · 1.45 KB
/
buffer.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import 'dart:convert';
import 'package:buffer/buffer.dart';
import 'package:postgres/src/timezone_settings.dart';
/// This class doesn't add much over using `List<int>` instead, however,
/// it creates a nice explicit type difference from both `String` and `List<int>`,
/// and it allows better use for the string encoding that delimits the value with `0`.
class EncodedString {
final List<int> _bytes;
EncodedString._(this._bytes);
int get bytesLength => _bytes.length;
}
class PgByteDataWriter extends ByteDataWriter {
final Encoding encoding;
PgByteDataWriter({
super.bufferLength,
required this.encoding,
});
late final encodingName = encodeString(encoding.name);
EncodedString encodeString(String value) {
return EncodedString._(encoding.encode(value));
}
void writeEncodedString(EncodedString value) {
write(value._bytes);
writeInt8(0);
}
void writeLengthEncodedString(String value) {
final encoded = encodeString(value);
writeUint32(5 + encoded.bytesLength);
write(encoded._bytes);
writeInt8(0);
}
}
const _emptyString = '';
class PgByteDataReader extends ByteDataReader {
final Encoding encoding;
final TimeZoneSettings timeZone;
PgByteDataReader({
required this.encoding,
required this.timeZone,
});
String readNullTerminatedString() {
final bytes = readUntilTerminatingByte(0);
if (bytes.isEmpty) {
return _emptyString;
}
return encoding.decode(bytes);
}
}