Skip to content

Commit 9a70427

Browse files
committed
Add FileSystem.getBlockSize()
1 parent e75b973 commit 9a70427

3 files changed

Lines changed: 33 additions & 15 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,9 @@ The <action> type attribute can be add,update,fix,remove.
465465
<action issue="IO-786" dev="ggregory" type="add" due-to="Gary Gregory, Benoit Tellier">
466466
Add UnsynchronizedBufferedInputStream.
467467
</action>
468+
<action dev="ggregory" type="add" due-to="Gary Gregory">
469+
Add FileSystem.getBlockSize().
470+
</action>
468471
<!-- UPDATE -->
469472
<action dev="kinow" type="update" due-to="Dependabot, Gary Gregory">
470473
Bump actions/cache from 2.1.6 to 3.0.10 #307, #337, #393.

src/main/java/org/apache/commons/io/FileSystem.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ public enum FileSystem {
3636
/**
3737
* Generic file system.
3838
*/
39-
GENERIC(false, false, Integer.MAX_VALUE, Integer.MAX_VALUE, new int[] { 0 }, new String[] {}, false, false, '/'),
39+
GENERIC(4096, false, false, Integer.MAX_VALUE, Integer.MAX_VALUE, new int[] { 0 }, new String[] {}, false, false, '/'),
4040

4141
/**
4242
* Linux file system.
4343
*/
44-
LINUX(true, true, 255, 4096, new int[] {
44+
LINUX(8192, true, true, 255, 4096, new int[] {
4545
// KEEP THIS ARRAY SORTED!
4646
// @formatter:off
4747
// ASCII NUL
@@ -53,7 +53,7 @@ public enum FileSystem {
5353
/**
5454
* MacOS file system.
5555
*/
56-
MAC_OSX(true, true, 255, 1024, new int[] {
56+
MAC_OSX(4096, true, true, 255, 1024, new int[] {
5757
// KEEP THIS ARRAY SORTED!
5858
// @formatter:off
5959
// ASCII NUL
@@ -76,8 +76,9 @@ public enum FileSystem {
7676
* @see <a href="https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#consoles">
7777
* CreateFileA function - Consoles (microsoft.com)</a>
7878
*/
79-
WINDOWS(false, true, 255,
80-
32000, new int[] {
79+
WINDOWS(4096, false, true,
80+
255, 32000, // KEEP THIS ARRAY SORTED!
81+
new int[] {
8182
// KEEP THIS ARRAY SORTED!
8283
// @formatter:off
8384
// ASCII NUL
@@ -87,9 +88,8 @@ public enum FileSystem {
8788
29, 30, 31,
8889
'"', '*', '/', ':', '<', '>', '?', '\\', '|'
8990
// @formatter:on
90-
}, // KEEP THIS ARRAY SORTED!
91-
new String[] { "AUX", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "CON", "CONIN$", "CONOUT$",
92-
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", "NUL", "PRN" }, true, true, '\\');
91+
}, new String[] { "AUX", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "CON", "CONIN$", "CONOUT$",
92+
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", "NUL", "PRN" }, true, true, '\\');
9393

9494
/**
9595
* <p>
@@ -296,6 +296,8 @@ private static boolean isOsNameMatch(final String osName, final String osNamePre
296296
private static String replace(final String path, final char oldChar, final char newChar) {
297297
return path == null ? null : path.replace(oldChar, newChar);
298298
}
299+
300+
private final int blockSize;
299301
private final boolean casePreserving;
300302
private final boolean caseSensitive;
301303
private final int[] illegalFileNameChars;
@@ -305,12 +307,12 @@ private static String replace(final String path, final char oldChar, final char
305307
private final boolean reservedFileNamesExtensions;
306308
private final boolean supportsDriveLetter;
307309
private final char nameSeparator;
308-
309310
private final char nameSeparatorOther;
310311

311312
/**
312313
* Constructs a new instance.
313314
*
315+
* @param blockSize file allocation block size in bytes.
314316
* @param caseSensitive Whether this file system is case-sensitive.
315317
* @param casePreserving Whether this file system is case-preserving.
316318
* @param maxFileLength The maximum length for file names. The file name does not include folders.
@@ -321,9 +323,10 @@ private static String replace(final String path, final char oldChar, final char
321323
* @param supportsDriveLetter Whether this file system support driver letters.
322324
* @param nameSeparator The name separator, '\\' on Windows, '/' on Linux.
323325
*/
324-
FileSystem(final boolean caseSensitive, final boolean casePreserving, final int maxFileLength,
325-
final int maxPathLength, final int[] illegalFileNameChars, final String[] reservedFileNames,
326-
final boolean reservedFileNamesExtensions, final boolean supportsDriveLetter, final char nameSeparator) {
326+
FileSystem(final int blockSize, final boolean caseSensitive, final boolean casePreserving,
327+
final int maxFileLength, final int maxPathLength, final int[] illegalFileNameChars,
328+
final String[] reservedFileNames, final boolean reservedFileNamesExtensions, final boolean supportsDriveLetter, final char nameSeparator) {
329+
this.blockSize = blockSize;
327330
this.maxFileNameLength = maxFileLength;
328331
this.maxPathLength = maxPathLength;
329332
this.illegalFileNameChars = Objects.requireNonNull(illegalFileNameChars, "illegalFileNameChars");
@@ -336,6 +339,16 @@ private static String replace(final String path, final char oldChar, final char
336339
this.nameSeparatorOther = FilenameUtils.flipSeparator(nameSeparator);
337340
}
338341

342+
/**
343+
* Gets the file allocation block size in bytes.
344+
* @return the file allocation block size in bytes.
345+
*
346+
* @since 2.12.0
347+
*/
348+
public int getBlockSize() {
349+
return blockSize;
350+
}
351+
339352
/**
340353
* Gets a cloned copy of the illegal characters for this file system.
341354
*

src/test/java/org/apache/commons/io/FileSystemTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import static org.junit.jupiter.api.Assertions.assertFalse;
2222
import static org.junit.jupiter.api.Assertions.assertTrue;
2323

24-
import java.io.IOException;
25-
2624
import org.apache.commons.lang3.SystemUtils;
2725
import org.junit.jupiter.api.Test;
2826
import org.junit.jupiter.api.condition.EnabledOnOs;
@@ -33,6 +31,10 @@
3331
*/
3432
public class FileSystemTest {
3533

34+
@Test
35+
public void testGetBlockSize() {
36+
assertTrue(FileSystem.getCurrent().getBlockSize() >= 0);
37+
}
3638

3739
@Test
3840
public void testGetCurrent() {
@@ -72,7 +74,7 @@ public void testIsReservedFileName() {
7274

7375
@Test
7476
@EnabledOnOs(OS.WINDOWS)
75-
public void testIsReservedFileNameOnWindows() throws IOException {
77+
public void testIsReservedFileNameOnWindows() {
7678
final FileSystem fs = FileSystem.WINDOWS;
7779
for (final String candidate : fs.getReservedFileNames()) {
7880
// System.out.printf("Reserved %s exists: %s%n", candidate, Files.exists(Paths.get(candidate)));

0 commit comments

Comments
 (0)