commit | author | age
|
7fdd7e
|
1 |
/* |
U |
2 |
http-cm - File management extensions to jdk.httpserver |
|
3 |
Copyright (C) 2021 Ulrich Hilger |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.httpserver.cm; |
|
19 |
|
|
20 |
import java.io.File; |
|
21 |
import java.util.logging.Logger; |
|
22 |
|
|
23 |
/** |
|
24 |
* FileTransporter |
|
25 |
* |
|
26 |
* @author Ulrich Hilger |
|
27 |
* @version 1, 04.07.2021 |
|
28 |
*/ |
|
29 |
public class FileTransporter { |
|
30 |
|
|
31 |
private static final Logger logger = Logger.getLogger(FileTransporter.class.getName()); |
|
32 |
|
|
33 |
/** |
|
34 |
* Einen Namen fuer eine Datei erzeugen, der noch nicht existiert. |
|
35 |
* |
|
36 |
* Der Dateiname wird wie folgt gebildet |
|
37 |
* '[Bestehender Dateiname]-[Nummer].[Erweiterung]' |
|
38 |
* |
|
39 |
* Ordner haben in der Regel keine Erweiterung, dann lautet der neue Name |
|
40 |
* '[Bestehender Ordnername]-[Nummer]' |
|
41 |
* |
|
42 |
* Fuer Nummer wird eine Nummer mit 1 beginnend heraufgezaehlt bis ein |
|
43 |
* Dateiname entsteht, den es im betreffenden Ordner noch nicht gibt. |
|
44 |
* |
|
45 |
* @param file die Datei, die schon existiert |
|
46 |
* @return ein Dateiname, der im Ordner noch nicht verwendet wird |
|
47 |
*/ |
|
48 |
public File getNewFileName(File file) { |
|
49 |
File dir = file.getParentFile(); |
|
50 |
String targetName = file.getName(); |
|
51 |
logger.fine("targetName: " + targetName); |
|
52 |
String ext = ""; |
|
53 |
int dotpos = targetName.indexOf("."); |
|
54 |
if (dotpos > -1) { |
|
55 |
ext = targetName.substring(dotpos); |
|
56 |
targetName = targetName.substring(0, dotpos); |
|
57 |
} |
|
58 |
logger.fine("targetName: " + targetName + ", ext: " + ext); |
|
59 |
int i = 1; |
|
60 |
while (file.exists()) { |
|
61 |
StringBuffer buf = new StringBuffer(); |
|
62 |
buf.append(targetName); |
|
63 |
buf.append("-"); |
|
64 |
buf.append(i); |
|
65 |
if (ext.length() > 0) { |
|
66 |
buf.append(ext); |
|
67 |
} |
|
68 |
file = new File(dir, buf.toString()); |
|
69 |
i++; |
|
70 |
} |
|
71 |
logger.fine("new file: " + file.getName()); |
|
72 |
return file; |
|
73 |
} |
|
74 |
|
|
75 |
} |