Skip to content

Commit

Permalink
cleanups: replaced File#delete with Files#delete
Browse files Browse the repository at this point in the history
  • Loading branch information
LostArtist committed Oct 19, 2023
1 parent 6a085cd commit 270633e
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.IOError;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

Expand Down Expand Up @@ -73,7 +72,7 @@ public List<String> listPatches() {
@Override
public void clear() {
try {
Files.delete(repository.toPath());
repository.delete();
repository.createNewFile();
} catch (IOException e) {
throw new IOError(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,8 @@ private File getHdfsFileToTmpFile(String hdfsPath, HdfsConfiguration configurati
}

if (outputDest.exists()) {
try {
Files.delete(outputDest.toPath());
} catch (IOException e) {
boolean result = outputDest.delete();
if (!result) {
LOG.error("Failed to delete output destination {}", outputDest);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,8 @@ public void init(FilterConfig config) throws ServletException {
//go ahead and set it to the default tmp dir on the system.
try {
File file = Files.createTempFile("camel", "").toFile();
try {
Files.delete(file.toPath());
}
catch(IOException e) {
boolean result = file.delete();
if (!result) {
LOG.error("failed to delete {}", file);
}
config.getServletContext().setAttribute("jakarta.servlet.context.tempdir",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -1143,10 +1142,8 @@ protected CamelServlet createServletForConnector(
context.addServlet(holder, "/*");

File file = File.createTempFile("camel", "");
try {
Files.delete(file.toPath());
}
catch(IOException e) {
boolean result = file.delete();
if (!result) {
LOG.error("failed to delete {}", file);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.camel.component.rest.openapi;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
Expand Down Expand Up @@ -80,6 +79,7 @@
import org.slf4j.LoggerFactory;

import static java.util.Optional.ofNullable;

import static org.apache.camel.component.rest.openapi.RestOpenApiHelper.isHostParam;
import static org.apache.camel.component.rest.openapi.RestOpenApiHelper.isMediaRange;
import static org.apache.camel.util.ObjectHelper.isNotEmpty;
Expand Down Expand Up @@ -802,7 +802,7 @@ static String hostFrom(final RestConfiguration restConfiguration) {
* @param camelContext context to use
* @return the specification
*/
static OpenAPI loadSpecificationFrom(final CamelContext camelContext, final URI uri) throws IOException {
static OpenAPI loadSpecificationFrom(final CamelContext camelContext, final URI uri) {
final String uriAsString = uri.toString();
final OpenAPIParser openApiParser = new OpenAPIParser();
final ParseOptions options = new ParseOptions();
Expand Down Expand Up @@ -835,7 +835,7 @@ static OpenAPI loadSpecificationFrom(final CamelContext camelContext, final URI
"The given OpenApi specification could not be loaded from `" + uri + "`.", e);
} finally {
if (tmpFileToDelete != null) {
Files.delete(tmpFileToDelete.toPath());
tmpFileToDelete.delete();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public void onCompletion(Exchange exchange, Exchange inputExchange) {

private void addFileToTar(File source, File file, String fileName) throws IOException, ArchiveException {
File tmpTar = Files.createTempFile(parentDir.toPath(), source.getName(), null).toFile();
Files.delete(tmpTar.toPath());
tmpTar.delete();
if (!source.renameTo(tmpTar)) {
throw new IOException("Could not make temp file (" + source.getName() + ")");
}
Expand Down Expand Up @@ -251,7 +251,7 @@ private void copyExistingEntries(TarArchiveInputStream tin, TarArchiveOutputStre

private void addEntryToTar(File source, String entryName, byte[] buffer, int length) throws IOException, ArchiveException {
File tmpTar = Files.createTempFile(parentDir.toPath(), source.getName(), null).toFile();
Files.delete(tmpTar.toPath());
tmpTar.delete();
if (!source.renameTo(tmpTar)) {
throw new IOException("Cannot create temp file: " + source.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public void onCompletion(Exchange exchange, Exchange inputExchange) {
}

private static void newZipFile(File zipFile) throws URISyntaxException, IOException {
if (zipFile.exists() && !Files.deleteIfExists(zipFile.toPath())) { //Delete, because ZipFileSystem needs to create file on its own (with correct END bytes in the file)
if (zipFile.exists() && !zipFile.delete()) { //Delete, because ZipFileSystem needs to create file on its own (with correct END bytes in the file)
throw new IOException("Cannot delete file " + zipFile);
}
Map<String, Object> env = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ private void writeSettings(String key, String value) {
}
}

private static void removeDir(File d) throws IOException {
private static void removeDir(File d) {
String[] list = d.list();
if (list == null) {
list = new String[0];
Expand All @@ -1309,14 +1309,14 @@ private static void removeDir(File d) throws IOException {
delete(d);
}

private static void delete(File f) throws IOException {
if (!Files.deleteIfExists(f.toPath())) {
private static void delete(File f) {
if (!f.delete()) {
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
// Ignore Exception
}
if (!Files.deleteIfExists(f.toPath())) {
if (!f.delete()) {
f.deleteOnExit();
}
}
Expand Down

0 comments on commit 270633e

Please sign in to comment.