auto-sync

This commit is contained in:
rusEfi 2015-03-07 15:04:54 -06:00
parent e093061b24
commit b76a8ac551
3 changed files with 96 additions and 3 deletions

View File

@ -1,7 +1,9 @@
package com.rusefi;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
/**
* (c) Andrey Belomutskiy
@ -9,16 +11,19 @@ import java.io.IOException;
*/
public class ConfigurationImage {
private final static String BIN_HEADER = "RUSEFI0.1";
private final int size;
private byte content[];
public ConfigurationImage(int size) {
this.size = size;
content = new byte[size];
}
public ConfigurationImage(byte[] content) {
this.content = content;
}
public int getSize() {
return size;
return content.length;
}
public void saveToFile(String fileName) throws IOException {
@ -32,6 +37,18 @@ public class ConfigurationImage {
System.out.println("Saved to " + fileName);
}
public boolean readFromFile(String fileName) throws IOException {
FileInputStream fis = new FileInputStream(fileName);
byte[] header = new byte[BIN_HEADER.length()];
int result = fis.read(header);
if (result != header.length)
return false;
if (!Arrays.equals(header, BIN_HEADER.getBytes()))
return false;
result = fis.read(content);
return result == content.length;
}
public byte[] getContent() {
return content;
}

View File

@ -0,0 +1,34 @@
package com.rusefi;
import com.rusefi.core.Pair;
/**
* (c) Andrey Belomutskiy
* 3/6/2015
*/
public class ConfigurationImageDiff {
public static Pair<Integer, Integer> findDifferences(ConfigurationImage image1, ConfigurationImage image2, int position) {
byte[] c1 = image1.getContent();
byte[] c2 = image2.getContent();
int length = c1.length;
if (length != c2.length)
throw new IllegalArgumentException("Length mismatch");
while (position < length && c1[position] == c2[position])
position++;
if (position == length)
return null; // no difference
int startOfDiff = position;
int prevDiff = position;
while (position - prevDiff < 4 && position < length) {
if (c1[position] != c2[position]) {
prevDiff = position;
}
position++;
}
return new Pair<>(startOfDiff, position);
}
}

View File

@ -0,0 +1,42 @@
package com.rusefi.test;
import com.rusefi.ConfigurationImage;
import com.rusefi.core.Pair;
import org.junit.Test;
import static com.rusefi.ConfigurationImageDiff.findDifferences;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* (c) Andrey Belomutskiy
* 3/6/2015
*/
public class ConfigurationImageDiffTest {
@Test
public void testCompare() {
{
byte[] data1 = {1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6};
byte[] data2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 1, 2, 3, 4, 5, 6};
Pair<Integer, Integer> p = findDifferences(new ConfigurationImage(data1), new ConfigurationImage(data2), 0);
assertNotNull(p);
assertEquals(5, (int) p.first);
assertEquals(15, (int) p.second);
}
{
byte[] data1 = {1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6};
byte[] data2 = {1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6};
Pair<Integer, Integer> p = findDifferences(new ConfigurationImage(data1), new ConfigurationImage(data2), 0);
assertNotNull(p);
assertEquals(5, (int) p.first);
assertEquals(9, (int) p.second);
}
{
byte[] data1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 1, 0, 0, 0, 0, 0};
byte[] data2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 1, 2, 3, 4, 5, 6};
Pair<Integer, Integer> p = findDifferences(new ConfigurationImage(data1), new ConfigurationImage(data2), 0);
assertEquals(13, (int) p.first);
assertEquals(18, (int) p.second);
}
}
}