Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.ironz.binaryprefs.file.directory.DirectoryProvider;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
Expand All @@ -16,7 +17,7 @@
* If adapter detects backup file it will be replaced
* to original file. See {@link #fetchBackupOrOriginal(String)}.
*/
public final class NioFileAdapter implements FileAdapter {
public class NioFileAdapter implements FileAdapter {

private static final String ZERO_BYTES_MESSAGE = "%s key's value is zero bytes for saving";

Expand Down Expand Up @@ -56,10 +57,18 @@ private byte[] fetchBackupOrOriginal(String name) {
File backupFile = new File(backupDir, name + BACKUP_EXTENSION);
File file = new File(baseDir, name);
if (backupFile.exists()) {
restoreBackup(file, backupFile);
}
return fetchInternal(file);
}

private void restoreBackup(File file, File backupFile) {
if (backupFile.length() == 0) {
delete(file);
delete(backupFile);
} else {
swap(backupFile, file);
}
return fetchInternal(file);
}

private byte[] fetchInternal(File file) {
Expand Down Expand Up @@ -99,12 +108,14 @@ private void backupAndSave(String name, byte[] bytes) {
}
File file = new File(baseDir, name);
File backupFile = new File(backupDir, name + BACKUP_EXTENSION);

ensureExists(file);
swap(file, backupFile);
saveInternal(file, bytes);
delete(backupFile);
}

private void saveInternal(File file, byte[] bytes) {
void saveInternal(File file, byte[] bytes) {
FileChannel channel = null;
RandomAccessFile randomAccessFile = null;
try {
Expand All @@ -130,6 +141,17 @@ private void saveInternal(File file, byte[] bytes) {
}
}

private void ensureExists(File file) {
if (!file.exists()) {
try {
//noinspection ResultOfMethodCallIgnored
file.createNewFile();
} catch (IOException e) {
throw new FileOperationException(e);
}
}
}

private void swap(File from, File to) {
if (!from.exists()) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.ironz.binaryprefs.file;
package com.ironz.binaryprefs.file.adapter;

import com.ironz.binaryprefs.exception.FileOperationException;
import com.ironz.binaryprefs.file.adapter.FileAdapter;
import com.ironz.binaryprefs.file.adapter.NioFileAdapter;
import com.ironz.binaryprefs.file.directory.DirectoryProvider;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -26,6 +24,7 @@ public final class NioFileAdapterTest {
public final TemporaryFolder folder = new TemporaryFolder();

private FileAdapter fileAdapter;
private FileAdapter failingAdapter;

@Before
public void setUp() throws Exception {
Expand All @@ -49,6 +48,14 @@ public File getLockDirectory() {
}
};
fileAdapter = new NioFileAdapter(directoryProvider);
failingAdapter = new NioFileAdapter(directoryProvider) {
@Override
void saveInternal(File file, byte[] bytes) {
byte[] successBytes = Arrays.copyOf(bytes, 1);
super.saveInternal(file, successBytes);
throw new IllegalStateException();
}
};
}

@Test
Expand Down Expand Up @@ -114,4 +121,26 @@ public void deleteAll() {
assertNotNull(fileAdapter.fetch(FILE_NAME));
fileAdapter.fetch(FILE_NAME_1);
}
}

@Test(expected = FileOperationException.class)
public void fetchNoFile() {
fileAdapter.fetch(FILE_NAME);
}

@Test(expected = IllegalStateException.class)
public void failOnSave() {
failingAdapter.save(FILE_NAME, bytes);
}

@Test(expected = FileOperationException.class)
public void recoverAfterFail() {
try {
failingAdapter.save(FILE_NAME, bytes);
throw new AssertionError();
} catch (IllegalStateException ignored) {
}

fileAdapter.fetch(FILE_NAME);
}

}