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:
parent
085ce861d8
commit
1442f1609c
|
@ -115,15 +115,28 @@ public class FTPClient {
|
|||
|
||||
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);
|
||||
|
||||
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();
|
||||
|
||||
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() {
|
||||
|
@ -208,7 +221,6 @@ public class FTPClient {
|
|||
String resolvedPath = resolveRemotePath(path);
|
||||
logger.debug("Uploading file to remote path: " + resolvedPath);
|
||||
|
||||
|
||||
attemptUpload(source, resolvedPath);
|
||||
|
||||
FTPReplyCodeAndString reply = getFTPReplyCodeAndString();
|
||||
|
@ -239,8 +251,6 @@ public class FTPClient {
|
|||
private void attemptUpload(String source, String resolvedPath) throws IOException {
|
||||
final long startTime = System.nanoTime();
|
||||
FileInputStream localStream = new FileInputStream(source);
|
||||
clientImpl.setFileType(FTP.BINARY_FILE_TYPE);
|
||||
clientImpl.enterLocalPassiveMode();
|
||||
clientImpl.storeFile(resolvedPath, localStream);
|
||||
localStream.close();
|
||||
this.storeFileTimeNanos += System.nanoTime() - startTime;
|
||||
|
|
|
@ -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_UPLOAD = "dummyFileToUpload";
|
||||
|
||||
public static final String DUMMY_FILE_TO_UPLOAD_2 = "dummyFileToUpload2";
|
||||
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_2 = "Dummy file content 2\nMore content\n";
|
||||
public static final String LOCALHOST = "localhost";
|
||||
|
||||
|
||||
|
@ -48,26 +51,31 @@ public class FTPClientTest implements ResourceLoaderTests {
|
|||
|
||||
|
||||
Path dummyFileToUploadPath;
|
||||
|
||||
Path dummyFileToUploadPath2;
|
||||
Path dummyUploadedFilePath;
|
||||
Path dummyUploadedFilePath2;
|
||||
|
||||
String dummyFileContent;
|
||||
|
||||
String dummyFileContent2;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() throws IOException {
|
||||
setupDummyFileToUpload();
|
||||
setupDummyFilesToUpload();
|
||||
setupFakeFtpDirectory();
|
||||
setupFakeFtpServer();
|
||||
}
|
||||
|
||||
private void setupDummyFileToUpload() throws IOException {
|
||||
dummyFileContent = createDummyFileContent();
|
||||
private void setupDummyFilesToUpload() throws IOException {
|
||||
dummyFileContent = DUMMY_FILE_CONTENT;
|
||||
dummyFileContent2 = DUMMY_FILE_CONTENT;
|
||||
|
||||
dummyFileToUploadPath = Files.createTempFile("dummyFtpFileToUpload", "dummy");
|
||||
Files.write(dummyFileToUploadPath, DUMMY_FILE_CONTENT.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private String createDummyFileContent() {
|
||||
return DUMMY_FILE_CONTENT;
|
||||
dummyFileToUploadPath2 = Files.createTempFile("dummyFtpFileToUpload2", "dummy");
|
||||
Files.write(dummyFileToUploadPath2, DUMMY_FILE_CONTENT_2.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
public void setupFakeFtpServer() throws IOException {
|
||||
|
@ -101,6 +109,7 @@ public class FTPClientTest implements ResourceLoaderTests {
|
|||
|
||||
dummyFileToDeletePath = Files.createFile(relativePath2.resolve(DUMMY_FILE_TO_DELETE));
|
||||
dummyUploadedFilePath = relativePath2.resolve(DUMMY_FILE_TO_UPLOAD);
|
||||
dummyUploadedFilePath2 = relativePath2.resolve(DUMMY_FILE_TO_UPLOAD_2);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
|
@ -141,6 +150,7 @@ public class FTPClientTest implements ResourceLoaderTests {
|
|||
|
||||
private FTPClient connectToFTPClient() throws IOException {
|
||||
FTPClient client = new FTPClient(LOCALHOST, FAKE_FTP_PORT, RELATIVE_PATH_1, DUMMY_USER, DUMMY_PASSWORD);
|
||||
client.setBufferSize(16000);
|
||||
client.connect();
|
||||
|
||||
assertAllMillisFieldsAreZero(client);
|
||||
|
@ -175,21 +185,43 @@ public class FTPClientTest implements ResourceLoaderTests {
|
|||
|
||||
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.getStoreFileTimeNanos() > 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));
|
||||
FileEntry fileEntry = (FileEntry)fakeFtpServer.getFileSystem().getEntry(uploadedFilePath);
|
||||
assertNotNull(fileEntry);
|
||||
InputStream inputStream = fileEntry.createInputStream();
|
||||
byte[] bytes = IOUtils.toByteArray(inputStream);
|
||||
String actualContent = new String(bytes, StandardCharsets.UTF_8);
|
||||
assertEquals(DUMMY_FILE_CONTENT,actualContent);
|
||||
assertEquals(expectedFileContent,actualContent);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -237,7 +269,7 @@ public class FTPClientTest implements ResourceLoaderTests {
|
|||
assertTrue(fakeFtpServer.getFileSystem().exists(newPath1.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.getStoreFileTimeNanos() > 0);
|
||||
|
|
Loading…
Reference in New Issue