refactoring

This commit is contained in:
rusefi 2017-03-01 14:55:40 -05:00
parent b03905c0f3
commit 889a4eb772
4 changed files with 42 additions and 34 deletions

View File

@ -1,8 +1,5 @@
package com.rusefi; package com.rusefi;
import java.io.*;
import java.util.Arrays;
/** /**
* (c) Andrey Belomutskiy * (c) Andrey Belomutskiy
* 3/6/2015 * 3/6/2015
@ -13,7 +10,7 @@ public class ConfigurationImage {
* 1) as a header while saving configuration to a binary file * 1) as a header while saving configuration to a binary file
* 2) as RomRaider RomID#internalIdString * 2) as RomRaider RomID#internalIdString
*/ */
public final static String BIN_HEADER = "RUSEFI0.1"; public final static String BIN_HEADER = "OPEN_SR5_0.1";
private byte content[]; private byte content[];
public ConfigurationImage(int size) { public ConfigurationImage(int size) {
@ -28,28 +25,6 @@ public class ConfigurationImage {
return content.length; return content.length;
} }
public byte[] getFileContent() {
try {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
byte[] bytes = BIN_HEADER.getBytes();
if (bytes.length != BIN_HEADER.length())
throw new IllegalStateException("Encoding issue");
baos.write(bytes);
baos.write(content);
return baos.toByteArray();
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
public void saveToFile(String fileName) throws IOException {
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(getFileContent());
fos.close();
System.out.println("Saved to " + fileName);
}
public static byte[] extractContent(byte[] rom) { public static byte[] extractContent(byte[] rom) {
if (rom.length < BIN_HEADER.length()) if (rom.length < BIN_HEADER.length())
return null; return null;
@ -69,9 +44,9 @@ public class ConfigurationImage {
return new ConfigurationImage(copy); return new ConfigurationImage(copy);
} }
public byte[] getRange(Integer first, int size) { public byte[] getRange(int offset, int size) {
byte[] r = new byte[size]; byte[] r = new byte[size];
System.arraycopy(content, first, r, 0, size); System.arraycopy(content, offset, r, 0, size);
return r; return r;
} }
} }

View File

@ -1,24 +1,33 @@
package com.rusefi.io; package com.rusefi.io;
import com.rusefi.ConfigurationImage; import com.rusefi.ConfigurationImage;
import org.jetbrains.annotations.Nullable;
import java.io.File; import java.io.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
/** /**
* Utility class to read/write {@link ConfigurationImage} into a file
*
* (c) Andrey Belomutskiy * (c) Andrey Belomutskiy
* 6/20/2015. * 6/20/2015.
*/ */
public class ConfigurationImageFile { public class ConfigurationImageFile {
private ConfigurationImageFile() {
}
public static ConfigurationImage readFromFile(String fileName) throws IOException { public static ConfigurationImage readFromFile(String fileName) throws IOException {
File file = new File(fileName); File file = new File(fileName);
int size = (int) file.length(); int size = (int) file.length();
FileInputStream fis = new FileInputStream(fileName);
return readFromStream(size, fis);
}
@Nullable
private static ConfigurationImage readFromStream(int size, FileInputStream fis) throws IOException {
int contentSize = size - ConfigurationImage.BIN_HEADER.length(); int contentSize = size - ConfigurationImage.BIN_HEADER.length();
FileInputStream fis = new FileInputStream(fileName);
byte[] header = new byte[ConfigurationImage.BIN_HEADER.length()]; byte[] header = new byte[ConfigurationImage.BIN_HEADER.length()];
int result = fis.read(header); int result = fis.read(header);
if (result != header.length) if (result != header.length)
@ -29,4 +38,26 @@ public class ConfigurationImageFile {
result = fis.read(image.getContent()); result = fis.read(image.getContent());
return result == image.getContent().length ? image : null; return result == image.getContent().length ? image : null;
} }
public static byte[] getFileContent(ConfigurationImage configurationImage) {
try {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
byte[] bytes = ConfigurationImage.BIN_HEADER.getBytes();
if (bytes.length != ConfigurationImage.BIN_HEADER.length())
throw new IllegalStateException("Encoding issue");
baos.write(bytes);
baos.write(configurationImage.getContent());
return baos.toByteArray();
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
public static void saveToFile(ConfigurationImage configurationImage, String fileName) throws IOException {
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(getFileContent(configurationImage));
fos.close();
System.out.println("Saved to " + fileName);
}
} }

View File

@ -3,6 +3,7 @@ package com.rusefi;
import com.romraider.editor.ecu.ECUEditor; import com.romraider.editor.ecu.ECUEditor;
import com.rusefi.binaryprotocol.BinaryProtocol; import com.rusefi.binaryprotocol.BinaryProtocol;
import com.rusefi.binaryprotocol.BinaryProtocolCmd; import com.rusefi.binaryprotocol.BinaryProtocolCmd;
import com.rusefi.io.ConfigurationImageFile;
import com.rusefi.ui.util.UiUtils; import com.rusefi.ui.util.UiUtils;
import javax.swing.*; import javax.swing.*;
@ -33,7 +34,7 @@ public class TableEditorPane extends JPanel {
if (instance == null) if (instance == null)
throw new NullPointerException("instance"); throw new NullPointerException("instance");
ConfigurationImage image = instance.getController(); ConfigurationImage image = instance.getController();
byte[] fileContent = image.getFileContent(); byte[] fileContent = ConfigurationImageFile.getFileContent(image);
ECUEditor.openImage(fileContent); ECUEditor.openImage(fileContent);
} catch (Exception e) { } catch (Exception e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);

View File

@ -2,6 +2,7 @@ package com.rusefi.binaryprotocol;
import com.romraider.editor.ecu.ECUEditor; import com.romraider.editor.ecu.ECUEditor;
import com.rusefi.*; import com.rusefi.*;
import com.rusefi.io.ConfigurationImageFile;
import com.rusefi.io.serial.PortHolder; import com.rusefi.io.serial.PortHolder;
import jssc.SerialPort; import jssc.SerialPort;
@ -48,7 +49,7 @@ public class BinaryProtocolCmd {
if (!checkForDefinitionFile()) if (!checkForDefinitionFile())
return; return;
RomRaiderWrapper.startRomRaider(); RomRaiderWrapper.startRomRaider();
ECUEditor.openImage(image.getFileContent()); ECUEditor.openImage(ConfigurationImageFile.getFileContent(image));
} }
public static boolean checkForDefinitionFile() { public static boolean checkForDefinitionFile() {