Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Gayatri Sinha
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic