Skip to content

Commit d21fa00

Browse files
author
Stephen Colebourne
committed
Improve validation and directory creation in LockableFileWriter
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@292959 13f79535-47bb-0310-9956-ffa450edef68
1 parent e5cc89d commit d21fa00

2 files changed

Lines changed: 29 additions & 19 deletions

File tree

RELEASE-NOTES.txt

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,22 @@ Binary compatible - Yes
2222
Source compatible - Yes
2323

2424
Semantic compatible - Yes, except:
25-
- FileUtils.touch()
25+
- FileUtils.writeStringToFile()
26+
A null encoding previously used 'ISO-8859-1', now it uses the platform default
27+
Generally this will make no difference
28+
29+
- LockableFileWriter
30+
Improved validation and now create directories if necesssary
31+
32+
plus these bug fixes may affect you semantically:
33+
- FileUtils.touch() (Bug fix 29821)
2634
Now creates the file if it did not previously exist
27-
(Bug fix 29821)
2835

29-
- FileUtils.toFile(URL)
36+
- FileUtils.toFile(URL) (Bug fix 32575)
3037
Now handles escape syntax such as %20
31-
(Bug fix 32575)
3238

33-
- FileUtils.sizeOfDirectory()
39+
- FileUtils.sizeOfDirectory() (Bug fix 36801)
3440
May now return a size of 0 if the directory is security restricted
35-
(Bug fix 36801)
36-
37-
- FileUtils.writeStringToFile()
38-
A null encoding previously used 'ISO-8859-1', now it uses the platform default
39-
Generally this will make no difference
4041

4142

4243
Deprecations from 1.0
@@ -173,6 +174,8 @@ Enhancements from 1.0
173174

174175
- LockableFileWriter - encoding support [36825]
175176
Add support for character encodings to LockableFileWriter
177+
Improve the validation
178+
Create directories if necesssary
176179

177180
- IOUtils and EndianUtils are no longer final [28978]
178181
Allows developers to have subclasses if desired

src/java/org/apache/commons/io/output/LockableFileWriter.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.io.OutputStreamWriter;
2323
import java.io.Writer;
2424

25+
import org.apache.commons.io.FileUtils;
26+
2527
/**
2628
* FileWriter that will create and honor lock files to allow simple
2729
* cross thread file lock handling.
@@ -153,19 +155,25 @@ public LockableFileWriter(File file, String encoding) throws IOException {
153155
public LockableFileWriter(File file, String encoding, boolean append,
154156
String lockDir) throws IOException {
155157
super();
158+
// init file to create/append
156159
file = file.getAbsoluteFile();
157-
if (file.exists()) {
158-
if (file.isDirectory()) {
159-
throw new IOException("File specified is a directory");
160-
}
161-
} else if (file.getParentFile() != null) {
162-
file.getParentFile().mkdirs();
160+
if (file.getParentFile() != null) {
161+
FileUtils.forceMkdir(file.getParentFile());
163162
}
163+
if (file.isDirectory()) {
164+
throw new IOException("File specified is a directory");
165+
}
166+
167+
// init lock file
164168
if (lockDir == null) {
165169
lockDir = System.getProperty("java.io.tmpdir");
166170
}
167-
testLockDir(new File(lockDir));
168-
this.lockFile = new File(lockDir, file.getName() + LCK);
171+
File lockDirFile = new File(lockDir);
172+
FileUtils.forceMkdir(lockDirFile);
173+
testLockDir(lockDirFile);
174+
this.lockFile = new File(lockDirFile, file.getName() + LCK);
175+
176+
// init wrapped writer
169177
try {
170178
createLock();
171179
if (encoding == null) {
@@ -178,7 +186,6 @@ public LockableFileWriter(File file, String encoding, boolean append,
178186
this.lockFile.delete();
179187
throw ioe;
180188
}
181-
182189
this.out = new FileWriter(file.getAbsolutePath(), append);
183190
}
184191

0 commit comments

Comments
 (0)