Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- part of emulator;
- class Packet {
- Uint8List data;
- Uint8List body;
- int header;
- int length;
- String bodyReadable;
- Packet(Uint8List this.data){
- ByteData bytes = data.buffer.asByteData(0);
- try{
- length = bytes.getUint32(0);
- header = bytes.getUint16(4);
- body = _getContent(data, 6, length);
- bodyReadable = _bodyToString(body.toList()).toString();
- }catch(error){
- throw new Error(error);
- }
- }
- StringBuffer _bodyToString(List<int> bodyInCharacters){
- StringBuffer result = new StringBuffer();
- bodyInCharacters.forEach((byte) {
- if(byte < 31){
- result.write('[$byte]');
- }else{
- result.write(new String.fromCharCode(byte));
- }
- });
- return result;
- }
- Uint8List _getContent(Uint8List data, int start, int end){
- List<int> tempList = new List();
- int i = 1;
- data.forEach((byte) {
- if(i >= start && i <= end){
- tempList.add(byte);
- }
- i++;
- });
- return new Uint8List.fromList(tempList);
- }
- toString(){
- return '[HEADER: $header][LENGTH: $length][BODY: $bodyReadable]';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement