refactoring
This commit is contained in:
parent
b03905c0f3
commit
889a4eb772
|
@ -1,8 +1,5 @@
|
|||
package com.rusefi;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* (c) Andrey Belomutskiy
|
||||
* 3/6/2015
|
||||
|
@ -13,7 +10,7 @@ public class ConfigurationImage {
|
|||
* 1) as a header while saving configuration to a binary file
|
||||
* 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[];
|
||||
|
||||
public ConfigurationImage(int size) {
|
||||
|
@ -28,28 +25,6 @@ public class ConfigurationImage {
|
|||
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) {
|
||||
if (rom.length < BIN_HEADER.length())
|
||||
return null;
|
||||
|
@ -69,9 +44,9 @@ public class ConfigurationImage {
|
|||
return new ConfigurationImage(copy);
|
||||
}
|
||||
|
||||
public byte[] getRange(Integer first, int size) {
|
||||
public byte[] getRange(int offset, int size) {
|
||||
byte[] r = new byte[size];
|
||||
System.arraycopy(content, first, r, 0, size);
|
||||
System.arraycopy(content, offset, r, 0, size);
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,24 +1,33 @@
|
|||
package com.rusefi.io;
|
||||
|
||||
import com.rusefi.ConfigurationImage;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Utility class to read/write {@link ConfigurationImage} into a file
|
||||
*
|
||||
* (c) Andrey Belomutskiy
|
||||
* 6/20/2015.
|
||||
*/
|
||||
public class ConfigurationImageFile {
|
||||
private ConfigurationImageFile() {
|
||||
}
|
||||
|
||||
public static ConfigurationImage readFromFile(String fileName) throws IOException {
|
||||
File file = new File(fileName);
|
||||
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();
|
||||
|
||||
FileInputStream fis = new FileInputStream(fileName);
|
||||
byte[] header = new byte[ConfigurationImage.BIN_HEADER.length()];
|
||||
int result = fis.read(header);
|
||||
if (result != header.length)
|
||||
|
@ -29,4 +38,26 @@ public class ConfigurationImageFile {
|
|||
result = fis.read(image.getContent());
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.rusefi;
|
|||
import com.romraider.editor.ecu.ECUEditor;
|
||||
import com.rusefi.binaryprotocol.BinaryProtocol;
|
||||
import com.rusefi.binaryprotocol.BinaryProtocolCmd;
|
||||
import com.rusefi.io.ConfigurationImageFile;
|
||||
import com.rusefi.ui.util.UiUtils;
|
||||
|
||||
import javax.swing.*;
|
||||
|
@ -33,7 +34,7 @@ public class TableEditorPane extends JPanel {
|
|||
if (instance == null)
|
||||
throw new NullPointerException("instance");
|
||||
ConfigurationImage image = instance.getController();
|
||||
byte[] fileContent = image.getFileContent();
|
||||
byte[] fileContent = ConfigurationImageFile.getFileContent(image);
|
||||
ECUEditor.openImage(fileContent);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.rusefi.binaryprotocol;
|
|||
|
||||
import com.romraider.editor.ecu.ECUEditor;
|
||||
import com.rusefi.*;
|
||||
import com.rusefi.io.ConfigurationImageFile;
|
||||
import com.rusefi.io.serial.PortHolder;
|
||||
import jssc.SerialPort;
|
||||
|
||||
|
@ -48,7 +49,7 @@ public class BinaryProtocolCmd {
|
|||
if (!checkForDefinitionFile())
|
||||
return;
|
||||
RomRaiderWrapper.startRomRaider();
|
||||
ECUEditor.openImage(image.getFileContent());
|
||||
ECUEditor.openImage(ConfigurationImageFile.getFileContent(image));
|
||||
}
|
||||
|
||||
public static boolean checkForDefinitionFile() {
|
||||
|
|
Loading…
Reference in New Issue