Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get system block size in Java

I'm trying to write the fastest, most optimal file saving method possible. Is there any way to get the system block size in java? Something like System.getProperty("block.size") or something.

like image 315
kentcdodds Avatar asked May 07 '12 14:05

kentcdodds


2 Answers

The 'fastest most optimal way possible' is surely to write using the biggest buffer you can afford; to make sure its size is a power of two (a megabyte comes to mind); and to make sure that the writes themselves are buffer-aligned:

new BufferedOutputStream(new FileOutputStream(file), 1024*1024);

As long as you are over the system block size, which you will be at this size, and remain aligned with it, which is guaranteed by the BufferedOutputStream, this is about as optimal as it gets.

You should also look into FileChannel.transferTo(), noting that you must call it in a loop, and that the actual implementations so far don't appear to use any low-level operating system primitives (contrary to the advertising), just the same kind of loop you could write yourself.

like image 148
user207421 Avatar answered Oct 18 '22 21:10

user207421


I don't think there is a java function to do that. However there is a system dependent way to retrieve that information. Have a look at this answer. You'll need to use Runtime.exec() and parse the data.

like image 38
user845279 Avatar answered Oct 18 '22 21:10

user845279