File tree Expand file tree Collapse file tree 5 files changed +102
-0
lines changed
patterns/strategy/view_strategy Expand file tree Collapse file tree 5 files changed +102
-0
lines changed Original file line number Diff line number Diff line change 1+ import 'view_strategy.dart' ;
2+
3+ class ByteContext {
4+ String toStringView (ViewStrategy strategy) {
5+ return '${strategy .runtimeType }:\n '
6+ '${strategy .out (this )}' ;
7+ }
8+
9+ void add (dynamic value) {
10+ _buf.add (value);
11+ }
12+
13+ List toList () => _buf;
14+
15+ final _buf = < dynamic > [];
16+ }
Original file line number Diff line number Diff line change 1+ import 'byte_context.dart' ;
2+
3+ abstract class ViewStrategy {
4+ String out (ByteContext byteList);
5+ }
Original file line number Diff line number Diff line change 1+ import 'dart:typed_data' ;
2+
3+ import '../pattern/byte_context.dart' ;
4+ import '../pattern/view_strategy.dart' ;
5+
6+ class HexViewStrategy implements ViewStrategy {
7+ @override
8+ String out (ByteContext byteList) {
9+ final buf = StringBuffer ();
10+
11+ for (final val in byteList.toList ()) {
12+ buf.write ('${val .toString ().padRight (15 )}: ' );
13+
14+ if (val is String ) {
15+ buf.writeln (_stringToHex (val));
16+ } else {
17+ buf.writeln (_valueToHex (val, size: 8 ));
18+ }
19+ }
20+
21+ return buf.toString ();
22+ }
23+
24+ String _stringToHex (String value) {
25+ return value.codeUnits
26+ .map ((charCode) => _valueToHex (size: 1 , charCode))
27+ .join (' ' );
28+ }
29+
30+ String _valueToHex <T >(T value, {required int size}) {
31+ late ByteData byteData;
32+
33+ if (size == 1 ) {
34+ byteData = ByteData (1 )..setInt8 (0 , value as int );
35+ } else {
36+ byteData = ByteData (size);
37+ if (value is double ) {
38+ byteData.setFloat64 (0 , value);
39+ } else if (value is int ) {
40+ byteData.setInt64 (0 , value);
41+ }
42+ }
43+
44+ final bytes = byteData.buffer.asUint8List ();
45+ return bytes.map ((e) => e.toRadixString (16 ).padLeft (2 , '0' )).join (' ' );
46+ }
47+ }
Original file line number Diff line number Diff line change 1+ import '../pattern/byte_context.dart' ;
2+ import '../pattern/view_strategy.dart' ;
3+
4+ class StrViewStrategy implements ViewStrategy {
5+ @override
6+ String out (ByteContext byteList) {
7+ return '${byteList .toList ().join (', ' )}\n ' ;
8+ }
9+ }
Original file line number Diff line number Diff line change 1+ import 'dart:io' ;
2+
3+ import '../pattern/byte_context.dart' ;
4+ import 'str_view_strategy.dart' ;
5+
6+ class ZipViewStrategy extends StrViewStrategy {
7+ @override
8+ String out (ByteContext byteList) {
9+ final codes = super .out (byteList).codeUnits;
10+ final bytes = GZipCodec ().encode (codes);
11+ final buf = StringBuffer ();
12+
13+ var odd = 1 ;
14+ for (final byte in bytes) {
15+ final hexByte = byte.toRadixString (16 ).padLeft (2 , '0' );
16+ buf.write ('$hexByte ' );
17+
18+ if (odd++ % 16 == 0 ) {
19+ buf.writeln ();
20+ }
21+ }
22+
23+ return buf.toString ();
24+ }
25+ }
You can’t perform that action at this time.
0 commit comments