class StratumConnection {

  void handleBuffer(final Buffer buffer) {
    LOG.trace("Buffer received {}", buffer);
    Splitter splitter = Splitter.on('\n');
    boolean firstMessage = false;
    String messagesString;
    try {
      messagesString = buffer.toString(StandardCharsets.UTF_8);
    } catch (IllegalArgumentException e) {
      LOG.debug("Invalid message with non UTF-8 characters: " + e.getMessage(), e);
      closeHandle.run();
      return;
    }
    Iterator<String> messages = splitter.split(messagesString).iterator();
    while (messages.hasNext()) {
      String message = messages.next();
      if (!firstMessage) {
        message = incompleteMessage + message;
        firstMessage = true;
      }
      if (!messages.hasNext()) {
        incompleteMessage = message;
      } else {
        LOG.trace("Dispatching message {}", message);
        handleMessage(message);
      }
    }
  }

    StratumConnection(
      final StratumProtocol[] protocols,
      final Runnable closeHandle,
      final Consumer<String> sender);

}