import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
private static final Logger LOGGER = LoggerFactory.getLogger(CertFileWriter.class);
private final String destPath;
- public CertFileWriter(String destPath) {
+ private CertFileWriter(String destPath) {
this.destPath = destPath;
}
+ public static CertFileWriter createWithDir(String destPath) {
+ createDirIfNotExists(destPath);
+ return new CertFileWriter(destPath);
+ }
+
public void saveData(byte[] data, String filename) throws CertFileWriterException {
LOGGER.debug("Attempt to save file {} in path {}", filename, destPath);
try (FileOutputStream outputStream = new FileOutputStream(Path.of(destPath, filename).toString())) {
throw new CertFileWriterException(e);
}
}
+
+ private static void createDirIfNotExists(String destPath) {
+ File destFolderPath = new File(destPath);
+ if (!destFolderPath.exists()) {
+ LOGGER.debug("Destination path not exists, subdirectories are created");
+ destFolderPath.mkdirs();
+ }
+ }
}