• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Java NIO error (anyone?)  RSS feed

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the following error whenever I try to use NIO to copy certain files. Has anyone seen this..
java.io.IOException: Bad address
at sun.nio.ch.FileDispatcher.pwrite0(Native Method)
at sun.nio.ch.FileDispatcher.pwrite(FileDispatcher.java:46)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:96)
at sun.nio.ch.IOUtil.write(IOUtil.java:60)
at sun.nio.ch.FileChannelImpl.write(FileChannelImpl.java:397)
at sun.nio.ch.FileChannelImpl.transfer(FileChannelImpl.java:330)
at sun.nio.ch.FileChannelImpl.transferFrom(FileChannelImpl.java:316)
Code
--------

try {
// Create channel on the source
FileChannel srcChannel = new FileInputStream("srcFilename").getChannel();

// Create channel on the destination
FileChannel dstChannel = new FileOutputStream("dstFilename").getChannel();

// Copy file contents from source to destination
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

// Close the channels
srcChannel.close();
dstChannel.close();
} catch (IOException e) {
}
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting the exact same problem. Has anyone else found a solution since the previous post?
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just ran this under 1.4.2_04 with no problems for a text file.

Alok - What did you mean "certain files"? What resolution did you come to?

Andrew - please post your code, someone here can help.

Regards, Guy
 
Andrew Will
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the code I am using. I have tested this with hundreds of files on a Windows XP box copying files from the C:\ to a external USB drive and it works fine, but when I run the same test on a Linux box about 40% of the files fail on the following line:

dstChannel.transferFrom(srcChannel, 0, srcChannel.size())

with a java.io.IOException Bad Address error. The files that fail are sporatic. When I run the test again, other files fail, but then many of the files that failed from the first test now pass the second test.

///////////////////////////////////////////////////////////

public static boolean copyFile(File source, File dest) {

FileChannel srcChannel = null;
FileChannel dstChannel = null;

boolean result;

try {
srcChannel = new FileInputStream(source).getChannel();
dstChannel = new FileOutputStream(dest).getChannel();

// Copy file contents from source to destination
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
result = true;
}
catch (FileNotFoundException fe) {
result = false;
fe.printStackTrace();
}
catch (IOException ie) {
result = false;
ie.printStackTrace();
}
finally {
try { srcChannel.close(); } catch (IOException ie) {}
try { dstChannel.close(); } catch (IOException ie) {}
}

return result;
}

///////////////////////////////////////////////////////////
 
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew,

Just for grins, can you do a


before you write to it?
 
Andrew Will
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yeah, I do the file.exists() in the code that calls this method.


Andrew
 
Andrew Will
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found solution:

Bug ID 4641545
[ October 28, 2004: Message edited by: Andrew Will ]
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is marked "Closed, Not Reproducible".

you should add some comments, provide a more robust test, and request that it be repopened.

Guy
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this problem is going to be very closely related to:

-The OS you are running (seems to be *nix related)
-JRE level

This morning I ran this 1,000,000 times on Fedora Core 2, JRE 1.5 with no IOExceptions.

I now have it running in background for 1,000,000,000 times. No exceptions yet, I expect it to complete ???, few days at least, maybe a week or 2.

N.B. - I am only catching IOE's not FNFE's).

Guy
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!