History refactor
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package org.solovyev.android.io;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
abstract class BaseFileLoader {
|
||||
|
||||
@NonNull
|
||||
protected final Context context;
|
||||
|
||||
public BaseFileLoader(@NonNull Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public CharSequence load() {
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
final InputStream is = getInputStream();
|
||||
if (is == null) {
|
||||
return null;
|
||||
}
|
||||
reader = new BufferedReader(new InputStreamReader(is));
|
||||
final StringBuilder result = new StringBuilder();
|
||||
String line = reader.readLine();
|
||||
while (line != null) {
|
||||
result.append(line).append("\n");
|
||||
line = reader.readLine();
|
||||
}
|
||||
return result;
|
||||
} catch (IOException e) {
|
||||
Log.e(getClass().getSimpleName(), e.getMessage(), e);
|
||||
} finally {
|
||||
Io.close(reader);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected abstract InputStream getInputStream() throws IOException;
|
||||
}
|
39
app/src/main/java/org/solovyev/android/io/BaseFileSaver.java
Normal file
39
app/src/main/java/org/solovyev/android/io/BaseFileSaver.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package org.solovyev.android.io;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
|
||||
public abstract class BaseFileSaver implements Runnable {
|
||||
|
||||
@NonNull
|
||||
private final CharSequence data;
|
||||
|
||||
protected BaseFileSaver(@NonNull CharSequence data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void save() {
|
||||
OutputStreamWriter out = null;
|
||||
try {
|
||||
out = new OutputStreamWriter(getOutputStream());
|
||||
out.write(data.toString());
|
||||
} catch (IOException e) {
|
||||
Log.e("FileSaver", e.getMessage(), e);
|
||||
} finally {
|
||||
Io.close(out);
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
protected abstract FileOutputStream getOutputStream() throws FileNotFoundException;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
save();
|
||||
}
|
||||
}
|
28
app/src/main/java/org/solovyev/android/io/FileSaver.java
Normal file
28
app/src/main/java/org/solovyev/android/io/FileSaver.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package org.solovyev.android.io;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
public class FileSaver extends BaseFileSaver {
|
||||
|
||||
@NonNull
|
||||
private final File file;
|
||||
|
||||
private FileSaver(@NonNull File file, @NonNull CharSequence data) {
|
||||
super(data);
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public static void save(@NonNull File file, @NonNull CharSequence data) {
|
||||
final FileSaver fileSaver = new FileSaver(file, data);
|
||||
fileSaver.save();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
protected FileOutputStream getOutputStream() throws FileNotFoundException {
|
||||
return new FileOutputStream(file);
|
||||
}
|
||||
}
|
22
app/src/main/java/org/solovyev/android/io/Io.java
Normal file
22
app/src/main/java/org/solovyev/android/io/Io.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package org.solovyev.android.io;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
||||
public final class Io {
|
||||
|
||||
private Io() {
|
||||
}
|
||||
|
||||
public static void close(Closeable closeable) {
|
||||
try {
|
||||
if (closeable != null) {
|
||||
closeable.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.e("Io", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user