blob: 768de5b3c271fd9cf7b50b7bd6d5b20d689f5318 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.task.util.editor;
import com.yahoo.vespa.hosted.node.admin.task.util.file.UnixPath;
import java.nio.file.Path;
/**
* @author hakon
*/
public class FileEditor {
private final UnixPath path;
private final StringEditor stringEditor;
private String fileText;
private Version fileVersion;
public static FileEditor open(Path path) {
UnixPath unixPath = new UnixPath(path);
String text = unixPath.readUtf8File();
StringEditor stringEditor = new StringEditor(text);
return new FileEditor(unixPath, text, stringEditor);
}
private FileEditor(UnixPath path, String fileText, StringEditor stringEditor) {
this.path = path;
this.fileText = fileText;
this.stringEditor = stringEditor;
fileVersion = stringEditor.bufferVersion();
}
public Cursor cursor() {
return stringEditor.cursor();
}
public void reloadFile() {
fileText = path.readUtf8File();
stringEditor.cursor().deleteAll().write(fileText);
fileVersion = stringEditor.bufferVersion();
}
public boolean save() {
Version bufferVersion = stringEditor.bufferVersion();
if (bufferVersion.equals(fileVersion)) {
return false;
}
String newText = stringEditor.cursor().getBufferText();
if (newText.equals(fileText)) {
return false;
}
path.writeUtf8File(newText);
fileVersion = bufferVersion;
return true;
}
}
|