Search in sources :

Example 1 with ISafeByteBufferReader

use of org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferReader in project tracecompass by tracecompass.

the class CustomStateValueTest method testNoFactoryAvailable.

/**
 * Test serialization/unserialization if no factory is available
 */
@Test
public void testNoFactoryAvailable() {
    CustomStateValueStub.unregisterFactory();
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    ISafeByteBufferWriter safeBufferWriter = SafeByteBufferFactory.wrapWriter(buffer, VALUE.getSerializedSize());
    VALUE.serialize(safeBufferWriter);
    // Reset buffer
    buffer.flip();
    ISafeByteBufferReader safeBufferReader = SafeByteBufferFactory.wrapReader(buffer, VALUE.getSerializedSize());
    TmfStateValue read = CustomStateValue.readSerializedValue(safeBufferReader);
    assertEquals("Custom state value: no factory", TmfStateValue.nullValue(), read);
}
Also used : ITmfStateValue(org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue) TmfStateValue(org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue) ByteBuffer(java.nio.ByteBuffer) ISafeByteBufferReader(org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferReader) ISafeByteBufferWriter(org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferWriter) Test(org.junit.Test)

Example 2 with ISafeByteBufferReader

use of org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferReader in project tracecompass by tracecompass.

the class CustomStateValueTest method testSerialization.

/**
 * Test serialization/unserialization with the factory present
 */
@Test
public void testSerialization() {
    CustomStateValueStub.registerFactory();
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    ISafeByteBufferWriter safeBufferWriter = SafeByteBufferFactory.wrapWriter(buffer, VALUE.getSerializedSize());
    VALUE.serialize(safeBufferWriter);
    // Reset buffer
    buffer.flip();
    ISafeByteBufferReader safeBufferReader = SafeByteBufferFactory.wrapReader(buffer, VALUE.getSerializedSize());
    TmfStateValue read = CustomStateValue.readSerializedValue(safeBufferReader);
    assertEquals("Custom state value: no factory", VALUE, read);
}
Also used : ITmfStateValue(org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue) TmfStateValue(org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue) ByteBuffer(java.nio.ByteBuffer) ISafeByteBufferReader(org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferReader) ISafeByteBufferWriter(org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferWriter) Test(org.junit.Test)

Example 3 with ISafeByteBufferReader

use of org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferReader in project tracecompass by tracecompass.

the class HTInterval method readFrom.

/**
 * Reader factory method. Builds the interval using an already-allocated
 * ByteBuffer, which normally comes from a NIO FileChannel.
 *
 * The interval is just a start, end, attribute and value, this is the
 * layout of the HTInterval on disk
 * <ul>
 * <li>start (2-9 bytes)</li>
 * <li>end (2-9 bytes)</li>
 * <li>attribute (4 bytes)</li>
 * <li>sv type (1 byte)</li>
 * <li>sv ( 0 bytes for null, 4 for int , 8 for long and double, and the
 * length of the string +2 for strings (it's variable))</li>
 * </ul>
 *
 * @param buffer
 *            The ByteBuffer from which to read the information
 * @param nodeStart
 *             The start time of the node this interval is linked to
 * @return The interval object
 * @throws IOException
 *             If there was an error reading from the buffer
 */
public static final HTInterval readFrom(ByteBuffer buffer, long nodeStart) throws IOException {
    Object value;
    int posStart = buffer.position();
    /* Read the Data Section entry */
    long intervalStart = HTVarInt.readLong(buffer) + nodeStart;
    long intervalEnd = HTVarInt.readLong(buffer) + intervalStart;
    int attribute = buffer.getInt();
    /* Read the 'type' of the value, then react accordingly */
    byte valueType = buffer.get();
    switch(valueType) {
        case TYPE_NULL:
            value = null;
            break;
        case TYPE_INTEGER:
            value = buffer.getInt();
            break;
        case TYPE_STRING:
            {
                /* the first short = the size to read */
                int valueSize = buffer.getShort();
                byte[] array = new byte[valueSize];
                buffer.get(array);
                value = new String(array, CHARSET);
                /* Confirm the 0'ed byte at the end */
                byte res = buffer.get();
                if (res != 0) {
                    throw new IOException(errMsg);
                }
                break;
            }
        case TYPE_LONG:
            /* Go read the matching entry in the Strings section of the block */
            value = buffer.getLong();
            break;
        case TYPE_DOUBLE:
            /* Go read the matching entry in the Strings section of the block */
            value = buffer.getDouble();
            break;
        case TYPE_CUSTOM:
            {
                short valueSize = buffer.getShort();
                ISafeByteBufferReader safeBuffer = SafeByteBufferFactory.wrapReader(buffer, valueSize);
                value = CustomStateValue.readSerializedValue(safeBuffer);
                break;
            }
        default:
            /* Unknown data, better to not make anything up... */
            throw new IOException(errMsg);
    }
    try {
        return new HTInterval(intervalStart, intervalEnd, attribute, value, buffer.position() - posStart);
    } catch (TimeRangeException e) {
        throw new IOException(errMsg);
    }
}
Also used : TimeRangeException(org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException) IOException(java.io.IOException) ISafeByteBufferReader(org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferReader)

