Stop re-initializing file type and mode with each upload (#1178)

* Stop re-initializing file type and mode with each upload

* Methods for setting up buffer size
This commit is contained in:
dotasek 2023-03-21 15:36:19 -04:00 committed by GitHub
parent 085ce861d8
commit 1442f1609c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 15 deletions

View File

@ -115,15 +115,28 @@ public class FTPClient {
resetTimers(); resetTimers();
logger.debug("Initial Working directory: " + clientImpl.printWorkingDirectory()); clientImpl.setFileType(FTP.BINARY_FILE_TYPE);
clientImpl.enterLocalPassiveMode();
logger.debug("Setting initial working directory: " + clientImpl.printWorkingDirectory());
clientImpl.changeWorkingDirectory(path); clientImpl.changeWorkingDirectory(path);
throwExceptionForNegativeCompletion("FTP server could not establish default working directory", true); throwExceptionForNegativeCompletion("FTP server could not establish default working directory", true);
logger.debug("Set initial working directory.");
logger.debug("Resolving remote resolved path.");
resolvedPath = clientImpl.printWorkingDirectory(); resolvedPath = clientImpl.printWorkingDirectory();
logger.debug("Resolved working directory: " + resolvedPath); logger.debug("Resolved remote resolved path: " + resolvedPath);
}
public void setBufferSize(int bufferSize) {
clientImpl.setBufferSize(bufferSize);
}
public int getBufferSize() {
return clientImpl.getBufferSize();
} }
private void resetTimers() { private void resetTimers() {
@ -208,7 +221,6 @@ public class FTPClient {
String resolvedPath = resolveRemotePath(path); String resolvedPath = resolveRemotePath(path);
logger.debug("Uploading file to remote path: " + resolvedPath); logger.debug("Uploading file to remote path: " + resolvedPath);
attemptUpload(source, resolvedPath); attemptUpload(source, resolvedPath);
FTPReplyCodeAndString reply = getFTPReplyCodeAndString(); FTPReplyCodeAndString reply = getFTPReplyCodeAndString();
@ -239,8 +251,6 @@ public class FTPClient {
private void attemptUpload(String source, String resolvedPath) throws IOException { private void attemptUpload(String source, String resolvedPath) throws IOException {
final long startTime = System.nanoTime(); final long startTime = System.nanoTime();
FileInputStream localStream = new FileInputStream(source); FileInputStream localStream = new FileInputStream(source);
clientImpl.setFileType(FTP.BINARY_FILE_TYPE);
clientImpl.enterLocalPassiveMode();
clientImpl.storeFile(resolvedPath, localStream); clientImpl.storeFile(resolvedPath, localStream);
localStream.close(); localStream.close();
this.storeFileTimeNanos += System.nanoTime() - startTime; this.storeFileTimeNanos += System.nanoTime() - startTime;

View File

@ -30,8 +30,11 @@ public class FTPClientTest implements ResourceLoaderTests {
public static final String DUMMY_FILE_TO_DELETE = "dummyFileToDelete"; public static final String DUMMY_FILE_TO_DELETE = "dummyFileToDelete";
public static final String DUMMY_FILE_TO_UPLOAD = "dummyFileToUpload"; public static final String DUMMY_FILE_TO_UPLOAD = "dummyFileToUpload";
public static final String DUMMY_FILE_TO_UPLOAD_2 = "dummyFileToUpload2";
public static final int FAKE_FTP_PORT = 8022; public static final int FAKE_FTP_PORT = 8022;
public static final String DUMMY_FILE_CONTENT = "Dummy file content\nMore content\n"; public static final String DUMMY_FILE_CONTENT = "Dummy file content\nMore content\n";
public static final String DUMMY_FILE_CONTENT_2 = "Dummy file content 2\nMore content\n";
public static final String LOCALHOST = "localhost"; public static final String LOCALHOST = "localhost";
@ -48,26 +51,31 @@ public class FTPClientTest implements ResourceLoaderTests {
Path dummyFileToUploadPath; Path dummyFileToUploadPath;
Path dummyFileToUploadPath2;
Path dummyUploadedFilePath; Path dummyUploadedFilePath;
Path dummyUploadedFilePath2;
String dummyFileContent; String dummyFileContent;
String dummyFileContent2;
@BeforeEach @BeforeEach
public void setup() throws IOException { public void setup() throws IOException {
setupDummyFileToUpload(); setupDummyFilesToUpload();
setupFakeFtpDirectory(); setupFakeFtpDirectory();
setupFakeFtpServer(); setupFakeFtpServer();
} }
private void setupDummyFileToUpload() throws IOException { private void setupDummyFilesToUpload() throws IOException {
dummyFileContent = createDummyFileContent(); dummyFileContent = DUMMY_FILE_CONTENT;
dummyFileContent2 = DUMMY_FILE_CONTENT;
dummyFileToUploadPath = Files.createTempFile("dummyFtpFileToUpload", "dummy"); dummyFileToUploadPath = Files.createTempFile("dummyFtpFileToUpload", "dummy");
Files.write(dummyFileToUploadPath, DUMMY_FILE_CONTENT.getBytes(StandardCharsets.UTF_8)); Files.write(dummyFileToUploadPath, DUMMY_FILE_CONTENT.getBytes(StandardCharsets.UTF_8));
}
private String createDummyFileContent() { dummyFileToUploadPath2 = Files.createTempFile("dummyFtpFileToUpload2", "dummy");
return DUMMY_FILE_CONTENT; Files.write(dummyFileToUploadPath2, DUMMY_FILE_CONTENT_2.getBytes(StandardCharsets.UTF_8));
} }
public void setupFakeFtpServer() throws IOException { public void setupFakeFtpServer() throws IOException {
@ -101,6 +109,7 @@ public class FTPClientTest implements ResourceLoaderTests {
dummyFileToDeletePath = Files.createFile(relativePath2.resolve(DUMMY_FILE_TO_DELETE)); dummyFileToDeletePath = Files.createFile(relativePath2.resolve(DUMMY_FILE_TO_DELETE));
dummyUploadedFilePath = relativePath2.resolve(DUMMY_FILE_TO_UPLOAD); dummyUploadedFilePath = relativePath2.resolve(DUMMY_FILE_TO_UPLOAD);
dummyUploadedFilePath2 = relativePath2.resolve(DUMMY_FILE_TO_UPLOAD_2);
} }
@AfterEach @AfterEach
@ -141,6 +150,7 @@ public class FTPClientTest implements ResourceLoaderTests {
private FTPClient connectToFTPClient() throws IOException { private FTPClient connectToFTPClient() throws IOException {
FTPClient client = new FTPClient(LOCALHOST, FAKE_FTP_PORT, RELATIVE_PATH_1, DUMMY_USER, DUMMY_PASSWORD); FTPClient client = new FTPClient(LOCALHOST, FAKE_FTP_PORT, RELATIVE_PATH_1, DUMMY_USER, DUMMY_PASSWORD);
client.setBufferSize(16000);
client.connect(); client.connect();
assertAllMillisFieldsAreZero(client); assertAllMillisFieldsAreZero(client);
@ -175,21 +185,43 @@ public class FTPClientTest implements ResourceLoaderTests {
client.upload(dummyFileToUploadPath.toFile().getAbsolutePath(), RELATIVE_PATH_2 + "/" + DUMMY_FILE_TO_UPLOAD); client.upload(dummyFileToUploadPath.toFile().getAbsolutePath(), RELATIVE_PATH_2 + "/" + DUMMY_FILE_TO_UPLOAD);
assertUploadedFileCorrect(uploadFilePath); assertUploadedFileCorrect(uploadFilePath, DUMMY_FILE_CONTENT);
assertTrue(client.getDeleteFileTimeNanos() == 0); assertTrue(client.getDeleteFileTimeNanos() == 0);
assertTrue(client.getStoreFileTimeNanos() > 0); assertTrue(client.getStoreFileTimeNanos() > 0);
assertTrue(client.getCreateRemotePathIfNotExistsNanos() == 0); assertTrue(client.getCreateRemotePathIfNotExistsNanos() == 0);
} }
private void assertUploadedFileCorrect(String uploadedFilePath) throws IOException { @Test
public void testMultiUpload() throws IOException {
FTPClient client = connectToFTPClient();
String uploadFilePath = dummyUploadedFilePath.toFile().getAbsolutePath();
assertFalse(fakeFtpServer.getFileSystem().exists(uploadFilePath));
String uploadFilePath2 = dummyUploadedFilePath2.toFile().getAbsolutePath();
assertFalse(fakeFtpServer.getFileSystem().exists(uploadFilePath));
client.upload(dummyFileToUploadPath.toFile().getAbsolutePath(), RELATIVE_PATH_2 + "/" + DUMMY_FILE_TO_UPLOAD);
client.upload(dummyFileToUploadPath2.toFile().getAbsolutePath(), RELATIVE_PATH_2 + "/" + DUMMY_FILE_TO_UPLOAD_2);
assertUploadedFileCorrect(uploadFilePath, DUMMY_FILE_CONTENT);
assertUploadedFileCorrect(uploadFilePath2, DUMMY_FILE_CONTENT_2);
assertTrue(client.getDeleteFileTimeNanos() == 0);
assertTrue(client.getStoreFileTimeNanos() > 0);
assertTrue(client.getCreateRemotePathIfNotExistsNanos() == 0);
}
private void assertUploadedFileCorrect(String uploadedFilePath, String expectedFileContent) throws IOException {
assertTrue(fakeFtpServer.getFileSystem().exists(uploadedFilePath)); assertTrue(fakeFtpServer.getFileSystem().exists(uploadedFilePath));
FileEntry fileEntry = (FileEntry)fakeFtpServer.getFileSystem().getEntry(uploadedFilePath); FileEntry fileEntry = (FileEntry)fakeFtpServer.getFileSystem().getEntry(uploadedFilePath);
assertNotNull(fileEntry); assertNotNull(fileEntry);
InputStream inputStream = fileEntry.createInputStream(); InputStream inputStream = fileEntry.createInputStream();
byte[] bytes = IOUtils.toByteArray(inputStream); byte[] bytes = IOUtils.toByteArray(inputStream);
String actualContent = new String(bytes, StandardCharsets.UTF_8); String actualContent = new String(bytes, StandardCharsets.UTF_8);
assertEquals(DUMMY_FILE_CONTENT,actualContent); assertEquals(expectedFileContent,actualContent);
} }
@Test @Test
@ -237,7 +269,7 @@ public class FTPClientTest implements ResourceLoaderTests {
assertTrue(fakeFtpServer.getFileSystem().exists(newPath1.toFile().getAbsolutePath())); assertTrue(fakeFtpServer.getFileSystem().exists(newPath1.toFile().getAbsolutePath()));
assertTrue(fakeFtpServer.getFileSystem().exists(newPath2.toFile().getAbsolutePath())); assertTrue(fakeFtpServer.getFileSystem().exists(newPath2.toFile().getAbsolutePath()));
assertUploadedFileCorrect(uploadFilePath.toFile().getAbsolutePath()); assertUploadedFileCorrect(uploadFilePath.toFile().getAbsolutePath(), DUMMY_FILE_CONTENT);
assertTrue(client.getDeleteFileTimeNanos() == 0); assertTrue(client.getDeleteFileTimeNanos() == 0);
assertTrue(client.getStoreFileTimeNanos() > 0); assertTrue(client.getStoreFileTimeNanos() > 0);