public static byte[] readFiletoByteArray(final File file) { Validate.notNull(file); try { return FileUtils.readFileToByteArray(file); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_READING_FILE, e) .addContextValue(FileErrorContext.FILE, file); } } }
/** * Check is a {@link Path file} is empty. * * @param filePath the path to a file. * @return {@code true} if the file is empty; {@code false} otherwise. */ public static boolean isFileEmpty(final Path filePath) { try { return Files.size(filePath) == 0; } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_READING_FILE, "Cannot read file", e) .addContextValue(FileErrorContext.FILE, filePath); } }
/** * Open an {@link InputStream} on a file {@link File}. * * @param file the file. * @return the input stream. */ public static InputStream openInputStream(final File file) { try { return new ByteArrayInputStream(FileUtils.readFileToByteArray(file)); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_OPENING_STREAM, e) .addContextValue(FileErrorContext.FILE, file); } }
/** * Read the content of a {@link File} as a collection of {@link String} lines. * * @param file the file. * @return the collection if {@link String} lines. */ public static Collection<String> readLines(final File file) { Validate.notNull(file); try { return FileUtils.readLines(file, StandardCharsets.UTF_8); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_READING_FILE, e) .addContextValue(FileErrorContext.FILE, file); } }
/** * Check if the content of a folder is empty (i.e. doesn't contain any folder nor file) * * @param folderPath the folder * @return true if the folder is empty, false otherwise */ public static boolean isFolderEmpty(final Path folderPath) { Validate.notNull(folderPath); try { return Files.list(folderPath).count() <= 0; } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_READING_FILE, e) .addContextValue(FileErrorContext.FOLDER, folderPath); } }
/** * Check if a file is empty. * * @param file the file * @return {@code true} if the file is empty; {@code false} otherwise. */ public static boolean isFileEmpty(final File file) { try { final List<String> lines = FileUtils.readLines(file, StandardCharsets.UTF_8); return lines.isEmpty(); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_READING_FILE, "Cannot read file", e) .addContextValue(FileErrorContext.FILE, file); } }
/** * Read the content of a {@link Path file} as a collection {@link String lines}. * * @param filePath the path to a file. * @return the collection of lines. */ public static Collection<String> readLines(final Path filePath) { Validate.notNull(filePath); try { return Files.readAllLines(filePath, StandardCharsets.UTF_8); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_READING_FILE, e) .addContextValue(FileErrorContext.FILE, filePath); } } }
/** * Clean a folder from its content (file and sub-folders). * * @param folder the folder. */ public static void cleanDirectory(final File folder) { if (!folder.exists()) { logger.info("Folder [{}] does not exist. No need to clean.", folder); return; } try { FileUtils.cleanDirectory(folder); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_CLEANING_FOLDER, e) .addContextValue(FileErrorContext.FOLDER, folder); } }
/** * Return or create a folder. * * @param folder the folder * @return the already existing / created folder */ public static File provideFolder(final File folder) { Validate.notNull(folder); if (!folder.exists()) { try { FileUtils.forceMkdir(folder); logger.info("Folder created [{}]", folder); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_CREATING_FOLDER, e) .addContextValue(FileErrorContext.FOLDER, folder); } } return folder; }
/** * Write down a byte array in an output file. * * @param bytes the file in byte array * @param outputFilePath the output file. * @return the written file */ public static Path writeFile(final byte[] bytes, final Path outputFilePath) { Validate.notNull(bytes); Validate.notNull(outputFilePath); try { FileSystemUtils.retrieveOrCreateFile(outputFilePath); Files.write(outputFilePath, bytes, StandardOpenOption.CREATE, StandardOpenOption.WRITE); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_WRITING_FILE, "cannot write file", e) .addContextValue(FileErrorContext.FILE, outputFilePath); } return outputFilePath; }
/** * Copy a source file to a target file. * * @param file the source file. * @param outputFile the target file. * @return the target file. */ public static File copyFile(final File file, final File outputFile) { try { FileUtils.copyFile(file, outputFile); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_COPY_FILE, e) .addContextValue(FileErrorContext.FILE, file) .addContextValue(FileErrorContext.FILE, outputFile); } return outputFile; }
/** * Copy a source file to a target file. * * @param filePath the path to the source file. * @param outputFilePath the path to the target file. * @return the path to the target file. */ public static Path copyFile(final Path filePath, final Path outputFilePath) { final Path ret; try { ret = Files.copy(filePath, outputFilePath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_COPY_FILE, e) .addContextValue(FileErrorContext.FILE, filePath) .addContextValue(FileErrorContext.FILE, outputFilePath); } return ret; }
/** * Create a new {@link XPathFactory}. * * @return the new XPath factory. */ public static XPathFactory newXpathFactory() { try { return XPathFactory.newInstance( NamespaceConstant.OBJECT_MODEL_SAXON, SAXON_XPATH_FACTORY, XmlPathCalculator.class.getClass().getClassLoader()); } catch (XPathFactoryConfigurationException e) { throw new SystemException(XPathErrorType.ERROR_XPATH_EVALUATION, e.getMessage()) .addContextValue(CommonErrorContext.CAUSE, e.getMessage()); } } }
@Override public PutObjectResult uploadObject(final String key, final String content) { Validate.notNull(key); Validate.notNull(content); final PutObjectResult ret; try { ret = amazonS3.putObject(awsS3Bucket.getName(), key, content); } catch (AmazonServiceException e) { throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e) .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.WRITE); } catch (SdkClientException e) { throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e); } logger.info("Uploaded content to AWS S3 bucket [{}] : [{}]", awsS3Bucket.getName(), ret); return ret; }
@Override public PutObjectResult uploadObject(final String key, final File file) { Validate.notNull(key); Validate.notNull(file); final PutObjectResult ret; try { ret = amazonS3.putObject(awsS3Bucket.getName(), key, file); } catch (AmazonServiceException e) { throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e) .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.WRITE); } catch (SdkClientException e) { throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e); } logger.info("Uploaded file [{}] to AWS S3 bucket [{}] : [{}]", file, awsS3Bucket.getName(), ret); return ret; }
@Override public PutObjectResult uploadObject(final String key, final InputStream inputStream, final ObjectMetadata objectMetadata) { Validate.notNull(key); Validate.notNull(inputStream); final PutObjectResult ret; try { ret = amazonS3.putObject(awsS3Bucket.getName(), key, inputStream, objectMetadata); } catch (AmazonServiceException e) { throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e) .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.WRITE); } catch (SdkClientException e) { throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e); } logger.info("Uploaded stream to AWS S3 bucket [{}] : [{}]", awsS3Bucket.getName(), ret); return ret; }
@Override public void deleteObject(final String key) { Validate.notNull(key); try { amazonS3.deleteObject(awsS3Bucket.getName(), key); logger.info("Deleted object [{}] from AWS S3 bucket [{}]", key, awsS3Bucket.getName()); } catch (AmazonServiceException e) { throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e) .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.DELETE); } catch (SdkClientException e) { throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e); } }
@Override public Collection<S3ObjectSummary> listObjects() { try { ListObjectsV2Result result = amazonS3.listObjectsV2(awsS3Bucket.getName()); final List<S3ObjectSummary> ret = result.getObjectSummaries(); logger.info("List [{}] objects from AWS S3 bucket [{}]", ret.size(), awsS3Bucket.getName()); return ret; } catch (AmazonServiceException e) { throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e) .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.LIST); } catch (SdkClientException e) { throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e); } }
@Override public Collection<S3ObjectSummary> listObjects(final String prefix) { Validate.notNull(prefix); try { ListObjectsV2Result result = amazonS3.listObjectsV2(awsS3Bucket.getName(), prefix); final List<S3ObjectSummary> ret = result.getObjectSummaries(); logger.info("List [{}] objects with prefix [{}] from AWS S3 bucket [{}]", ret.size(), prefix, awsS3Bucket.getName()); return ret; } catch (AmazonServiceException e) { throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e) .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.LIST) .addContextValue(AwsS3ErrorContext.PREFIX, prefix); } catch (SdkClientException e) { throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e); } }
@Override public File downloadObject(final String key, final File outputFile) { Validate.notNull(key); try { final S3Object o = amazonS3.getObject(awsS3Bucket.getName(), key); try (S3ObjectInputStream s3is = o.getObjectContent()) { FileIOUtils.writeFile(s3is, outputFile); } } catch (AmazonServiceException | IOException e) { throw new SystemException(AwsS3ErrorType.CANNOT_READ_OBJECT, e) .addContextValue(AwsS3ErrorContext.KEY, key) .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.READ); } catch (SdkClientException e) { throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e); } logger.info("Downloaded object [{}] from AWS S3 bucket [{}} to [{}]", key, awsS3Bucket.getName(), outputFile); return outputFile; }