Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.16 KB | None | 0 0
  1. part of emulator;
  2.  
  3. class Packet {
  4.  
  5.   Uint8List data;
  6.  
  7.   Uint8List body;
  8.  
  9.   int header;
  10.  
  11.   int length;
  12.  
  13.   String bodyReadable;
  14.  
  15.   Packet(Uint8List this.data){
  16.  
  17.     ByteData bytes = data.buffer.asByteData(0);
  18.  
  19.     try{
  20.       length = bytes.getUint32(0);
  21.       header = bytes.getUint16(4);
  22.       body = _getContent(data, 6, length);
  23.       bodyReadable = _bodyToString(body.toList()).toString();
  24.     }catch(error){
  25.       throw new Error(error);
  26.     }
  27.   }
  28.  
  29.   StringBuffer _bodyToString(List<int> bodyInCharacters){
  30.     StringBuffer result = new StringBuffer();
  31.  
  32.     bodyInCharacters.forEach((byte) {
  33.       if(byte < 31){
  34.         result.write('[$byte]');
  35.       }else{
  36.         result.write(new String.fromCharCode(byte));
  37.       }
  38.     });
  39.  
  40.     return result;
  41.   }
  42.  
  43.   Uint8List _getContent(Uint8List data, int start, int end){
  44.     List<int> tempList = new List();
  45.  
  46.     int i = 1;
  47.  
  48.     data.forEach((byte) {
  49.       if(i >= start && i <= end){
  50.         tempList.add(byte);
  51.       }
  52.       i++;
  53.     });
  54.  
  55.     return new Uint8List.fromList(tempList);
  56.   }
  57.  
  58.   toString(){
  59.     return '[HEADER: $header][LENGTH: $length][BODY: $bodyReadable]';
  60.   }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement