refactoring: extracting utility method

This commit is contained in:
rusefillc 2022-03-15 13:55:52 -04:00
parent 6c8f130ff9
commit d88a049c9e
4 changed files with 18 additions and 11 deletions

View File

@ -1,9 +1,9 @@
package com.opensr5; package com.opensr5;
import com.rusefi.shared.FileUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/** /**
* Andrey Belomutskiy, (c) 2013-2020 * Andrey Belomutskiy, (c) 2013-2020
@ -28,9 +28,7 @@ public class ConfigurationImage {
@NotNull @NotNull
public ByteBuffer getByteBuffer(int offset, int size) { public ByteBuffer getByteBuffer(int offset, int size) {
ByteBuffer wrapped = ByteBuffer.wrap(content, offset, size); return FileUtil.littleEndianWrap(content, offset, size);
wrapped.order(ByteOrder.LITTLE_ENDIAN);
return wrapped;
} }
public int getSize() { public int getSize() {

View File

@ -2,12 +2,12 @@ package com.rusefi.config;
import com.macfaq.io.LittleEndianOutputStream; import com.macfaq.io.LittleEndianOutputStream;
import com.opensr5.ConfigurationImage; import com.opensr5.ConfigurationImage;
import com.rusefi.shared.FileUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Objects; import java.util.Objects;
import static com.rusefi.config.FieldType.*; import static com.rusefi.config.FieldType.*;
@ -159,8 +159,7 @@ public class Field {
} }
public void setValue(byte[] content, boolean value) { public void setValue(byte[] content, boolean value) {
ByteBuffer wrapped = ByteBuffer.wrap(content, 0, content.length); ByteBuffer wrapped = FileUtil.littleEndianWrap(content, 0, content.length);
wrapped.order(ByteOrder.LITTLE_ENDIAN);
if (bitOffset != NO_BIT_OFFSET) { if (bitOffset != NO_BIT_OFFSET) {
int packed = wrapped.getInt(); int packed = wrapped.getInt();
int thisBit = (value ? 1 : 0) << bitOffset; int thisBit = (value ? 1 : 0) << bitOffset;

View File

@ -1,8 +1,8 @@
package com.rusefi.core; package com.rusefi.core;
import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import static com.rusefi.shared.FileUtil.littleEndianWrap;
public interface ISensorHolder { public interface ISensorHolder {
default void grabSensorValues(byte[] response) { default void grabSensorValues(byte[] response) {
@ -17,8 +17,7 @@ public interface ISensorHolder {
if (offset + size > response.length) { if (offset + size > response.length) {
throw new IllegalArgumentException(sensor + String.format(" but %d+%d in %d", offset, size, response.length)); throw new IllegalArgumentException(sensor + String.format(" but %d+%d in %d", offset, size, response.length));
} }
ByteBuffer bb = ByteBuffer.wrap(response, offset, size); ByteBuffer bb = littleEndianWrap(response, offset, size);
bb.order(ByteOrder.LITTLE_ENDIAN);
double rawValue = sensor.getValueForChannel(bb); double rawValue = sensor.getValueForChannel(bb);
double scaledValue = rawValue * sensor.getScale(); double scaledValue = rawValue * sensor.getScale();

View File

@ -1,6 +1,10 @@
package com.rusefi.shared; package com.rusefi.shared;
import org.jetbrains.annotations.NotNull;
import java.io.*; import java.io.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
@ -66,4 +70,11 @@ public class FileUtil {
} }
} }
} }
@NotNull
public static ByteBuffer littleEndianWrap(byte[] array, int offset, int length) {
ByteBuffer wrapped = ByteBuffer.wrap(array, offset, length);
wrapped.order(ByteOrder.LITTLE_ENDIAN);
return wrapped;
}
} }