             return nil
         }
 
-        //read in chunks
         if let  string = String(data: data, encoding: encoding),
                 string.characters.count > 0
         {
             data.removeAll()
             return string
         } else {
+            var size = data.count - dataUnitSize
+            while(size > 0) {
+                
+                if let  string = String(bytes: data[0 ..< size], encoding: encoding),
+                    string.characters.count > 0
+                {
+                    data.removeSubrange(0 ..< size)
+                    return string
+                }
+
+                size -= dataUnitSize
+            }
             
-            
-        }
-
-//        var startIndex = 0
-//        
-//        while(startIndex + index < data.count)
-//        {
-//            //try to read 0..<index from the data as String
-//            //if it is not enough, we will increment the chunk size
-//            while(startIndex + index < data.count)
-//            {
-//                let chunk = data[startIndex ..< startIndex + index]
-//                if let  string = String(bytes: chunk, encoding: encoding),
-//                        string.characters.count > 0
-//                {
-//                    //we were able to read the string chunk, 
-//                    //append it to resul
-//                    result.append(string)
-//                    
-//                    //reset index position
-//                    startIndex += index
-//                    index = dataUnitSize
-//                    break
-//                }
-//                index += dataUnitSize
-//            }
-//        }
-//
-//        //remove the chunk from data
-//        data.removeSubrange(0 ..< startIndex)
-//
-//        if(result == "" && data.count > MINIMUM_BUFFER_SIZE) {
-//            //could not find any string in the data
-//            throw Exception.InvalidDataEncoding(requestedEncoding: encoding, description: description)
-//        }
-        
+        }
+
         return ""
     }
     

    let stream:                 InputStream
    let streamDescription:      String
    let bufferSize:             Int

    var data:                   Data
    var buffer:                 [UInt8]

        self.stream = stream
        self.encoding = encoding
        
        self.dataUnitSize = {
            switch encoding {
            case String.Encoding.utf32,
                 String.Encoding.utf32LittleEndian,
                 String.Encoding.utf32BigEndian:
                return 4
            case String.Encoding.utf16,
                 String.Encoding.utf16LittleEndian,
                 String.Encoding.utf16BigEndian:
                return 2
            default:
                return 1
            }
        }()
        
        //make the buffer size a number aligned to dataUnitSize bytes
        self.bufferSize = Int(max(bufferSize, MINIMUM_BUFFER_SIZE) / dataUnitSize) * dataUnitSize
        
        self.streamDescription = description ?? stream.description
        self.firstData = true
