ac3856be4a188f28bfa197b0ed3b7f1df843c20e
[svjatoslav_commons.git] / FilePathParser.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality.
3  * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public License
7  * as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.commons.file;
11
12 import java.io.File;
13
14 public class FilePathParser {
15
16         public static String getFileExtension(final File file) {
17                 final String fullFileName = file.getName();
18
19                 return getFileExtension(fullFileName);
20         }
21
22         public static String getFileExtension(final String fullFileName) {
23                 final int dot = fullFileName.lastIndexOf('.');
24                 String fileExtension;
25                 if (dot == -1)
26                         fileExtension = "";
27                 else {
28                         fileExtension = fullFileName.substring(dot + 1);
29                         fileExtension = fileExtension.toLowerCase();
30                 }
31
32                 return fileExtension;
33         }
34
35         public static String getFileNameWithoutExtension(final File file) {
36                 final String fullFileName = file.getName();
37                 return getFileNameWithoutExtension(fullFileName);
38         }
39
40         public static String getFileNameWithoutExtension(final String fullFileName) {
41                 final int dot = fullFileName.lastIndexOf('.');
42                 String fileName;
43                 if (dot == -1)
44                         fileName = fullFileName;
45                 else
46                         fileName = fullFileName.substring(0, dot);
47
48                 return fileName;
49         }
50
51         public static String getFileSizeDescription(long fileSize) {
52                 String suffix = "b";
53
54                 if (fileSize > (1024 * 1024 * 10)) {
55                         fileSize = fileSize / (1024 * 1024);
56                         suffix = "Mb";
57                 } else if (fileSize > (1024 * 10)) {
58                         fileSize = fileSize / 1024;
59                         suffix = "Kb";
60                 }
61
62                 final String fileSizeString = String.valueOf(fileSize) + " " + suffix;
63
64                 return fileSizeString;
65         }
66 }