java.io.IOException: Stream closed - cannot perform read operation while file copy

I have code below that is trying to copy a file from attachment.

ReadAttachmentMgr readAttMgr = db.getReadAttachmentMgrByUnid(doc.getUnid());
InputStream is = readAttMgr.readFile();
AttachmentInputStream ais = (AttachmentInputStream)is;
File file = new File("C:\\doc\\" + ais.getFileName());
Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);

The file is copied successfully but I got this error:

java.io.IOException: Stream closed - cannot perform read operation
	at com.hcl.domino.db.model.DominoInputStream.read(DominoInputStream.java:135)
	at java.nio.file.Files.copy(Unknown Source)
	at java.nio.file.Files.copy(Unknown Source)
	at com.xred.DqlDemo.FilesCopyIssue.main(FilesCopyIssue.java:40)

Is there something wrong? How can I fix it?

Any help would be greatly appreciated.

Thanks!

Update:

It is running with appdev pack 1.0.7

Is it running on Domino server or client ?

Seems like the agent has no access to the file, maybe access restrictions on file system

Sorry, I forgot to mention in my question.

It is running with appdev pack 1.0.7

But the main question is: where is the code executed ?

I guess on server, so the file has to be available on the server side at the specified location and the server needs to have access to it

The code is running on Eclipse with pure Java, not in Designer.

And the attachment is in Domino document.

did you check if the domino input stream is null ?

If yes this is the problem

Yes, I have just checked that it is not null.

but it still got error.

ReadAttachmentMgr readAttMgr = db.getReadAttachmentMgrByUnid(doc.getUnid());
is = readAttMgr.readFile();
if(is != null) {
  AttachmentInputStream ais = (AttachmentInputStream)is;
  File file = new File("C:\\doc\\" + ais.getFileName());
  Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
}

And did you also check that you have rights to this directory on server and it exists ?

This directory is on my local and it exists.

When I change InputStream source to local file, it works fine.

is = new FileInputStream(new File("C:\\source\\test.txt"));
if(is != null) {
  File file = new File("C:\\doc\\text.txt");
  Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
}

But use readAttMgr.readFile got error.

ReadAttachmentMgr readAttMgr = db.getReadAttachmentMgrByUnid(doc.getUnid());
is = readAttMgr.readFile();
if(is != null) {
  File file = new File("C:\\doc\\text.txt");
  Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
}

Did you check access to document with file attachment ?

You can check if you can get size of input stream to see if there is data available

This issue will be fixed in next release(1.0.9).

As a workaround we can enclose the call in try-catch block.

got it, Thanks for the help.