Skip to content

Commit

Permalink
Remove EncodedString.
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos committed Sep 24, 2023
1 parent ae390ab commit 0c30705
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 44 deletions.
4 changes: 2 additions & 2 deletions lib/src/auth/sasl_authenticator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ class SaslClientFirstMessage extends ClientMessage {
void applyToBuffer(PgByteDataWriter buffer) {
buffer.writeUint8(ClientMessage.PasswordIdentifier);

final encodedMechanismName = buffer.prepareString(mechanismName);
final encodedMechanismName = buffer.encodeString(mechanismName);
final msgLength = bytesToSendToServer.length;
// No Identifier bit + 4 byte counts (for whole length) + mechanism bytes + zero byte + 4 byte counts (for msg length) + msg bytes
final length = 4 + encodedMechanismName.bytesLength + 1 + 4 + msgLength;
final length = 4 + encodedMechanismName.length + 1 + 4 + msgLength;

buffer.writeUint32(length);
buffer.writeEncodedString(encodedMechanismName);
Expand Down
25 changes: 9 additions & 16 deletions lib/src/buffer.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import 'dart:convert';
import 'dart:typed_data';

import 'package:buffer/buffer.dart';

class EncodedString {
final List<int> _bytes;
EncodedString.fromValue(String value, {Encoding encoding = utf8})
: _bytes = encoding.encode(value);

int get bytesLength => _bytes.length;
}

class PgByteDataWriter extends ByteDataWriter {
final Encoding _encoding;

Expand All @@ -18,21 +11,21 @@ class PgByteDataWriter extends ByteDataWriter {
Encoding encoding = utf8,
}) : _encoding = encoding;

late final encodingName = prepareString(_encoding.name);
late final encodingName = encodeString(_encoding.name);

EncodedString prepareString(String value) {
return EncodedString.fromValue(value, encoding: _encoding);
Uint8List encodeString(String value) {
return castBytes(_encoding.encode(value));
}

void writeEncodedString(EncodedString value) {
write(value._bytes);
void writeEncodedString(Uint8List value) {
write(value);
writeInt8(0);
}

void writeLengthEncodedString(String value) {
final encoded = EncodedString.fromValue(value, encoding: _encoding);
writeUint32(5 + encoded.bytesLength);
write(encoded._bytes);
final encoded = encodeString(value);
writeUint32(5 + value.length);
write(encoded);
writeInt8(0);
}
}
47 changes: 22 additions & 25 deletions lib/src/client_messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,21 @@ class StartupMessage extends ClientMessage {

@override
void applyToBuffer(PgByteDataWriter buffer) {
final databaseName = buffer.prepareString(_databaseName);
final timeZone = buffer.prepareString(_timeZone);
final username =
_username == null ? null : buffer.prepareString(_username!);
final replication = buffer.prepareString(_replication);
var fixedLength = 44 + buffer.encodingName.bytesLength;
var variableLength = databaseName.bytesLength + timeZone.bytesLength + 2;
final databaseName = buffer.encodeString(_databaseName);
final timeZone = buffer.encodeString(_timeZone);
final username = _username == null ? null : buffer.encodeString(_username!);
final replication = buffer.encodeString(_replication);
var fixedLength = 44 + buffer.encodingName.length;
var variableLength = databaseName.length + timeZone.length + 2;

if (username != null) {
fixedLength += 5;
variableLength += username.bytesLength + 1;
variableLength += username.length + 1;
}

if (_replication != ReplicationMode.none.value) {
fixedLength += UTF8ByteConstants.replication.length;
variableLength += replication.bytesLength + 1;
variableLength += replication.length + 1;
}

buffer.writeInt32(fixedLength + variableLength);
Expand Down Expand Up @@ -134,12 +133,10 @@ class ParseMessage extends ClientMessage {
@override
void applyToBuffer(PgByteDataWriter buffer) {
buffer.writeUint8(ClientMessage.ParseIdentifier);
final statement = buffer.prepareString(_statement);
final statementName = buffer.prepareString(_statementName);
final length = 8 +
statement.bytesLength +
statementName.bytesLength +
_types.length * 4;
final statement = buffer.encodeString(_statement);
final statementName = buffer.encodeString(_statementName);
final length =
8 + statement.length + statementName.length + _types.length * 4;
buffer.writeUint32(length);
// Name of prepared statement
buffer.writeEncodedString(statementName);
Expand Down Expand Up @@ -168,8 +165,8 @@ class DescribeMessage extends ClientMessage {
@override
void applyToBuffer(PgByteDataWriter buffer) {
buffer.writeUint8(ClientMessage.DescribeIdentifier);
final name = buffer.prepareString(_name);
final length = 6 + name.bytesLength;
final name = buffer.encodeString(_name);
final length = 6 + name.length;
buffer.writeUint32(length);
buffer.writeUint8(_isPortal ? $P : $S);
buffer.writeEncodedString(name); // Name of prepared statement
Expand All @@ -189,8 +186,8 @@ class BindMessage extends ClientMessage {
@override
void applyToBuffer(PgByteDataWriter buffer) {
buffer.writeUint8(ClientMessage.BindIdentifier);
final portalName = buffer.prepareString(_portalName);
final statementName = buffer.prepareString(_statementName);
final portalName = buffer.encodeString(_portalName);
final statementName = buffer.encodeString(_statementName);

final parameterBytes = _parameters.map((p) => p.encodeAsBytes()).toList();
final typeSpecCount = _parameters.where((p) => p.hasKnownType).length;
Expand All @@ -200,8 +197,8 @@ class BindMessage extends ClientMessage {
}

var length = 14;
length += statementName.bytesLength;
length += portalName.bytesLength;
length += statementName.length;
length += portalName.length;
length += inputParameterElementCount * 2;
length += parameterBytes.fold<int>(
0, (len, bytes) => len + 4 + (bytes?.length ?? 0));
Expand Down Expand Up @@ -258,8 +255,8 @@ class ExecuteMessage extends ClientMessage {
@override
void applyToBuffer(PgByteDataWriter buffer) {
buffer.writeUint8(ClientMessage.ExecuteIdentifier);
final portalName = buffer.prepareString(_portalName);
buffer.writeUint32(9 + portalName.bytesLength);
final portalName = buffer.encodeString(_portalName);
buffer.writeUint32(9 + portalName.length);
buffer.writeEncodedString(portalName);
buffer.writeUint32(0);
}
Expand All @@ -279,8 +276,8 @@ class CloseMessage extends ClientMessage {

@override
void applyToBuffer(PgByteDataWriter buffer) {
final name = buffer.prepareString(_name);
final length = 6 + name.bytesLength;
final name = buffer.encodeString(_name);
final length = 6 + name.length;

buffer
..writeUint8(ClientMessage.CloseIdentifier)
Expand Down
1 change: 0 additions & 1 deletion lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class UTF8ByteConstants {
103,
0
];
static const utf8 = <int>[85, 84, 70, 56, 0];
static const timeZone = <int>[84, 105, 109, 101, 90, 111, 110, 101, 0];
static const replication = <int>[
114,
Expand Down

0 comments on commit 0c30705

Please sign in to comment.