private static byte[] readBytes(DataInput input) { int length = input.readVInt(); return input.readBytes(length); }
@Override public Object deserialize(DataInput dataInput) { int size = dataInput.readVInt(); return new ByteArray(dataInput.readBytes(size)); } }
@Override @SuppressWarnings("unchecked") public ByteArray read(DataInput dataInput) { int length = dataInput.readInt(); // We can use ByteArray.wrap here since in this use case the dataInput // will always be a DataInputImpl which copies the bytes when reading // them. return ByteArray.wrap(dataInput.readBytes(length)); }
public static Link read(DataInput dataInput, IdGenerator idGenerator) { // Format: see write(DataOutput). int tableLength = dataInput.readInt(); byte[] tableBytes = dataInput.readBytes(tableLength); int recordIdLength = dataInput.readInt(); byte[] recordIdBytes = null; if (recordIdLength > 0) { recordIdBytes = dataInput.readBytes(recordIdLength); } String args = dataInput.readUTF(); if (recordIdLength == 0 && args == null) { return new Link(); } LinkBuilder builder = Link.newBuilder(); if (tableLength > 0) { builder.table(new String(tableBytes)); } if (recordIdLength > 0) { RecordId id = idGenerator.fromBytes(recordIdBytes); builder.recordId(id); } if (args != null && args.length() > 0) { argsFromString(args, builder, args /* does not matter, should never be invalid */); } return builder.create(); }
/** * See write for the byte format. */ @Override @SuppressWarnings("unchecked") public Blob read(DataInput dataInput) { // Read the encoding version byte, but ignore it for the moment since there is only one encoding dataInput.readByte(); int keyLength = dataInput.readVInt(); byte[] key = null; if (keyLength > 0) { key = dataInput.readBytes(keyLength); } String mediaType = dataInput.readUTF(); Long size = dataInput.readLong(); if (size == -1) { size = null; } String filename = dataInput.readUTF(); return new Blob(key, mediaType, size, filename); }
/** * Decodes links from Lily 2.0 and earlier (before table name was added to Link serialization). */ public static Link decode(DataInput dataInput, IdGenerator idGenerator) { // The bytes format is as follows: // [byte representation of master record id, if not null][args: bytes of the string representation] int recordIdLength = dataInput.readInt(); byte[] recordIdBytes = null; if (recordIdLength > 0) { recordIdBytes = dataInput.readBytes(recordIdLength); } String args = dataInput.readUTF(); if (recordIdLength == 0 && args == null) { return new Link(); } LinkBuilder builder = Link.newBuilder(); if (recordIdLength > 0) { RecordId id = idGenerator.fromBytes(recordIdBytes); builder.recordId(id); } if (args != null && args.length() > 0) { argsFromString(args, builder, args /* does not matter, should never be invalid */); } return builder.create(); }
public static Link decodeLink(DataInput dataInput, IdGenerator idGenerator) { // Format: see toBytes. int recordIdLength = dataInput.readInt(); byte[] recordIdBytes = null; if (recordIdLength > 0) { recordIdBytes = dataInput.readBytes(recordIdLength); } String args = dataInput.readUTF(); if (recordIdLength == 0 && args == null) { return new Link(); } Link.LinkBuilder builder = Link.newBuilder(); if (recordIdLength > 0) { RecordId id = decode(new DataInputImpl(recordIdBytes), idGenerator); builder.recordId(id); } if (args != null && args.length() > 0) { argsFromString(args, builder, args /* does not matter, should never be invalid */); } return builder.create(); }
@Override @SuppressWarnings("unchecked") public Record read(DataInput dataInput) throws RepositoryException, InterruptedException { Record record = new RecordImpl(); dataInput.readByte(); // Ignore, there is currently only one encoding : 1 int length = dataInput.readVInt(); byte[] recordTypeId = dataInput.readBytes(length); Long recordTypeVersion = dataInput.readLong(); RecordType recordType = typeManager.getRecordTypeById(new SchemaIdImpl(recordTypeId), recordTypeVersion); record.setRecordType(recordType.getName(), recordTypeVersion); Map<SchemaId, QName> idToQNameMapping = new HashMap<SchemaId, QName>(); List<FieldType> fieldTypes = getSortedFieldTypes(recordType); for (FieldType fieldType : fieldTypes) { byte readByte = dataInput.readByte(); if (DEFINED == readByte) { Object value = fieldType.getValueType().read(dataInput); record.setField(fieldType.getName(), value); idToQNameMapping.put(fieldType.getId(), fieldType.getName()); } } Map<Scope, SchemaId> recordTypeIds = new EnumMap<Scope, SchemaId>(Scope.class); recordTypeIds.put(Scope.NON_VERSIONED, recordType.getId()); return new IdRecordImpl(record, idToQNameMapping, recordTypeIds); }