diff --git a/numass-core/src/main/java/inr/numass/NumassEnvelopeType.java b/numass-core/src/main/java/inr/numass/NumassEnvelopeType.java new file mode 100644 index 00000000..d0654532 --- /dev/null +++ b/numass-core/src/main/java/inr/numass/NumassEnvelopeType.java @@ -0,0 +1,103 @@ +package inr.numass; + +import hep.dataforge.io.envelopes.*; +import hep.dataforge.values.Value; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.Map; + +import static hep.dataforge.io.envelopes.DefaultEnvelopeType.DEFAULT_ENVELOPE_TYPE; + +/** + * An envelope type for legacy numass tags. Reads legacy tag and writes DF02 tags + */ +public class NumassEnvelopeType implements EnvelopeType { + + @Override + public int getCode() { + return DEFAULT_ENVELOPE_TYPE; + } + + @Override + public String getName() { + return "numass"; + } + + @Override + public String description() { + return "Numass legacy envelope"; + } + + @Override + public EnvelopeReader getReader(Map properties) { + return new NumassEnvelopeReader(); + } + + @Override + public EnvelopeWriter getWriter(Map properties) { + return new DefaultEnvelopeWriter(this, MetaType.resolve(properties)); + } + + private static class LegacyTag extends EnvelopeTag { + private final byte[] START_SEQUENCE = {'#', '!'}; + private final byte[] END_SEQUENCE = {'!', '#', '\r', '\n'}; + + @Override + protected byte[] getStartSequence() { + return START_SEQUENCE; + } + + @Override + protected byte[] getEndSequence() { + return END_SEQUENCE; + } + + /** + * Get the length of tag in bytes. -1 means undefined size in case tag was modified + * + * @return + */ + public int getLength() { + return 30; + } + + /** + * Read leagscy version 1 tag without leading tag head + * + * @param buffer + * @return + * @throws IOException + */ + protected Map readHeader(ByteBuffer buffer) throws IOException { + Map res = new HashMap<>(); + + int type = buffer.getInt(2); + res.put(Envelope.TYPE_KEY, Value.of(type)); + + short metaTypeCode = buffer.getShort(10); + MetaType metaType = MetaType.resolve(metaTypeCode); + + if (metaType != null) { + res.put(Envelope.META_TYPE_KEY, Value.of(metaType.getName())); + } else { + LoggerFactory.getLogger(EnvelopeTag.class).warn("Could not resolve meta type. Using default"); + } + + long metaLength = Integer.toUnsignedLong(buffer.getInt(14)); + res.put(Envelope.META_LENGTH_KEY, Value.of(metaLength)); + long dataLength = Integer.toUnsignedLong(buffer.getInt(22)); + res.put(Envelope.DATA_LENGTH_KEY, Value.of(dataLength)); + return res; + } + } + + private static class NumassEnvelopeReader extends DefaultEnvelopeReader { + @Override + protected EnvelopeTag newTag() { + return new LegacyTag(); + } + } +} diff --git a/numass-core/src/main/resources/META-INF/services/hep.dataforge.io.envelopes.EnvelopeType b/numass-core/src/main/resources/META-INF/services/hep.dataforge.io.envelopes.EnvelopeType new file mode 100644 index 00000000..e38122ce --- /dev/null +++ b/numass-core/src/main/resources/META-INF/services/hep.dataforge.io.envelopes.EnvelopeType @@ -0,0 +1 @@ +inr.numass.NumassEnvelopeType \ No newline at end of file