I get a file then convert it to string and replace some words.
With this string I run the following:
com.hierynomus.smbj.share.File remoteSmbjFile = share.openFile(fileName, EnumSet.of(AccessMask.GENERIC_WRITE), null, s, null, null);
byte[] myFile= template.getBytes("UTF-8");
ByteArrayInputStream bs = new ByteArrayInputStream(myFile);
try (OutputStream outputStream = remoteSmbjFile.getOutputStream();) {
byte[] buffer = new byte[1024];
int length;
while ((length = bs.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
}
The file gets created at a network location and is fine (no problems). The file itself is an .xml type ranging in 2-10 MB in size.
I'm using the xml file type to open this file in word if there is one character off the file will be corrupted.
I'm just worried due to the files size this method will cause a corruption.
Is the above method safe practice are there any other approaches I could take?
Is it safe practice to convert a large string to bytes and send across the network?
It depends. (Nothing is totally safe. Ever.)
I'm just worried due to the files size this method will cause a corruption.
Large file sizes won't cause corruption.
However there are two scenarios where large file sizes could be problematic:
If the receiver of the file is unable or unwilling to accept a file beyond a certain size, then it might truncate the file. If this is not reported back to the sender, you have a potential source of corruption.
Network packets typically use a simple CRC-based error detection mechanism to detect packets that have been corrupted in transit by network errors. It is possible (with a very low probability1) for a multi-bit corruption to go undetected. If you implement file transfer over the top of a TCP stream (for example), there is a very small probability of file corruption.
1 - Actual error rates are difficult to predict, but if you are transferring terabytes of data, the probability of corruptions due to undetected network errors becomes significant.
Is the above method safe practice are there any other approaches I could take?
Given that you cannot prevent either of the above scenarios, you need a way to be absolutely sure that a large file transfer has not corrupted a file. Typically this is done by:
Note that the above are independent of the file type.
I understand that there are standard ways to put a strong digital signature on an XML file's content. Checking such a signature would also serve to detect accidental corruption.