class FileListJsonBuilder {

    public String buildMessageFileListJson(final File directory, final int maxValue) {
        String result = "{\"files\":[]}";
        String[] messageFiles = directory.list();
        if (messageFiles != null) {
            String[] resultFiles = messageFiles;
            Arrays.sort(resultFiles);
            if (messageFiles.length > maxValue) {
                resultFiles = Arrays.copyOfRange(messageFiles, resultFiles.length - maxValue, resultFiles.length);
            }
            result = getJson(resultFiles);
        } else {
            logger.debug("The directory '" + directory + "' has not been created yet, or an I/O error occured.");
        }
        return result;
    }

}