Writes the options to disk.
()
| 117 | * Writes the options to disk. |
| 118 | */ |
| 119 | public final synchronized void write() { |
| 120 | final StringList lines = new StringList(); |
| 121 | try { |
| 122 | for(final Option<?> option : options(getClass())) { |
| 123 | final String name = option.name(); |
| 124 | if(option instanceof Comment) { |
| 125 | if(!lines.isEmpty()) lines.add(""); |
| 126 | lines.add("# " + name); |
| 127 | } else if(option instanceof final NumbersOption no) { |
| 128 | final int[] ints = get(no); |
| 129 | final int is = ints == null ? 0 : ints.length; |
| 130 | for(int i = 0; i < is; i++) lines.add(name + i + " = " + ints[i]); |
| 131 | } else if(option instanceof final StringsOption so) { |
| 132 | final String[] strings = get(so); |
| 133 | final int ss = strings == null ? 0 : strings.length; |
| 134 | lines.add(name + " = " + ss); |
| 135 | for(int s = 0; s < ss; s++) lines.add(name + (s + 1) + " = " + strings[s]); |
| 136 | } else { |
| 137 | lines.add(name + " = " + get(option)); |
| 138 | } |
| 139 | } |
| 140 | lines.add("").add(PROPUSER).add(user); |
| 141 | |
| 142 | // only write file if contents have changed |
| 143 | final TokenBuilder tb = new TokenBuilder(); |
| 144 | for(final String line : lines) tb.add(line).add(NL); |
| 145 | final byte[] contents = tb.finish(); |
| 146 | |
| 147 | boolean skip = file.exists(); |
| 148 | if(skip) { |
| 149 | final TokenBuilder tmp = new TokenBuilder(contents.length); |
| 150 | try(NewlineInput nli = new NewlineInput(file)) { |
| 151 | for(String line; (line = nli.readLine()) != null;) tmp.add(line).add(NL); |
| 152 | } |
| 153 | skip = eq(contents, tmp.finish()); |
| 154 | } |
| 155 | if(!skip) { |
| 156 | file.parent().md(); |
| 157 | file.write(contents); |
| 158 | } |
| 159 | } catch(final Exception ex) { |
| 160 | Util.errln("% could not be written.", file); |
| 161 | Util.debug(ex); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Returns the option with the specified name. |