From bd1ac1e94b4aeed09091309d20eb116e267faca7 Mon Sep 17 00:00:00 2001 From: rusEfi Date: Sat, 7 Mar 2015 15:04:54 -0600 Subject: [PATCH] auto-sync --- .../io/src/com/rusefi/ConfigurationImage.java | 23 ++++++++-- .../com/rusefi/ConfigurationImageDiff.java | 34 +++++++++++++++ .../test/ConfigurationImageDiffTest.java | 42 +++++++++++++++++++ 3 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 java_console/io/src/com/rusefi/ConfigurationImageDiff.java create mode 100644 java_console/io/src/com/rusefi/test/ConfigurationImageDiffTest.java diff --git a/java_console/io/src/com/rusefi/ConfigurationImage.java b/java_console/io/src/com/rusefi/ConfigurationImage.java index cb8fbbe099..53a150afec 100644 --- a/java_console/io/src/com/rusefi/ConfigurationImage.java +++ b/java_console/io/src/com/rusefi/ConfigurationImage.java @@ -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; } diff --git a/java_console/io/src/com/rusefi/ConfigurationImageDiff.java b/java_console/io/src/com/rusefi/ConfigurationImageDiff.java new file mode 100644 index 0000000000..2da71cda65 --- /dev/null +++ b/java_console/io/src/com/rusefi/ConfigurationImageDiff.java @@ -0,0 +1,34 @@ +package com.rusefi; + +import com.rusefi.core.Pair; + +/** + * (c) Andrey Belomutskiy + * 3/6/2015 + */ +public class ConfigurationImageDiff { + public static Pair 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); + } +} diff --git a/java_console/io/src/com/rusefi/test/ConfigurationImageDiffTest.java b/java_console/io/src/com/rusefi/test/ConfigurationImageDiffTest.java new file mode 100644 index 0000000000..335a9dbb93 --- /dev/null +++ b/java_console/io/src/com/rusefi/test/ConfigurationImageDiffTest.java @@ -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 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 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 p = findDifferences(new ConfigurationImage(data1), new ConfigurationImage(data2), 0); + assertEquals(13, (int) p.first); + assertEquals(18, (int) p.second); + } + } +}