code: public static String streamToString(InputStream is, String name, Integer numberOfLines) {
          String result = null;
          try {
              try {
                  if (numberOfLines == null) {
                      // read complete file into string
                      result = new Scanner(is, FILE_ENCODING).useDelimiter("\\A").next();
                  } else {
                      // read only given number of lines
                      Scanner scanner = new Scanner(is, FILE_ENCODING);
                      int resultNrOfLines = 1;
                      result = "";
                      while (scanner.hasNext()) {
                          result = result + scanner.nextLine() + System.lineSeparator();
                          if (++resultNrOfLines > numberOfLines) {
                              break;
                          }
                      }
                      scanner.close();
                      if (result.isEmpty()) {
                          result = null;
                      }
                  }
              } catch (java.util.NoSuchElementException e) {
                  throw new IllegalStateException("Unable to read: " + name + ". Error: " + e.getMessage(), e);
              }
          } finally {
              try {
                  is.close();
              } catch (IOException e) {
                  // what the hell?!
                  throw new RuntimeException("Unable to close: " + name, e);
              }
          }
          return result;
      }
docs: Copies first <numberOfLines> or all lines from UTF-8 input stream's content to a
  string and closes the stream.
  
  @param is            input stream (UTF-8) to read.
  @param name          description for stream in error messages.
  @param numberOfLines number of lines to be read from input stream - if null, all lines are read
  @return content of stream
  @throws RuntimeException if content could not be read.