/** * Creates a new deserializer string registry. * @param values The values to use */ public DeserializerStringRegistry(final List<String> values) { super(); long key = 0; for (final String value : values) { this.register(key++, value); } }
public String get(final int i) { return this.get(i); }
@Override public List<IMonitoringRecord> deserializeRecords(final ByteBuffer buffer, final int dataSize) { // Check the container identifier final int magic = buffer.getInt(); if (magic != CONTAINER_IDENTIFIER) { throw new InvalidFormatException(String.format("Invalid magic value %08x was found.", magic)); } // Check the actual format identifier final int formatIdentifier = buffer.getInt(); if (formatIdentifier != this.getFormatIdentifier()) { throw new InvalidFormatException(String.format("An invalid format identifier %08x was found.", formatIdentifier)); } return this.decodeRecords(buffer, (dataSize - HEADER_SIZE)); }
@Override protected List<IMonitoringRecord> decodeRecords(final ByteBuffer buffer, final int dataSize) throws InvalidFormatException { final int baseOffset = buffer.position(); // Retrieve the offset of the string table (last four bytes of the data) buffer.position((baseOffset + dataSize) - 4); final int stringTableOffset = buffer.getInt(); // Position the buffer and decode the string table final int absoluteStringTableOffset = baseOffset + stringTableOffset; buffer.position(absoluteStringTableOffset); final IRegistry<String> stringRegistry = this.decodeStringRegistry(buffer); // Position the buffer and decode the records buffer.position(baseOffset); final List<IMonitoringRecord> records = this.decodeMonitoringRecords(buffer, stringRegistry, absoluteStringTableOffset); return records; }
@Override public void decodeAndDeliverRecords(final ByteBuffer rawData, final int dataSize) { final List<IMonitoringRecord> records = this.deserializer.deserializeRecords(rawData, dataSize); // Deliver the records for (final IMonitoringRecord record : records) { this.deliver(OUTPUT_PORT_NAME_RECORDS, record); } }
private IRegistry<String> decodeStringRegistry(final ByteBuffer buffer) { final int numberOfEntries = VariableLengthEncoding.decodeInt(buffer); final List<String> values = new ArrayList<String>(numberOfEntries); for (int entryIndex = 0; entryIndex < numberOfEntries; entryIndex++) { final int entryLength = VariableLengthEncoding.decodeInt(buffer); final byte[] entryDataBytes = new byte[entryLength]; buffer.get(entryDataBytes); final String entryData = new String(entryDataBytes, CHARSET); values.add(entryData); } return new DeserializerStringRegistry(values); }
private List<IMonitoringRecord> decodeMonitoringRecords(final ByteBuffer buffer, final IRegistry<String> stringRegistry, final int endOffset) { final List<IMonitoringRecord> records = new ArrayList<IMonitoringRecord>(); int currentOffset = buffer.position(); final CachedRecordFactoryCatalog recordFactoryCatalog = this.cachedRecordFactoryCatalog; while (currentOffset < endOffset) { final int recordTypeId = buffer.getInt(); final String recordTypeName = stringRegistry.get(recordTypeId); final long loggingTimestamp = buffer.getLong(); final IRecordFactory<? extends IMonitoringRecord> recordFactory = recordFactoryCatalog.get(recordTypeName); final IMonitoringRecord record = recordFactory.create(DefaultValueDeserializer.create(buffer, stringRegistry)); record.setLoggingTimestamp(loggingTimestamp); records.add(record); currentOffset = buffer.position(); } // The record data must end exactly at the given end offset if (currentOffset != endOffset) { throw new InvalidFormatException("Invalid record data found, should have ended at offset " + endOffset + ", but ended at offset " + currentOffset + "."); } return records; }
private List<IMonitoringRecord> decodeRecords(final String dataBase64) { final byte[] rawData = DatatypeConverter.parseBase64Binary(dataBase64); final BinaryDeserializer deserializer = new BinaryDeserializer(null, null); final ByteBuffer rawDataBuffer = ByteBuffer.wrap(rawData); return deserializer.deserializeRecords(rawDataBuffer, rawData.length); }
@Override public List<IMonitoringRecord> deserializeRecords(final ByteBuffer buffer, final int dataSize) { // Check the container identifier final int magic = buffer.getInt(); if (magic != CONTAINER_IDENTIFIER) { throw new InvalidFormatException(String.format("Invalid magic value %08x was found.", magic)); } // Check the actual format identifier final int formatIdentifier = buffer.getInt(); if (formatIdentifier != this.getFormatIdentifier()) { throw new InvalidFormatException( String.format("An invalid format identifier %08x was found.", formatIdentifier)); } return this.decodeRecords(buffer, (dataSize - HEADER_SIZE)); }
@Override protected List<IMonitoringRecord> decodeRecords(final ByteBuffer buffer, final int dataSize) throws InvalidFormatException { final int baseOffset = buffer.position(); // Retrieve the offset of the string table (last four bytes of the data) buffer.position((baseOffset + dataSize) - 4); final int stringTableOffset = buffer.getInt(); // Position the buffer and decode the string table final int absoluteStringTableOffset = baseOffset + stringTableOffset; buffer.position(absoluteStringTableOffset); final ReaderRegistry<String> stringRegistry = this.decodeStringRegistry(buffer); // Position the buffer and decode the records buffer.position(baseOffset); return this.decodeMonitoringRecords(buffer, stringRegistry, absoluteStringTableOffset); }
@Override public void decodeAndDeliverRecords(final ByteBuffer rawData, final int dataSize) { final List<IMonitoringRecord> records = this.deserializer.deserializeRecords(rawData, dataSize); // Deliver the records for (final IMonitoringRecord record : records) { this.deliver(OUTPUT_PORT_NAME_RECORDS, record); } }
private ReaderRegistry<String> decodeStringRegistry(final ByteBuffer buffer) { final int numberOfEntries = VariableLengthEncoding.decodeInt(buffer); final List<String> values = new ArrayList<>(numberOfEntries); for (int entryIndex = 0; entryIndex < numberOfEntries; entryIndex++) { final int entryLength = VariableLengthEncoding.decodeInt(buffer); final byte[] entryDataBytes = new byte[entryLength]; buffer.get(entryDataBytes); final String entryData = new String(entryDataBytes, CHARSET); values.add(entryData); } return new DeserializerStringRegistry(values); }
private List<IMonitoringRecord> decodeMonitoringRecords(final ByteBuffer buffer, final ReaderRegistry<String> stringRegistry, final int endOffset) { final List<IMonitoringRecord> records = new ArrayList<>(); int currentOffset = buffer.position(); final CachedRecordFactoryCatalog recordFactoryCatalog = this.cachedRecordFactoryCatalog; while (currentOffset < endOffset) { final int recordTypeId = buffer.getInt(); final String recordTypeName = stringRegistry.get(recordTypeId); final long loggingTimestamp = buffer.getLong(); final IRecordFactory<? extends IMonitoringRecord> recordFactory = recordFactoryCatalog.get(recordTypeName); final IMonitoringRecord record = recordFactory .create(BinaryValueDeserializer.create(buffer, stringRegistry)); record.setLoggingTimestamp(loggingTimestamp); records.add(record); currentOffset = buffer.position(); } // The record data must end exactly at the given end offset if (currentOffset != endOffset) { throw new InvalidFormatException("Invalid record data found, should have ended at offset " + endOffset + ", but ended at offset " + currentOffset + "."); } return records; }
/** * Decodes the given raw data using the configured deserializer and delivers * them to the given output port. * * @param rawData * The raw data to decode * @param dataSize * The size of the data to decode * @param outputPortName * The output port name to send the decoded records to */ protected void decodeAndDeliverRecords(final ByteBuffer rawData, final int dataSize, final String outputPortName) { final List<IMonitoringRecord> monitoringRecords = this.deserializer.deserializeRecords(rawData, dataSize); for (final IMonitoringRecord monitoringRecord : monitoringRecords) { this.deliver(outputPortName, monitoringRecord); } }
/** * Decodes the given raw data using the configured deserializer and delivers * them to the given output port. * * @param rawData * The raw data to decode * @param dataSize * The size of the data to decode * @param outputPortName * The output port name to send the decoded records to */ protected void decodeAndDeliverRecords(final ByteBuffer rawData, final int dataSize, final String outputPortName) { final List<IMonitoringRecord> monitoringRecords = this.deserializer.deserializeRecords(rawData, dataSize); for (final IMonitoringRecord monitoringRecord : monitoringRecords) { this.deliver(outputPortName, monitoringRecord); } }