aspose file tools*
The moose likes Java in General and the fly likes java.io.Exception: Insufficient system resources exist to complete the requested serv Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "java.io.Exception: Insufficient system resources exist to complete the requested serv" Watch "java.io.Exception: Insufficient system resources exist to complete the requested serv" New topic
Author

java.io.Exception: Insufficient system resources exist to complete the requested serv

Gayatri Sinha
Ranch Hand

Joined: Feb 13, 2006
Posts: 34
HI,

I am trying to copy a large number of bytes into a new File. When doing this I get the error

java.io.Exception: Insufficient system resources exist to complete the requested service

When the file is small I have no problem for the transfer, but when the file is huge I get this error.


Here is the chunk of code I am using
The destination path is UNC (e.g. "\\mymachine\myshare\destfile.doc")

public void copyToFile(String path, byte[] fileData) {
FileOutputStream fos = null;

try {
fos = new FileOutputStream(path);
fos.write(fileData);
} catch(Exception e) {}
} finally {close(fos);
}


I was trying to search the solution on the internet, and it seems like windows 200 with sp2 has this problem of copying the large files. And the workaround this is to write the file in chunks.

So I could do this

int maxCount = (64 * 1024 * 1024) - (32 * 1024);
long size = fileData.length;
long position = 0;

while (position < size) {
fos.write(fileData, position, maxCount);
position = position + maxCount - 1;
}

The problem with this is for write() I cannot pass long for position, it has to be int, its write(byte[],long,int)

Can anyone please suggest a solution to this problem?

Thank you,
- Gayatri
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
The problem with this is for write() I cannot pass long for position, it has to be int, its write(byte[],long,int)


Since the index in a byte[] MUST BE an int, you can't possibly have more than an int of data in the byte[] in the first place. Just read from the source file in chunks and write to the destination file in chunks.

Bill
Gayatri Sinha
Ranch Hand

Joined: Feb 13, 2006
Posts: 34
Thanks Bill,

This is what I came up with and seems fine atleast now

----------------------------------------------------------
byte[] data = new byte[1024];

BufferedInputStream bin = new BufferedInputStream(new DataInputStream(new ByteArrayInputStream(fileData))); // byte[] fileData

BufferedOutputStream bout = new BufferedOutputStream(new DataOutputStream(new FileOutputStream(path)));

while(bin.read(data) != -1) {
bout.write(data);
}

bout.flush();
bout.close();


Thanks,
- Gayatri
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5

Unless the input file size is an exact multiple of the byte[] size, this code will write extra incorrect data in the last block. You should use the count of bytes read to control the number written.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.io.Exception: Insufficient system resources exist to complete the requested serv