X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=statesystem%2Forg.eclipse.tracecompass.statesystem.core%2Fsrc%2Forg%2Feclipse%2Ftracecompass%2Fstatesystem%2Fcore%2Fstatevalue%2FTmfStateValue.java;fp=statesystem%2Forg.eclipse.tracecompass.statesystem.core%2Fsrc%2Forg%2Feclipse%2Ftracecompass%2Fstatesystem%2Fcore%2Fstatevalue%2FTmfStateValue.java;h=e781238a12f18c5a4c6f5802102ddbdf6b92d9b7;hb=ce148788289534e20b56d0790a8ad93ce2e6473d;hp=c724410d21706952447b59424e33a6d8411b5bf1;hpb=6d80d8ef4d1cd1c99cff54c7cbd4c9cf9c971439;p=deliverable%2Ftracecompass.git diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/TmfStateValue.java b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/TmfStateValue.java index c724410d21..e781238a12 100644 --- a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/TmfStateValue.java +++ b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/TmfStateValue.java @@ -12,6 +12,8 @@ package org.eclipse.tracecompass.statesystem.core.statevalue; +import java.nio.ByteBuffer; + import org.eclipse.jdt.annotation.Nullable; import org.eclipse.tracecompass.internal.statesystem.core.Activator; import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; @@ -61,6 +63,55 @@ public abstract class TmfStateValue implements ITmfStateValue { return nullValue; } + /** + * Read a serialized value (obtained with the {@link #serialize()} method) + * into a real {@link TmfStateValue} object. + * + * @param array + * The serialized state value + * @return The state value object + * @since 2.0 + */ + public static TmfStateValue readSerializedValue(byte[] array) { + if (array.length == 0) { + /* This represents a null value */ + return nullValue; + } + + ByteBuffer buffer = ByteBuffer.wrap(array); + + byte typeByte = buffer.get(); + Type type = Type.getTypeFromByte(typeByte); + switch (type) { + case NULL: { + /* Should have been an empty array, but we'll accept it anyway */ + return nullValue; + } + case INTEGER: { + int value = buffer.getInt(); + return newValueInt(value); + } + case LONG: { + long value = buffer.getLong(); + return newValueLong(value); + } + case DOUBLE: { + double value = buffer.getDouble(); + return newValueDouble(value); + } + case STRING: { + /* The remaining of the buffer is the string's bytes */ + int size = array.length - 1; + byte[] strBytes = new byte[size]; + buffer.get(strBytes); + String value = new String(strBytes); + return newValueString(value); + } + default: + throw new IllegalArgumentException(); + } + } + /** * Factory constructor for Integer state values *