LCOV - code coverage report
Current view: top level - src/utility - mqtt_client_byte_buffer.dart (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 50 50 100.0 %
Date: 2017-10-09 Functions: 0 0 -

          Line data    Source code
       1             : /*
       2             :  * Package : mqtt_client
       3             :  * Author : S. Hamblett <steve.hamblett@linux.com>
       4             :  * Date   : 31/05/2017
       5             :  * Copyright :  S.Hamblett
       6             :  */
       7             : 
       8             : part of mqtt_client;
       9             : 
      10             : /// Utility class to allow stream like access to a sized byte buffer.
      11             : /// This class is in effect a cut-down implementation of the C# NET
      12             : /// System.IO class with Mqtt client specific extensions.
      13             : class MqttByteBuffer {
      14             :   /// The current position within the buffer.
      15             :   int _position = 0;
      16             : 
      17             :   /// A value representing the length of the stream in bytes.
      18             :   int _length;
      19             : 
      20             :   /// The underlying byte buffer
      21             :   typed.Uint8Buffer _buffer;
      22             : 
      23           5 :   MqttByteBuffer(this._buffer) {
      24          15 :     _length = _buffer.length;
      25             :   }
      26             : 
      27           4 :   MqttByteBuffer.fromList(List<int> data) {
      28           8 :     this._buffer = new typed.Uint8Buffer();
      29           8 :     this._buffer.addAll(data);
      30          12 :     _length = _buffer.length;
      31             :   }
      32             : 
      33           1 :   int get position => _position;
      34             : 
      35           5 :   int get length => _length;
      36             : 
      37           4 :   typed.Uint8Buffer get buffer => _buffer;
      38             : 
      39           1 :   set buffer(typed.Uint8Buffer buff) => _buffer = buff;
      40             : 
      41             :   /// Resets the position to 0
      42             :   void reset() {
      43           1 :     _position = 0;
      44             :   }
      45             : 
      46             :   // Reads a byte from the buffer and advances the position within the buffer by one
      47             :   // byte, or returns -1 if at the end of the buffer.
      48             :   int readByte() {
      49          15 :     final int tmp = _buffer[_position];
      50          20 :     if (_position <= (_length - 1)) {
      51          10 :       _position++;
      52             :     } else {
      53             :       return -1;
      54             :     }
      55             :     return tmp;
      56             :   }
      57             : 
      58             :   /// Read a short int(16 bits)
      59             :   int readShort() {
      60           5 :     final int high = readByte();
      61           5 :     final int low = readByte();
      62          10 :     return (high << 8) + low;
      63             :   }
      64             : 
      65             :   /// Reads a sequence of bytes from the current
      66             :   /// buffer and advances the position within the buffer by the number of bytes read.
      67             :   typed.Uint8Buffer read(int count) {
      68          30 :     if ((_length < count) || (_position + count) > _length) {
      69           1 :       throw new Exception(
      70             :           "mqtt_client::ByteBuffer: The buffer did not have enough bytes for the read operation "
      71           3 :               "length $_length, count $count, position $_position");
      72             :     }
      73           5 :     final typed.Uint8Buffer tmp = new typed.Uint8Buffer();
      74          30 :     tmp.addAll(_buffer.getRange(_position, _position + count));
      75          10 :     _position += count;
      76           5 :     final typed.Uint8Buffer tmp2 = new typed.Uint8Buffer();
      77           5 :     tmp2.addAll(tmp);
      78             :     return tmp2;
      79             :   }
      80             : 
      81             :   /// Writes a byte to the current position in the buffer and advances the position
      82             :   //  within the buffer by one byte.
      83             :   void writeByte(int byte) {
      84          15 :     if (_length == _position) {
      85           8 :       _buffer.add(byte);
      86           8 :       _length++;
      87             :     } else {
      88           6 :       _buffer[_position] = byte;
      89             :     }
      90          10 :     _position++;
      91             :   }
      92             : 
      93             :   /// Write a short(16 bit)
      94             :   void writeShort(int short) {
      95          10 :     writeByte(short >> 8);
      96          10 :     writeByte(short & 0xFF);
      97             :   }
      98             : 
      99             :   /// Writes a sequence of bytes to the current
     100             :   /// buffer and advances the position within the buffer by the number of
     101             :   /// bytes written.
     102             :   void write(typed.Uint8Buffer buffer) {
     103           5 :     if (_buffer == null) {
     104           1 :       _buffer = buffer;
     105             :     } else {
     106          10 :       _buffer.addAll(buffer);
     107             :     }
     108          15 :     _length = _buffer.length;
     109          10 :     _position = _length;
     110             :   }
     111             : 
     112             :   /// Seek. Sets the position in the buffer. If overflow occurs
     113             :   /// the position is set to the end of the buffer.
     114             :   void seek(int seek) {
     115          14 :     if ((seek <= _length) && (seek >= 0)) {
     116           4 :       _position = seek;
     117             :     } else
     118           2 :       _position = _length;
     119             :   }
     120             : 
     121             :   /// Writes an MQTT string member
     122             :   void writeMqttStringM(String stringToWrite) {
     123           4 :     writeMqttString(this, stringToWrite);
     124             :   }
     125             : 
     126             :   /// Writes an MQTT string.
     127             :   /// stringStream - The stream containing the string to write.
     128             :   /// stringToWrite - The string to write.
     129             :   static void writeMqttString(MqttByteBuffer stringStream,
     130             :       String stringToWrite) {
     131           4 :     final MqttEncoding enc = new MqttEncoding();
     132           4 :     final typed.Uint8Buffer stringBytes = enc.getBytes(stringToWrite);
     133           4 :     stringStream.write(stringBytes);
     134             :   }
     135             : 
     136             :   /// Reads an MQTT string from the underlying stream member
     137             :   String readMqttStringM() {
     138           4 :     return MqttByteBuffer.readMqttString(this);
     139             :   }
     140             : 
     141             :   /// Reads an MQTT string from the underlying stream.
     142             :   static String readMqttString(MqttByteBuffer buffer) {
     143             :     // Read and check the length
     144           4 :     final typed.Uint8Buffer lengthBytes = buffer.read(2);
     145           4 :     final MqttEncoding enc = new MqttEncoding();
     146           4 :     final int stringLength = enc.getCharCount(lengthBytes);
     147           4 :     final typed.Uint8Buffer stringBuff = buffer.read(stringLength);
     148           4 :     return enc.getString(stringBuff);
     149             :   }
     150             : }

Generated by: LCOV version 1.10