byte[] reconData = record.getBytes();
// Write out the first chunk
int firstChunk = reconData.length > 20544 ? 20544 : reconData.length;
String firstChunkData = printBase64Binary(Arrays.copyOfRange(reconData, 0, firstChunk));
XMLNode documentNode = getDxl().selectSingleNode("//note");
XMLNode fileDataNode = documentNode.addChildElement("item");
fileDataNode.setAttribute("name", itemName);
fileDataNode = fileDataNode.addChildElement("rawitemdata");
fileDataNode.setAttribute("type", "1");
fileDataNode.setText(firstChunkData);
// Write out any remaining chunks
int remaining = reconData.length - firstChunk;
int chunks = remaining / 20516;
if (remaining % 20516 > 0) {
chunks++;
}
int offset = firstChunk;
for (int i = 0; i < chunks; i++) {
int chunkSize = remaining > 20516 ? 20516 : remaining;
String chunkData = printBase64Binary(Arrays.copyOfRange(reconData, offset, offset + chunkSize));
fileDataNode = documentNode.addChildElement("item");
fileDataNode.setAttribute("name", itemName);
fileDataNode = fileDataNode.addChildElement("rawitemdata");
fileDataNode.setAttribute("type", "1");
fileDataNode.setText(chunkData);
remaining -= 20516;
offset += chunkSize;
}
// Also set the file size if we're setting the main field
if (DEFAULT_FILEDATA_FIELD.equals(itemName)) {
XMLNode fileSizeNode = getDocumentElement().selectSingleNode("//item[@name='$FileSize']");
if (fileSizeNode == null) {
fileSizeNode = getDocumentElement().addChildElement("item");
fileSizeNode.setAttribute("name", "$FileSize");
fileSizeNode.setAttribute("sign", "true");
fileSizeNode = fileSizeNode.addChildElement("number");
} else {
fileSizeNode = fileSizeNode.selectSingleNode("number");
}
fileSizeNode.setText(String.valueOf(fileData.length));
}
} catch (IOException ioe) {
DominoUtils.handleException(ioe);
}
}