My Java method is connecting to a remote server via SFTP and writing a string to a text file:
String messageText = "my very long Text";
ChannelSftp channel = (com.jcraft.jsch.ChannelSftp) session.openChannel("sftp");
channel.connect();
DataOutputStream out = new DataOutputStream(channel.put(filename));
out.writeBytes(messageText);
While writing the messageText to the file, the method crashes most of the time after writing exactly 4355Bytes. i.e. the text file is written until a certain amount of characters/bytes is reached and then just stops, however this does not happen every time the method is executed. No exception is thrown, but the spring workflow that contains this method is crashing at this point.
The Java class with the method is packed in a jar and runs inside of OSGI on a Windows Server. The remote server is Unix-based.
I appreciate any suggestions.
Update:
As proposed by mhan, I used a buffer and it seems to have solved the problem:
BufferedOutputStream out = new BufferedOutputStream(channel.put(filename + "_"));
out.write(messageText.getBytes());
As the comments suggest, put the code inside a try-catch block and catch Exception. Further suggestions that I could give;
1) Would you like to introduce a buffer and see if that can improve the performance.
2) Can you check if the remote file is available, before you write to it?