private byte[] marshall(Object key) { try { return (key instanceof WrappedByteArray) ? ((WrappedByteArray) key).getBytes() : this.ctx.getMarshaller().objectToByteBuffer(key); } catch (IOException | InterruptedException e) { throw new PersistenceException(e); } }
private Object unmarshall(byte[] bytes) { try { return this.ctx.getMarshaller().objectFromByteBuffer(bytes); } catch (IOException | ClassNotFoundException e) { throw new PersistenceException(e); } }
@Override public Object setAttribute(String name, Object value) { if (value == null) { return this.removeAttribute(name); } if (this.properties.isMarshalling() && !this.marshallability.isMarshallable(value)) { throw new IllegalArgumentException(new NotSerializableException(value.getClass().getName())); } Object old = this.attributes.put(name, value); this.mutator.mutate(); if (this.mutations != null) { this.mutations.remove(name); } return old; }
@Override public WrappedByteArray readObject(ObjectInput input) throws IOException { byte[] bytes = MarshallUtil.unmarshallByteArray(input); boolean hasHashCode = input.readBoolean(); if (hasHashCode) { return new WrappedByteArray(bytes, input.readInt()); } else { return new WrappedByteArray(bytes); } } }
@SerializeWith(value = SetStringConstant.Externalizer0.class) private static final class SetStringConstant<K> implements Consumer<WriteEntryView<K, String>> { @Override public void accept(WriteEntryView<K, String> wo) { wo.set("one"); } private static final SetStringConstant INSTANCE = new SetStringConstant(); public static final class Externalizer0 implements Externalizer<Object> { public void writeObject(ObjectOutput oo, Object o) {} public Object readObject(ObjectInput input) { return INSTANCE; } } }
@Override public CounterState readObject(ObjectInput input) throws IOException, ClassNotFoundException { return MarshallUtil.unmarshallEnum(input, CounterState::valueOf); } }
@Override public Object wrap(Object obj) { if (obj instanceof byte[]) return new WrappedByteArray((byte[]) obj); return obj; }
@Override public UUID readObject(ObjectInput input) throws IOException, ClassNotFoundException { return MarshallUtil.unmarshallUUID(input, true); } };
@Override public byte[] objectToByteBuffer(Object o) throws IOException, InterruptedException { if (o != null) { BufferSizePredictor sizePredictor = marshallableTypeHints .getBufferSizePredictor(o.getClass()); byte[] bytes = objectToByteBuffer(o, sizePredictor.nextSize(o)); sizePredictor.recordSize(bytes.length); return bytes; } else { return objectToByteBuffer(null, 1); } }
@Override public void finishObjectOutput(ObjectOutput oo) { marshaller.finishObjectOutput(oo); }
@Override public Object createExternal(Class<?> targetClass, ObjectInput input) throws IOException, ClassNotFoundException { return externalizer.readObject(input); } }
@Override public byte[] objectToByteBuffer(Object obj, int estimatedSize) throws IOException, InterruptedException { ByteBuffer b = objectToBuffer(obj, estimatedSize); return trimBuffer(b); }
@Override public Object createExternal(Class<?> subjectType, ObjectInput input) throws IOException, ClassNotFoundException { return new PojoWithJBossExternalize(PojoWithAttributes.readObject(input)); } }
@Override public PojoWithSerializeWith readObject(ObjectInput input) throws IOException, ClassNotFoundException { return new PojoWithSerializeWith(PojoWithAttributes.readObject(input)); } }
@Override public boolean isMarshallable(Object o) throws Exception { return marshaller.isMarshallable(o); }
@Override public MediaType mediaType() { return marshaller.mediaType(); } }
@Override public void writeExternal(Object subject, ObjectOutput output) throws IOException { PojoWithAttributes.writeObject(output, ((PojoWithJBossExternalize) subject).pojo); }
@Override public int hashCode() { return pojo != null ? pojo.hashCode() : 0; }
@Override public Object objectFromByteBuffer(byte[] buf, int offset, int length) throws IOException, ClassNotFoundException { return marshaller.objectFromByteBuffer(buf, offset, length); }
@Override public Object setAttribute(String name, Object attribute) { if (attribute == null) { return this.removeAttribute(name); } if (this.properties.isMarshalling() && !this.marshaller.isMarshallable(attribute)) { throw new IllegalArgumentException(new NotSerializableException(attribute.getClass().getName())); } V value = this.marshaller.write(attribute); UUID attributeId = this.names.get(name); if (attributeId == null) { UUID newAttributeId = UUID.randomUUID(); this.setNames(this.namesCache.getAdvancedCache().withFlags(Flag.FORCE_SYNCHRONOUS).compute(this.createKey(), this.properties.isTransactional() ? new CopyOnWriteMapPutFunction<>(name, newAttributeId) : new ConcurrentMapPutFunction<>(name, newAttributeId))); attributeId = this.names.get(name); } Object result = this.read(name, this.attributeCache.getAdvancedCache().withFlags(Flag.FORCE_SYNCHRONOUS).put(this.createKey(attributeId), value)); this.mutations.remove(name); return result; }