-
Notifications
You must be signed in to change notification settings - Fork 6
/
FileStorage.java
137 lines (118 loc) · 3.84 KB
/
FileStorage.java
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package io.split.android.client.storage;
import android.content.Context;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import io.split.android.client.utils.Logger;
/**
* Created by guillermo on 11/22/17.
*/
public class FileStorage implements IStorage {
private final File _dataFolder;
public FileStorage(@NotNull File rootFolder, @NotNull String folderName) {
_dataFolder = new File(rootFolder, folderName);
if(!_dataFolder.exists()) {
_dataFolder.mkdir();
}
}
/**
* read the file content returning it as String. Could return null if file not found or could not be opened
* @param elementId Identifier for the element to be read
* @return String | null
* @throws IOException
*/
@Override
public String read(String elementId) throws IOException {
File file = new File(_dataFolder, elementId);
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
Logger.d(e.getMessage());
return null;
}
StringBuilder fileContent = new StringBuilder("");
byte[] buffer = new byte[1024];
int n;
try {
while ((n = fileInputStream.read(buffer)) != -1) {
fileContent.append(new String(buffer, 0, n));
}
return fileContent.toString();
} catch (IOException e) {
Logger.e(e, "Can't read file");
throw e;
}
}
@Override
public boolean write(String elementId, String content) throws IOException {
File file = new File(_dataFolder, elementId);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(content.getBytes());
} catch (FileNotFoundException e) {
Logger.e(e, "Failed to write content");
throw e;
} catch (IOException e) {
Logger.e(e, "Failed to write content");
throw e;
} finally {
try {
if(fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
Logger.e(e, "Failed to close file");
}
}
return true;
}
@Override
public void delete(String elementId) {
File fileToDelete = new File(_dataFolder, elementId);
fileToDelete.delete();
}
@Override
public String[] getAllIds() {
File dataFolder = new File(_dataFolder, ".");
File[] fileList = dataFolder.listFiles();
if(fileList == null) {
return new String[0];
}
String[] nameList = new String[fileList.length];
int i = 0;
for(File file : fileList) {
nameList[i] = file.getName();
i++;
}
return nameList;
}
@Override
public List<String> getAllIds(String fileNamePrefix) {
List<String> fileNames = new ArrayList<>();
String[] fileIds = getAllIds();
for(String fileName : fileIds) {
if(fileName.startsWith(fileNamePrefix)){
fileNames.add(fileName);
}
}
return fileNames;
}
@Override
public boolean rename(String currentId, String newId) {
File oldFile = new File(_dataFolder, currentId);
File newFile = new File(_dataFolder, newId);
return oldFile.renameTo(newFile);
}
@Override
public boolean exists(String elementId) {
File file = new File(_dataFolder, elementId);
return file.exists();
}
}