byte_bloc 0.2.5

  • Readme
  • Changelog
  • Example
  • Installing
  • 53

byte_bloc #

ByteBloc simplify binary array parsing for C/C++ structures.

Usage #

Import #

import 'package:byte_bloc/byte_bloc.dart';

C/C++ structure #

Example of C/C++ structure from embeded device

struct DeviceStructure
{
    u_int32_t type;
    char name[12];
    unsigned isActive        :1;
    unsigned flags           :8;
};

Binary array #

Binary array of the structure will look like

[223, 0, 77, 121, 32, 68, 101, 118, 105, 99, 101, 0, 0, 0, 7, 0, 0, 0];

Model #

From dart side we can implement model with factory fromByteBloc and method toByteBloc

class DeviceStructure {
  static const int NAME_SIZE = 12;
  static const int FLAGS_SIZE = 8;
  final int id;
  final String name;
  final bool isActive;
  final int flags;

  DeviceStructure({this.id, this.name, this.isActive, this.flags});

  factory DeviceStructure.fromByteBloc(ByteBloc byteBloc) {
    final id = byteBloc.readUint16();
    final name = byteBloc.readString(NAME_SIZE);
    final bitBloc = byteBloc.readBitBloc32();
    final isActive = bitBloc.readBool();
    final flags = bitBloc.readBits(FLAGS_SIZE);
    return DeviceStructure(id: id, name: name, isActive: isActive, flags: flags);
  }

  ByteBloc toByteBloc() {
    final byteBloc = ByteBloc.empty()
      ..writeUint16(id)
      ..writeString(name, NAME_SIZE)
      ..writeBitBloc32(BitBloc()
        ..writeBool(isActive)
        ..writeBits(flags, FLAGS_SIZE)
      )
      ..normalize();
    return byteBloc;
  }
}

Deserialize model #

final binaryArray = [223, 0, 77, 121, 32, 68, 101, 118, 105, 99, 101, 0, 0, 0, 7, 0, 0, 0];
final binaryArrayByteBloc = ByteBloc(Uint8List.fromList(binaryArray));
final deviceStructure = DeviceStructure.fromByteBloc(binaryArrayByteBloc);

Serialize model #

final resultByteBloc = deviceStructure.toByteBloc();
print(resultByteBloc.list);

TODO #

  • Write unit tests for ByteBloc write methods

0.2.5 #

  • Minor fixes

0.2.4 #

  • Minor fixes

0.2.3 #

  • Fix readByteBloc with nested blocs

0.2.2 #

  • Minor fixes

0.2.1 #

  • Write test for appendUint8List

0.2.0 #

  • Add toString
  • Add appendUint8List

0.1.9 #

  • Minor fixes

0.1.8 #

  • Minor fixes

0.1.7 #

  • Update ByteBloc.empty constructor with size

0.1.6 #

  • Minor fixes

0.1.5 #

  • Write examples for bits stricture

0.1.4 #

  • Minor fixes

0.1.3 #

  • Minor fixes

0.1.2 #

  • Minor fixes

0.1.1 #

  • Update readme

0.1.0 #

  • Add doc comments to public methods

0.0.1 #

  • Implement ByteBloc read / write functionslity

example/example.dart

import 'dart:typed_data';

import 'package:byte_bloc/byte_bloc.dart';

class DeviceStructure {
  static const int NAME_SIZE = 12;
  static const int FLAGS_SIZE = 8;
  final int id;
  final String name;
  final bool isActive;
  final int flags;

  DeviceStructure({this.id, this.name, this.isActive, this.flags});

  factory DeviceStructure.fromByteBloc(ByteBloc byteBloc) {
    final id = byteBloc.readUint16();
    final name = byteBloc.readString(NAME_SIZE);
    final bitBloc = byteBloc.readBitBloc32();
    final isActive = bitBloc.readBool();
    final flags = bitBloc.readBits(FLAGS_SIZE);
    return DeviceStructure(
      id: id,
      name: name,
      isActive: isActive,
      flags: flags,
    );
  }

  ByteBloc toByteBloc() {
    final byteBloc = ByteBloc.empty()
      ..writeUint16(id)
      ..writeString(name, NAME_SIZE)
      ..writeBitBloc32(BitBloc()
        ..writeBool(isActive)
        ..writeBits(flags, FLAGS_SIZE)
      )
      ..normalize();
    return byteBloc;
  }
}

main() async {
  final binaryArray = [223, 0, 77, 121, 32, 68, 101, 118, 105, 99, 101, 0, 0, 0, 7, 0, 0, 0];
  print(binaryArray);
  final binaryArrayByteBloc = ByteBloc(Uint8List.fromList(binaryArray));
  final deviceStructure = DeviceStructure.fromByteBloc(binaryArrayByteBloc);
  final resultByteBloc = deviceStructure.toByteBloc();
  print(resultByteBloc.list);
  assert(binaryArray.join(',') == resultByteBloc.list.join(','));
  print('Source and generated binary array are equal');
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  byte_bloc: ^0.2.5

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:byte_bloc/byte_bloc.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
7
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
53
Learn more about scoring.

We analyzed this package on Apr 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
convert ^2.1.1 2.1.1
meta ^1.1.8 1.1.8
Transitive dependencies
charcode 1.1.3
typed_data 1.1.6
Dev dependencies
test ^1.9.4