0

Is it possible to read whole partition of disk in java?

Partition is treated as directory (obviously) and because of it I can't create FileInputStream which I need.

I'd like to compute hash for whole partition of disk (byte by byte) and I was wondering how to achieve that.

If that matters it has to work both on Windows and Linux.

Any thoughts are appreciated!

ICeQ
  • 150
  • 11
  • 4
    You can do this on unix by reading the raw device e.g. `/dev/sd1c` There might be an equivalent for windows. – Peter Lawrey Mar 25 '14 at 14:14
  • possible duplicate of [Get partition and volume information](http://stackoverflow.com/questions/15656139/get-partition-and-volume-information) – Vishrant Mar 25 '14 at 14:27
  • @PeterLawrey thanks, that should work on linux, but I still can't find equivalent solution on Windows. – ICeQ Mar 25 '14 at 14:45
  • @Vishrant I think that's not related, I need to read partition byte by byte, not just get informations about it. – ICeQ Mar 25 '14 at 14:46
  • for a sake of curiosity, why you want to read partition in that way, why don't you get the list of files and read it. – Vishrant Mar 25 '14 at 14:58
  • As I mentioned in the question I have to compute hash for whole partition, but not like computing hashes for all files and make it somehow in one hash, but compute it like for one big file. – ICeQ Mar 25 '14 at 15:02

2 Answers2

2

Try that with administrator's permissions:

File diskRoot = new File ("\\.\X:"); //X is your requested partition's letter 
RandomAccessFile diskAccess = new RandomAccessFile(diskRoot, "r");
byte[] content = new byte[1024];
diskAccess.readFully (content);

You can also use BufferedInputStream. It's the hunsricker's answer from How to access specific raw data on disk from java

The naming convention can be found here: http://support.microsoft.com/kb/100027

Community
  • 1
  • 1
Michal
  • 6,411
  • 6
  • 32
  • 45
  • Thanks for hint - especially for naming convention in windows - that helped a lot! In fact, that didn't worked, because RandomAccesFile#read(), readFully() and other read functions throws java.io.Exception: Parameter is invalid. But I made it work with just InputStream (solution below). – ICeQ Mar 28 '14 at 11:05
1

As @Michal suggested I've changed path to partition to "\\.\X:" convention.

Code looks like this:

File diskRoot = new File("\\\\.\\F:");
byte[] buffer = new byte[1024];
try (InpuStream stream = new FileInputStream(diskRoot)) {
    while(stream.read(buffer) != -1) {
        //do something with buffer
    }
} catch(IOException e) {
    //handle exception
}

Thanks for the comments and answers!

ICeQ
  • 150
  • 11