This discussion is archived
9 Replies Latest reply: May 7, 2013 2:35 PM by jschellSomeoneStoleMyAlias RSS

Raw disk access from Java on Windows.

1006845 Newbie
Currently Being Moderated
Dear forum,

I have, for various reasons, been inspired to try open a channel to a raw disk in Windows using RandomAccessFile. If I use the following statement while running as an administrator:

RandomAccessFile file = new RandomAccessFile("\\\\.\ \PhysicalDrive1","r");

I can successfully open the device. However, if I try to open for write...

RandomAccessFile file = new RandomAccessFile("\\\\.\ \PhysicalDrive1","rw");

I keep getting FileNotFoundExceptions. Has anyone ever tried to do this in Java on Windows? Any pointers are much appreciated.

Edited by: 1003842 on May 2, 2013 4:27 PM
  • 1. Re: Raw disk access from Java on Windows.
    Kayaman Guru
    Currently Being Moderated
    1003842 wrote:
    RandomAccessFile file = new RandomAccessFile("\\\\.\ \PhysicalDrive1","r");
    I can successfully open the device.
    Can you read from it successfully?

    I'd be surprised if Java allowed for such low level operation. What gave you the idea this is possible?
  • 2. Re: Raw disk access from Java on Windows.
    gimbal2 Guru
    Currently Being Moderated
    I'm more amazed that the OS allows it, I had no clue there was such a possibility in Windows.

    I still wouldn't know why you would ever want to write low level to a disc though, and I wonder if the OS would allow that. Even if it does, I wonder if the rights of the user are sufficient to allow it. It seems like something that can really cause some damage.
  • 3. Re: Raw disk access from Java on Windows.
    EJP Guru
    Currently Being Moderated
    There's no way Windows or any sane operating system will allow you write access to a raw drive while there are mounted filesystems on it.
  • 4. Re: Raw disk access from Java on Windows.
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    EJP wrote:
    There's no way Windows or any sane operating system will allow you write access to a raw drive while there are mounted filesystems on it.
    There must be a terminology problem in that response or in this thread.

    I am fairly certain that every modern desktop OS provides "raw" (also called "direct") access to file systems - even when in use. Without it I would suppose that it would be impossible to write certain tools/drivers/etc.

    This isn't of course suggesting that it is a good idea or even one that is likely to succeed unless one is very, very careful. And I doubt it is possible in Java.

    Following references which seem to suggest that raw access is possible. And I didn't see anything that suggested that it couldn't happen while the device was in use.

    Windows

    http://www.codeproject.com/Articles/28314/Reading-and-Writing-to-Raw-Disk-Sectors
    http://support.microsoft.com/kb/100027

    Linux

    http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=%2Fcom.ibm.db2.udb.admin.doc%2Fdoc%2Ft0004971.htm
    https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Tuning_and_Optimizing_Red_Hat_Enterprise_Linux_for_Oracle_9i_and_10g_Databases/chap-Oracle_9i_and_10g_Tuning_Guide-Configuring_IO_for_Raw_Partitions.html

    OSX

    http://stackoverflow.com/questions/9073614/open-raw-disk-and-get-size-os-x
  • 5. Re: Raw disk access from Java on Windows.
    EJP Guru
    Currently Being Moderated
    Nothing in any of those links refers to co-existence with mounted file systems.
  • 6. Re: Raw disk access from Java on Windows.
    1006845 Newbie
    Currently Being Moderated
    EJP,

    I suppose I should have clarified that point: I am trying to use block devices without any file systems. As a comparison, I was able to try out this code on a Linux distro yesterday, and when I ran as root, it seemed to work on a ramdisk without a mounted fs:

    RandomAccessFile file = new RandomAccessFile("/dev/ram2","rw");
    ByteBuffer buf = ByteBuffer.allocateDirect(512);
    file.geChannel().write(buf, 1024);

    Again, the question is why I can't do something similar in Windows, even when I run as administrator, and even when I can open a block device for read in Java.
  • 7. Re: Raw disk access from Java on Windows.
    gimbal2 Guru
    Currently Being Moderated
    I am trying to use block devices without any file systems. As a comparison, I was able to try out this code on a Linux distro yesterday, and when I ran as root, it seemed to work on a ramdisk without a mounted fs:
    This entire line is contradictory.The ramdisk may not have a filesystem itself, but /dev/ram2 most definitely IS mounted into a filesystem. And because it is you have easy access to it because Linux makes that possible.

    Windows on the other hand is not Linux. You can't throw Java at this problem and then automagically have a cross-platform way of accessing OS specific resources. The answer to your query should likely be found in a Windows programming forum and may just require the use of native code, if it is at all possible to do on Windows.
  • 8. Re: Raw disk access from Java on Windows.
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    1003842 wrote:
    I keep getting FileNotFoundExceptions.
    Did you print the stack trace?
  • 9. Re: Raw disk access from Java on Windows.
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    1003842 wrote:
    Again, the question is why I can't do something similar in Windows,
    From the docs....

    "To open a physical hard drive"
    "where N is 0, 1, 2, and ..."

    So perhaps the device name you are mapping to is not writable.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points