[wpiutil] Enhance DataLog Java raw value support

Add support for start, length, and ByteBuffers
This commit is contained in:
Peter Johnson
2023-05-17 14:06:52 -07:00
parent 8dae5af271
commit 3a6e40a44b
6 changed files with 309 additions and 25 deletions

View File

@@ -4,6 +4,8 @@
package edu.wpi.first.util.datalog;
import java.nio.ByteBuffer;
/**
* A data log. The log file is created immediately upon construction with a temporary filename. The
* file may be renamed at any time using the setFilename() function.
@@ -191,11 +193,49 @@ public final class DataLog implements AutoCloseable {
* Appends a raw record to the log.
*
* @param entry Entry index, as returned by start()
* @param data Byte array to record
* @param data Byte array to record; will send entire array contents
* @param timestamp Time stamp (0 to indicate now)
*/
public void appendRaw(int entry, byte[] data, long timestamp) {
DataLogJNI.appendRaw(m_impl, entry, data, timestamp);
appendRaw(entry, data, 0, data.length, timestamp);
}
/**
* Appends a record to the log.
*
* @param entry Entry index, as returned by start()
* @param data Byte array to record
* @param start Start position of data (in byte array)
* @param len Length of data (must be less than or equal to data.length - start)
* @param timestamp Time stamp (0 to indicate now)
*/
public void appendRaw(int entry, byte[] data, int start, int len, long timestamp) {
DataLogJNI.appendRaw(m_impl, entry, data, start, len, timestamp);
}
/**
* Appends a record to the log.
*
* @param entry Entry index, as returned by start()
* @param data Buffer to record; will send from data.position() to data.limit()
* @param timestamp Time stamp (0 to indicate now)
*/
public void appendRaw(int entry, ByteBuffer data, long timestamp) {
int pos = data.position();
appendRaw(entry, data, pos, data.limit() - pos, timestamp);
}
/**
* Appends a record to the log.
*
* @param entry Entry index, as returned by start()
* @param data Buffer to record
* @param start Start position of data (in buffer)
* @param len Length of data (must be less than or equal to data.capacity() - start)
* @param timestamp Time stamp (0 to indicate now)
*/
public void appendRaw(int entry, ByteBuffer data, int start, int len, long timestamp) {
DataLogJNI.appendRaw(m_impl, entry, data, start, len, timestamp);
}
@Override

View File

@@ -5,6 +5,7 @@
package edu.wpi.first.util.datalog;
import edu.wpi.first.util.WPIUtilJNI;
import java.nio.ByteBuffer;
public class DataLogJNI extends WPIUtilJNI {
static native long create(String dir, String filename, double period, String extraHeader);
@@ -25,7 +26,30 @@ public class DataLogJNI extends WPIUtilJNI {
static native void close(long impl);
static native void appendRaw(long impl, int entry, byte[] data, long timestamp);
static native void appendRaw(
long impl, int entry, byte[] data, int start, int len, long timestamp);
static void appendRaw(long impl, int entry, ByteBuffer data, int start, int len, long timestamp) {
if (data.isDirect()) {
if (start < 0) {
throw new IndexOutOfBoundsException("start must be >= 0");
}
if (len < 0) {
throw new IndexOutOfBoundsException("len must be >= 0");
}
if ((start + len) > data.capacity()) {
throw new IndexOutOfBoundsException("start + len must be smaller than buffer capacity");
}
appendRawBuffer(impl, entry, data, start, len, timestamp);
} else if (data.hasArray()) {
appendRaw(impl, entry, data.array(), data.arrayOffset() + start, len, timestamp);
} else {
throw new UnsupportedOperationException("ByteBuffer must be direct or have a backing array");
}
}
private static native void appendRawBuffer(
long impl, int entry, ByteBuffer data, int start, int len, long timestamp);
static native void appendBoolean(long impl, int entry, boolean value, long timestamp);

View File

@@ -4,6 +4,8 @@
package edu.wpi.first.util.datalog;
import java.nio.ByteBuffer;
/** Log raw byte array values. */
public class RawLogEntry extends DataLogEntry {
public static final String kDataType = "raw";
@@ -35,7 +37,7 @@ public class RawLogEntry extends DataLogEntry {
/**
* Appends a record to the log.
*
* @param value Value to record
* @param value Value to record; will send entire array contents
* @param timestamp Time stamp (0 to indicate now)
*/
public void append(byte[] value, long timestamp) {
@@ -45,9 +47,74 @@ public class RawLogEntry extends DataLogEntry {
/**
* Appends a record to the log.
*
* @param value Value to record
* @param value Value to record; will send entire array contents
*/
public void append(byte[] value) {
m_log.appendRaw(m_entry, value, 0);
append(value, 0);
}
/**
* Appends a record to the log.
*
* @param value Data to record
* @param start Start position of data (in byte array)
* @param len Length of data (must be less than or equal to value.length - offset)
* @param timestamp Time stamp (0 to indicate now)
*/
public void append(byte[] value, int start, int len, long timestamp) {
m_log.appendRaw(m_entry, value, start, len, timestamp);
}
/**
* Appends a record to the log.
*
* @param value Data to record
* @param start Start position of data (in byte array)
* @param len Length of data (must be less than or equal to value.length - offset)
*/
public void append(byte[] value, int start, int len) {
append(value, start, len, 0);
}
/**
* Appends a record to the log.
*
* @param value Data to record; will send from value.position() to value.capacity()
* @param timestamp Time stamp (0 to indicate now)
*/
public void append(ByteBuffer value, long timestamp) {
m_log.appendRaw(m_entry, value, timestamp);
}
/**
* Appends a record to the log.
*
* @param value Data to record; will send from value.position() to value.capacity()
*/
public void append(ByteBuffer value) {
append(value, 0);
}
/**
* Appends a record to the log.
*
* @param value Data to record
* @param start Start position of data (in value buffer)
* @param len Length of data (must be less than or equal to value.length - offset)
* @param timestamp Time stamp (0 to indicate now)
*/
public void append(ByteBuffer value, int start, int len, long timestamp) {
m_log.appendRaw(m_entry, value, start, len, timestamp);
}
/**
* Appends a record to the log.
*
* @param value Data to record
* @param start Start position of data (in value buffer)
* @param len Length of data (must be less than or equal to value.length - offset)
*/
public void append(ByteBuffer value, int start, int len) {
append(value, start, len, 0);
}
}