我想将存储在一个文档库中的一个对象的内容复制到存储在另一个文档库中的另一个对象。我不想创建文件,因为我有超过 300k 的文件要复制。以下是我的部分代码:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtils.copy(source.getContent(), baos);
[...]
targetObj.setContent(baos); // Documentum DFC
targetObj.save(); // Documentum DFC
如果我不调整 JVM,IOUtils.copy(source.getContent(), baos);
给出java.lang.OutOfMemoryError: Java heap空格
.
如果我通过设置 Xmx 最大值来调整 JVM,之前的指令没问题,但是 java.lang.OutOfMemoryError: Java heap space
发生在 targetObj.setContent(baos);
.
只有 8332175 字节的大内容... (7.94 MB)
知道哪里出了问题吗?从 ByteArrayInputStream 复制到 ByteArrayOutputStream 的更好方法?还有别的吗?
一些 Documentum API
getContent
public ByteArrayInputStream getContent() throws DfException
Copies this object's content from the Documentum server into a ByteArrayInputStream >object.
The following code example demonstrates how to copy an objects content from the >Documentum server into memory:
IDfSysObject sysObj = (IDfSysObject)session.getObject(new DfId("0900d5bb8001f900")); ByteArrayInputStream bais = sysObj.getContent(); if (bais.available() > 0) { // Data successfully fetched from the server... }
Returns: a ByteArrayInputStream object containing the objects content. Throws: DfException - if a server error occurs.
和
setContent
public boolean setContent(ByteArrayOutputStream content) throws DfException
Sets new content to an object. Use this method when you want to set data that resides >in working memory.
The following code example demonstrates how to set content residing in memory to a new document:
IDfSysObject sysObj = (IDfSysObject)sess.newObject("dm_document"); sysObj.setObjectName("testDoc"); sysObj.setContentType("crtext"); byte b[] = {35,36,37,38,39}; ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(b, 0, 5); sysObj.setContent(out); sysObj.save();
Parameters: content - the content as a ByteArrayOutputStream. Throws: DfException - if a server error occurs.
最佳答案
只要您使用 ByteArrayOutputStream
,数据就必须适合内存。
我对 Documentum 一无所知,但是否有 targetObj.setContent(File)
或 setContent(InputStream)
,这样您就可以避免将整个 block 读入一个 byte[]
?
(虽然 8MB 并不是那么大,也许您可以调整 Java 堆空间。它也可以帮助预先调整 BAOS 使用的缓冲区大小,您可以将初始大小传递给它的构造函数)
更新:您确定 setContent 接受 ByteArray Output 流吗?通常,setter 将从 InputStream 中读取。
关于java - 使用 apache IOUtils.copy() 和 setContent() 复制文档内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1706853/