| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.ini4j; |
| 17 | |
|
| 18 | |
import java.io.File; |
| 19 | |
import java.io.FileWriter; |
| 20 | |
|
| 21 | |
import java.util.prefs.BackingStoreException; |
| 22 | |
|
| 23 | |
public class IniFile extends IniPreferences |
| 24 | |
{ |
| 25 | 4 | public static enum Mode |
| 26 | |
{ |
| 27 | 1 | RO, |
| 28 | 1 | WO, |
| 29 | 1 | RW |
| 30 | |
} |
| 31 | |
|
| 32 | |
private final File _file; |
| 33 | |
private final Mode _mode; |
| 34 | |
|
| 35 | |
public IniFile(File file) throws BackingStoreException |
| 36 | |
{ |
| 37 | 3 | this(file, Mode.RO); |
| 38 | 3 | } |
| 39 | |
|
| 40 | |
public IniFile(File file, Mode mode) throws BackingStoreException |
| 41 | |
{ |
| 42 | 8 | super(new Ini()); |
| 43 | 8 | _file = file; |
| 44 | 8 | _mode = mode; |
| 45 | 8 | if ((_mode == Mode.RO) || ((_mode != Mode.WO) && _file.exists())) |
| 46 | |
{ |
| 47 | 6 | sync(); |
| 48 | |
} |
| 49 | 7 | } |
| 50 | |
|
| 51 | |
public File getFile() |
| 52 | |
{ |
| 53 | 1 | return _file; |
| 54 | |
} |
| 55 | |
|
| 56 | |
public Mode getMode() |
| 57 | |
{ |
| 58 | 1 | return _mode; |
| 59 | |
} |
| 60 | |
|
| 61 | |
@Override public void flush() throws BackingStoreException |
| 62 | |
{ |
| 63 | 3 | if (_mode == Mode.RO) |
| 64 | |
{ |
| 65 | 1 | throw new BackingStoreException("read only instance"); |
| 66 | |
} |
| 67 | |
|
| 68 | |
try |
| 69 | |
{ |
| 70 | 2 | synchronized (lock) |
| 71 | |
{ |
| 72 | 2 | FileWriter writer = new FileWriter(_file); |
| 73 | |
|
| 74 | |
try |
| 75 | |
{ |
| 76 | 1 | getIni().store(writer); |
| 77 | |
} |
| 78 | |
finally |
| 79 | |
{ |
| 80 | 1 | writer.close(); |
| 81 | 1 | } |
| 82 | 1 | } |
| 83 | |
} |
| 84 | 1 | catch (Exception x) |
| 85 | |
{ |
| 86 | 1 | throw new BackingStoreException(x); |
| 87 | 1 | } |
| 88 | 1 | } |
| 89 | |
|
| 90 | |
@Override public void sync() throws BackingStoreException |
| 91 | |
{ |
| 92 | 8 | if (_mode == Mode.WO) |
| 93 | |
{ |
| 94 | 1 | throw new BackingStoreException("write only instance"); |
| 95 | |
} |
| 96 | |
|
| 97 | |
try |
| 98 | |
{ |
| 99 | 7 | synchronized (lock) |
| 100 | |
{ |
| 101 | 7 | getIni().load(_file.toURI().toURL()); |
| 102 | 6 | } |
| 103 | |
} |
| 104 | 1 | catch (Exception x) |
| 105 | |
{ |
| 106 | 1 | throw new BackingStoreException(x); |
| 107 | 6 | } |
| 108 | 6 | } |
| 109 | |
} |