Example 4 with ISafeByteBufferReader

use of org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferReader in project tracecompass by tracecompass.

the class TmfIntervalDeserializer method deserialize.

@Override
public ITmfStateInterval deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
    JsonObject object = json.getAsJsonObject();
    long start = object.get(TmfIntervalStrings.START).getAsLong();
    long end = object.get(TmfIntervalStrings.END).getAsLong();
    int quark = object.get(TmfIntervalStrings.QUARK).getAsInt();
    String type = object.get(TmfIntervalStrings.TYPE).getAsString();
    if (type.equals(TmfIntervalStrings.NULL)) {
        return new TmfStateInterval(start, end, quark, (Object) null);
    }
    JsonElement value = object.get(TmfIntervalStrings.VALUE);
    try {
        Class<?> typeClass = Class.forName(type);
        if (typeClass.isAssignableFrom(CustomStateValue.class)) {
            String encoded = value.getAsString();
            byte[] serialized = Base64.getDecoder().decode(encoded);
            ByteBuffer buffer = ByteBuffer.wrap(serialized);
            ISafeByteBufferReader sbbr = SafeByteBufferFactory.wrapReader(buffer, serialized.length);
            TmfStateValue sv = CustomStateValue.readSerializedValue(sbbr);
            return new TmfStateInterval(start, end, quark, sv.unboxValue());
        }
        if (typeClass.isAssignableFrom(Integer.class)) {
            return new TmfStateInterval(start, end, quark, value.getAsInt());
        } else if (typeClass.isAssignableFrom(Long.class)) {
            return new TmfStateInterval(start, end, quark, value.getAsLong());
        } else if (typeClass.isAssignableFrom(Double.class)) {
            return new TmfStateInterval(start, end, quark, value.getAsDouble());
        } else if (typeClass.isAssignableFrom(String.class)) {
            return new TmfStateInterval(start, end, quark, value.getAsString());
        }
    } catch (ClassNotFoundException e) {
    // Fall through
    }
    // last ditch attempt
    return new TmfStateInterval(start, end, quark, value.toString());
}
Also used : ITmfStateInterval(org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval) TmfStateInterval(org.eclipse.tracecompass.statesystem.core.interval.TmfStateInterval) JsonObject(com.google.gson.JsonObject) ByteBuffer(java.nio.ByteBuffer) ISafeByteBufferReader(org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferReader) JsonElement(com.google.gson.JsonElement) TmfStateValue(org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue)

Example 5 with ISafeByteBufferReader

use of org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferReader in project tracecompass by tracecompass.

the class SafeByteBufferWrapperTest method testReadWriteChar.

/**
 * Test the {@link SafeByteBufferWrapper#putChar(char)}
 * {@link SafeByteBufferWrapper#getChar()} methods
 */
@Test
public void testReadWriteChar() {
    char val = 24;
    ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
    buffer.putChar(val);
    // Reset the buffer and read it again
    fMainBuffer.flip();
    ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
    assertEquals(val, reader.getChar());
}
Also used : ISafeByteBufferReader(org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferReader) ISafeByteBufferWriter(org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferWriter) Test(org.junit.Test)

Aggregations

ISafeByteBufferReader (org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferReader)19 ISafeByteBufferWriter (org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferWriter)16 Test (org.junit.Test)16 ByteBuffer (java.nio.ByteBuffer)5 TmfStateValue (org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue)3 IOException (java.io.IOException)2 ITmfStateValue (org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 ByteOrder (java.nio.ByteOrder)1 TimeRangeException (org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException)1 ITmfStateInterval (org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval)1 TmfStateInterval (org.eclipse.tracecompass.statesystem.core.interval.TmfStateInterval)1