Friday, November 5, 2010

Where is the Java 6 SDK source code on my Mac?

My IntelliJ IDEA stopped linking the Java source code after I upgraded my Mac to Java 6.
Well, that's because src.jar is no longer shipped with the standard Java distribution. Instead, you have to the get the developer version from Apple's developer connect website at http://connect.apple.com.

Here is the URL I used - it may not work for you unless you login.

Also, you can find where your Java is installed after you downloaded it from Developer Connection from these release notes. src.jar will be available at this location.

On my Mac, it is /Library/Java/JavaVirtualMachines/1.6.0_22-b04-307.jdk/Contents/Home/src.jar

Have a good one!

Wednesday, September 23, 2009

FileChannel.truncate() will not correctly position the seek ptr.

As per the javadoc for FileChannel.truncate()

If the given size is less than the file's current size then the file is truncated, discarding any bytes beyond the new end of the file. If the given size is greater than or equal to the file's current size then the file is not modified. In either case, if this channel's file position is greater than the given size then it is set to that size.

I was truncating the file by doing:
channel.truncate(0)


At this point, I was expecting the position() of the channel to be set to 0 - i.e. the begining of the file.

What was really happening, though, was that the FileChannel was not resetting it's position to 0 - in fact the position stayed where it was. And I would see NULL bytes for the size of the file before the truncation prefixed to the new data that is being written to the file.

The easy fix, of course, was to:
channel.truncate(0);
channel.position(0);


As easy as the fix is, it did waste an hour of my work, that did not deserve to be wasted.

My configuration:
Intel MBP 2009 Snow Leopard.
Java 6.

I am not sure if this is a platform specific issue, but will find out soon.

Good Luck!