OMFactory factory,
boolean isOptimize,
String attachmentDir) throws IOException, MessagingException {
OMText omText = null;
if (isOptimize) {
LifecycleManager lm = getLifecycleManager(factory);
int threshold = getThreshold(factory);
// TODO Consider lowering the threshold in low memory situations ?
//threshold = lm.getRuntimeThreshold(threshold);
if (length >= threshold && attachmentDir != null) {
// Get the file accessor
FileAccessor fileAccessor = lm.create(attachmentDir);
OutputStream fos = fileAccessor.getOutputStream();
//Copy the bytes into the file
ByteArrayInputStream is = new ByteArrayInputStream(b, off, length);
BufferUtils.inputStream2OutputStream(is, fos);
fos.close();
// Delete this temp file on exit
lm.deleteOnExit(fileAccessor.getFile());
lm.deleteOnTimeInterval(DELETE_TIME, fileAccessor.getFile());
// Create the OMText node from the datahandler
DataHandler dh = fileAccessor.getDataHandler(null);
omText = factory.createOMText(dh, isOptimize);
}