Changed license to CC0
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / file / FilePathParser.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.commons.file;
6
7 import java.io.File;
8
9 public class FilePathParser {
10
11     public static String getFileExtension(final File file) {
12         final String fullFileName = file.getName();
13
14         return getFileExtension(fullFileName);
15     }
16
17     public static String getFileExtension(final String fullFileName) {
18         final int dot = fullFileName.lastIndexOf('.');
19         String fileExtension;
20         if (dot == -1)
21             fileExtension = "";
22         else {
23             fileExtension = fullFileName.substring(dot + 1);
24             fileExtension = fileExtension.toLowerCase();
25         }
26
27         return fileExtension;
28     }
29
30     public static String getFileNameWithoutExtension(final File file) {
31         final String fullFileName = file.getName();
32         return getFileNameWithoutExtension(fullFileName);
33     }
34
35     public static String getFileNameWithoutExtension(final String fullFileName) {
36         final int dot = fullFileName.lastIndexOf('.');
37         String fileName;
38         if (dot == -1)
39             fileName = fullFileName;
40         else
41             fileName = fullFileName.substring(0, dot);
42
43         return fileName;
44     }
45
46     public static String getFileSizeDescription(long fileSize) {
47         String suffix = "b";
48
49         if (fileSize > (1024 * 1024 * 10)) {
50             fileSize = fileSize / (1024 * 1024);
51             suffix = "MiB";
52         } else if (fileSize > (1024 * 10)) {
53             fileSize = fileSize / 1024;
54             suffix = "KiB";
55         }
56
57         return String.valueOf(fileSize) + " " + suffix;
58     }
59 }