How can the block size of a file store be portably obtained in Java 7?
Asked Answered
P

2

4

I've looked at java.nio.file.attribute.Attributes and java.nio.file.FileStore, but couldn't find a way to discover the block-size of a disk-file.

Prefabricate answered 8/10, 2010 at 15:48 Comment(2)
Isn't block size intrinsically non-portable? What if the file store doesn't use blocks (sure unlikely, but possible)?Operand
@Mark The vast majority of file-stores are implemented via spinning disks and, consequently, use blocks. I can handle the case where the block size is one byte. What I want is a portable way to determine that size.Prefabricate
C
0

Here is an article on buffer size vs I/O throughput. Synopsis: nio handles all of that for you. This SO post concludes you should use a power of 2.

Chewink answered 8/10, 2010 at 17:7 Comment(2)
Devon, I want a way to determine the block size programmatically that doesn't rely on conducting an experiment and interpreting the results.Prefabricate
I understand that. However, there is no mechanism in Java that exposes the fielsystem's preferred IO size. Given that, the best fall back position is to use a likely multiple and trust the io/nio subsystem to do the right thing. Generally speaking, filesystems use I/O sizes of 4K, 8K, or 16K. So, using one of those sizes will generally be sufficient.Chewink
I
0

Java 7 does not have the requested information, and the users question is related to Java 7.

But in Java 10, there was a new method introduced which provides the requested information.

The getBlockSize() method of a FileStore class is used to return the number of bytes per block in this file store Object. Every File storage is organized into discrete sequences of bytes called blocks and a block is the smallest storage unit of a file store. Every read and write operation is performed on multiple blocks. This method is helpful in getting the size of Block.

Ic answered 9/12, 2020 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.