Skip to content

Commit 0db281c

Browse files
committed
Use Path instead of File
1 parent 089f103 commit 0db281c

4 files changed

Lines changed: 44 additions & 174 deletions

File tree

src/main/java/com/flowpowered/commons/FileUtil.java renamed to src/main/java/com/flowpowered/commons/PathUtil.java

Lines changed: 18 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -25,48 +25,34 @@
2525

2626
import java.io.BufferedReader;
2727
import java.io.BufferedWriter;
28-
import java.io.File;
29-
import java.io.FileInputStream;
3028
import java.io.FileNotFoundException;
31-
import java.io.FileOutputStream;
32-
import java.io.FileReader;
33-
import java.io.FileWriter;
3429
import java.io.IOException;
3530
import java.io.InputStream;
3631
import java.net.URL;
3732
import java.net.URLConnection;
38-
import java.nio.channels.FileChannel;
39-
import java.util.ArrayList;
33+
import java.nio.file.Files;
34+
import java.nio.file.Path;
4035
import java.util.Collection;
4136
import java.util.HashMap;
4237
import java.util.LinkedList;
4338

44-
public class FileUtil {
39+
public class PathUtil {
4540
private static final HashMap<String, String> fileNameCache = new HashMap<>();
4641

4742
/**
48-
* Computes a long CRC of a File
43+
* Computes a long CRC of a Path
4944
*
5045
* @param file the file to process
51-
* @param a buffer for temporary data
46+
* @param buffer a buffer for temporary data
5247
* @return the CRC or 0 on failure
5348
*/
54-
public static long getCRC(File file, byte[] buffer) {
55-
56-
FileInputStream in = null;
57-
58-
try {
59-
in = new FileInputStream(file);
49+
public static long getCRC(Path file, byte[] buffer) {
50+
try (InputStream in = Files.newInputStream(file)) {
6051
return getCRC(in, buffer);
6152
} catch (FileNotFoundException e) {
6253
return 0;
63-
} finally {
64-
if (in != null) {
65-
try {
66-
in.close();
67-
} catch (IOException e) {
68-
}
69-
}
54+
} catch (IOException ex) {
55+
return 0;
7056
}
7157
}
7258

@@ -150,147 +136,48 @@ public static String getFileName(String url) {
150136
}
151137

152138
/**
153-
* Writes a Collection of Strings to a File, overwriting any previous file contents.
139+
* Writes a Collection of Strings to a Path, overwriting any previous file contents.
154140
*
155-
* Each String is converted into a line in the File.
141+
* Each String is converted into a line in the Path.
156142
*
157143
* @param strings the Collection of Strings
158-
* @param file the file to write
144+
* @param path the file to write
159145
* @return true on success
160146
*/
161-
public static boolean stringToFile(Collection<String> strings, File file) {
162-
BufferedWriter bw;
163-
164-
try {
165-
bw = new BufferedWriter(new FileWriter(file));
166-
} catch (FileNotFoundException fnfe) {
167-
return false;
168-
} catch (IOException ioe) {
169-
return false;
170-
}
147+
public static boolean stringToFile(Collection<String> strings, Path path) {
171148

172-
try {
149+
try (BufferedWriter bw = Files.newBufferedWriter(path)) {
173150
for (String line : strings) {
174151
bw.write(line);
175152
bw.newLine();
176153
}
177154
return true;
178155
} catch (IOException ioe) {
179156
return false;
180-
} finally {
181-
try {
182-
bw.close();
183-
} catch (IOException ioe2) {
184-
}
185157
}
186158
}
187159

188160
/**
189-
* Reads a File and places the contents into a collection of Strings.
161+
* Reads a Path and places the contents into a collection of Strings.
190162
*
191-
* Each line in the File is converted into a String.
163+
* Each line in the Path is converted into a String.
192164
*
193165
* Iterators on the List will iterate through the Strings in the order the lines appear in the file
194166
*
195167
* @param the file to read
196168
* @return the Collection of Strings or null on failure
197169
*/
198-
public static Collection<String> fileToString(File file) {
199-
200-
BufferedReader br;
201-
202-
try {
203-
br = new BufferedReader(new FileReader(file));
204-
} catch (FileNotFoundException fnfe) {
205-
return null;
206-
}
207-
170+
public static Collection<String> fileToString(Path path) {
208171
String line;
209172

210-
try {
173+
try (BufferedReader br = Files.newBufferedReader(path)) {
211174
Collection<String> strings = new LinkedList<>();
212175
while ((line = br.readLine()) != null) {
213176
strings.add(line);
214177
}
215178
return strings;
216179
} catch (IOException | NumberFormatException ioe) {
217180
return null;
218-
} finally {
219-
try {
220-
br.close();
221-
} catch (IOException ioe) {
222-
}
223181
}
224182
}
225-
226-
private static final Collection<String> emptyCollection = new ArrayList<>();
227-
228-
/**
229-
* Creates a blank file
230-
*
231-
* @param the file to create
232-
* @return true on success
233-
*/
234-
public static boolean createFile(File file) {
235-
if (file == null) {
236-
return false;
237-
}
238-
File dir = file.getParentFile();
239-
if (dir != null) {
240-
if (!dir.exists()) {
241-
if (!dir.mkdirs()) {
242-
return false;
243-
}
244-
} else {
245-
if (!dir.isDirectory()) {
246-
return false;
247-
}
248-
}
249-
}
250-
return FileUtil.stringToFile(emptyCollection, file);
251-
}
252-
253-
/**
254-
* Copies one file to another location
255-
*
256-
* @param inFile the source filename
257-
* @param outFile the target filename
258-
* @return true on success
259-
*/
260-
@SuppressWarnings ("resource")
261-
public static boolean copy(File inFile, File outFile) {
262-
if (!inFile.exists()) {
263-
return false;
264-
}
265-
266-
FileChannel in = null;
267-
FileChannel out = null;
268-
269-
try {
270-
in = new FileInputStream(inFile).getChannel();
271-
out = new FileOutputStream(outFile).getChannel();
272-
273-
long pos = 0;
274-
long size = in.size();
275-
276-
while (pos < size) {
277-
pos += in.transferTo(pos, 10 * 1024 * 1024, out);
278-
}
279-
} catch (IOException ioe) {
280-
return false;
281-
} finally {
282-
try {
283-
if (in != null) {
284-
in.close();
285-
}
286-
if (out != null) {
287-
out.close();
288-
}
289-
} catch (IOException ioe) {
290-
return false;
291-
}
292-
}
293-
294-
return true;
295-
}
296183
}

src/main/java/com/flowpowered/commons/StringUtil.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
*/
2424
package com.flowpowered.commons;
2525

26-
import java.io.File;
2726
import java.util.ArrayList;
2827
import java.util.Collection;
2928
import java.util.List;
@@ -101,26 +100,6 @@ public static <T extends Named> Collection<T> matchName(Collection<T> values, St
101100
return result;
102101
}
103102

104-
/**
105-
* Matches a file name using a name
106-
*
107-
* @param values to look into
108-
* @param name to match against
109-
* @return a collection of values that matched
110-
*/
111-
public static Collection<File> matchFile(Collection<File> values, String name) {
112-
List<File> result = new ArrayList<>();
113-
for (File value : values) {
114-
if (value == null) {
115-
continue;
116-
}
117-
if (startsWithIgnoreCase(value.getName(), name)) {
118-
result.add(value);
119-
}
120-
}
121-
return result;
122-
}
123-
124103
/**
125104
* Gets the named object with the shortest name from the values specified
126105
*

src/main/java/com/flowpowered/commons/store/BinaryFileStore.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,34 @@
2828
import java.io.DataInputStream;
2929
import java.io.DataOutputStream;
3030
import java.io.EOFException;
31-
import java.io.File;
32-
import java.io.FileInputStream;
33-
import java.io.FileOutputStream;
3431
import java.io.IOException;
32+
import java.nio.file.Files;
33+
import java.nio.file.Path;
3534
import java.util.Iterator;
3635
import java.util.Map.Entry;
3736

3837
/**
3938
* This implements a SimpleStore that is stored in memory. The save and load methods can be used to write the map to a binary file.
4039
*/
4140
public class BinaryFileStore extends MemoryStore<Integer> {
42-
private File file;
41+
private Path path;
4342
private boolean dirty = true;
4443

45-
public BinaryFileStore(File file) {
44+
public BinaryFileStore(Path path) {
4645
super();
47-
this.file = file;
46+
this.path = path;
4847
}
4948

5049
public BinaryFileStore() {
5150
this(null);
5251
}
5352

54-
public synchronized void setFile(File file) {
55-
this.file = file;
53+
public synchronized void setPath(Path path) {
54+
this.path = path;
5655
}
5756

58-
public synchronized File getFile() {
59-
return file;
57+
public synchronized Path getPath() {
58+
return path;
6059
}
6160

6261
@Override
@@ -74,7 +73,7 @@ public synchronized boolean save() {
7473
boolean saved = true;
7574
DataOutputStream out = null;
7675
try {
77-
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
76+
out = new DataOutputStream(new BufferedOutputStream(Files.newOutputStream(path)));
7877
Iterator<Entry<String, Integer>> itr = super.getEntrySet().iterator();
7978

8079
while (itr.hasNext()) {
@@ -106,7 +105,7 @@ public synchronized boolean load() {
106105
boolean loaded = true;
107106
DataInputStream in = null;
108107
try {
109-
in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
108+
in = new DataInputStream(new BufferedInputStream(Files.newInputStream(path)));
110109

111110
boolean eof = false;
112111
while (!eof) {

src/main/java/com/flowpowered/commons/store/FlatFileStore.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,34 @@
2323
*/
2424
package com.flowpowered.commons.store;
2525

26-
import java.io.File;
26+
import java.io.IOException;
27+
import java.nio.file.Files;
28+
import java.nio.file.Path;
2729
import java.util.ArrayList;
2830
import java.util.Collection;
2931
import java.util.Iterator;
3032
import java.util.Map.Entry;
3133

32-
import com.flowpowered.commons.FileUtil;
34+
import com.flowpowered.commons.PathUtil;
3335

3436
/**
3537
* This implements a SimpleStore that is stored in memory. The save and load methods can be used to write the map to a File.
3638
*/
3739
public class FlatFileStore<T> extends MemoryStore<T> {
38-
private final File file;
40+
private final Path path;
3941
private boolean dirty = false;
4042
private final Class<?> clazz; // preserve class, so parser knows what to do
4143

42-
public FlatFileStore(File file, Class<?> clazz) {
44+
public FlatFileStore(Path path, Class<?> clazz) {
4345
super();
4446
this.clazz = clazz;
45-
this.file = file;
46-
if (file != null) {
47-
if (!file.exists()) {
48-
if (!FileUtil.createFile(file)) {
47+
this.path = path;
48+
if (path != null) {
49+
if (!Files.exists(path)) {
50+
try {
51+
Files.createFile(path);
52+
} catch (IOException ex) {
53+
ex.printStackTrace();
4954
}
5055
}
5156
}
@@ -64,7 +69,7 @@ public synchronized boolean save() {
6469
}
6570

6671
Collection<String> strings = getStrings();
67-
boolean saved = FileUtil.stringToFile(strings, file);
72+
boolean saved = PathUtil.stringToFile(strings, path);
6873
if (saved) {
6974
dirty = false;
7075
}
@@ -74,7 +79,7 @@ public synchronized boolean save() {
7479

7580
@Override
7681
public synchronized boolean load() {
77-
Collection<String> strings = FileUtil.fileToString(file);
82+
Collection<String> strings = PathUtil.fileToString(path);
7883
if (strings == null) {
7984
return false;
8085
}

0 commit comments

Comments
 (0)