@Override public <T> SimpleMarshalledValue<T> createMarshalledValue(T object) { return new SimpleMarshalledValue<>(object, this.context); } }
@Override public boolean equals(Object object) { if (object instanceof HashableMarshalledValue) { HashableMarshalledValue<?> value = (HashableMarshalledValue<?>) object; // Testing hashCode equivalence is just an optimization if (this.hashCode != value.hashCode) return false; } return super.equals(object); }
@Override public boolean equals(Object object) { if ((object == null) || !(object instanceof SimpleMarshalledValue)) return false; @SuppressWarnings("unchecked") SimpleMarshalledValue<T> value = (SimpleMarshalledValue<T>) object; if ((this.object != null) && (value.object != null)) { return this.object.equals(value.object); } try { byte[] us = this.getBytes(); byte[] them = value.getBytes(); return ((us != null) && (them != null)) ? Arrays.equals(us, them) : (us == them); } catch (IOException e) { throw new IllegalStateException(e); } }
byte[] getBytes() throws IOException { byte[] bytes = this.bytes; if (bytes != null) return bytes; if (this.object == null) return null; int version = this.context.getCurrentVersion(); ByteArrayOutputStream output = new ByteArrayOutputStream(); ClassLoader loader = setThreadContextClassLoader(this.context.getClassLoader()); try (SimpleDataOutput data = new SimpleDataOutput(Marshalling.createByteOutput(output))) { IndexSerializer.VARIABLE.writeInt(data, version); try (Marshaller marshaller = this.context.createMarshaller(version)) { marshaller.start(data); marshaller.writeObject(this.object); marshaller.finish(); return output.toByteArray(); } } finally { setThreadContextClassLoader(loader); } }
/** * {@inheritDoc} * @see org.wildfly.clustering.marshalling.spi.MarshalledValue#get(java.lang.Object) */ @SuppressWarnings("unchecked") @Override public synchronized T get(MarshallingContext context) throws IOException, ClassNotFoundException { if (this.object == null) { this.context = context; if (this.bytes != null) { ByteArrayInputStream input = new ByteArrayInputStream(this.bytes); ClassLoader loader = setThreadContextClassLoader(this.context.getClassLoader()); try (SimpleDataInput data = new SimpleDataInput(Marshalling.createByteInput(input))) { int version = IndexSerializer.VARIABLE.readInt(data); try (Unmarshaller unmarshaller = context.createUnmarshaller(version)) { unmarshaller.start(data); this.object = (T) unmarshaller.readObject(); unmarshaller.finish(); this.bytes = null; // Free up memory } } finally { setThreadContextClassLoader(loader); } } } return this.object; }
@Override public void writeObject(ObjectOutput output, SimpleMarshalledValue<T> object) throws IOException { byte[] bytes = object.getBytes(); IndexSerializer.VARIABLE.writeInt(output, (bytes != null) ? bytes.length : 0); if (bytes != null) { output.write(bytes); } }
@Override public SimpleMarshalledValue<T> readObject(ObjectInput input) throws IOException { int size = IndexSerializer.VARIABLE.readInt(input); byte[] bytes = (size > 0) ? new byte[size] : null; if (bytes != null) { input.readFully(bytes); } return new SimpleMarshalledValue<>(bytes); }
byte[] getBytes() throws IOException { byte[] bytes = this.bytes; if (bytes != null) return bytes; if (this.object == null) return null; int version = this.context.getCurrentVersion(); ByteArrayOutputStream output = new ByteArrayOutputStream(); ClassLoader loader = setThreadContextClassLoader(this.context.getClassLoader()); try (SimpleDataOutput data = new SimpleDataOutput(Marshalling.createByteOutput(output))) { IndexSerializer.VARIABLE.writeInt(data, version); try (Marshaller marshaller = this.context.createMarshaller(version)) { marshaller.start(data); marshaller.writeObject(this.object); marshaller.finish(); return output.toByteArray(); } } finally { setThreadContextClassLoader(loader); } }
@Override public boolean equals(Object object) { if (object instanceof HashableMarshalledValue) { HashableMarshalledValue<?> value = (HashableMarshalledValue<?>) object; // Testing hashCode equivalence is just an optimization if (this.hashCode != value.hashCode) return false; } return super.equals(object); }
private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); byte[] bytes = this.getBytes(); if (bytes != null) { out.writeInt(bytes.length); out.write(bytes); } else { out.writeInt(0); } }
@Override public <T> SimpleMarshalledValue<T> createMarshalledValue(T object) { return new SimpleMarshalledValue<>(object, this.context); } }
/** * {@inheritDoc} * @see org.wildfly.clustering.marshalling.spi.MarshalledValue#get(java.lang.Object) */ @SuppressWarnings("unchecked") @Override public synchronized T get(MarshallingContext context) throws IOException, ClassNotFoundException { if (this.object == null) { this.context = context; if (this.bytes != null) { ByteArrayInputStream input = new ByteArrayInputStream(this.bytes); ClassLoader loader = setThreadContextClassLoader(this.context.getClassLoader()); try (SimpleDataInput data = new SimpleDataInput(Marshalling.createByteInput(input))) { int version = IndexSerializer.VARIABLE.readInt(data); try (Unmarshaller unmarshaller = context.createUnmarshaller(version)) { unmarshaller.start(data); this.object = (T) unmarshaller.readObject(); unmarshaller.finish(); this.bytes = null; // Free up memory } } finally { setThreadContextClassLoader(loader); } } } return this.object; }
@Override public HashableMarshalledValue<T> readObject(ObjectInput input) throws IOException, ClassNotFoundException { SimpleMarshalledValue<T> value = this.externalizer.readObject(input); int hashCode = input.readInt(); return new HashableMarshalledValue<>(value.getBytes(), hashCode); }
@Override public SimpleMarshalledValue<T> readObject(ObjectInput input) throws IOException { int size = IndexSerializer.VARIABLE.readInt(input); byte[] bytes = (size > 0) ? new byte[size] : null; if (bytes != null) { input.readFully(bytes); } return new SimpleMarshalledValue<>(bytes); }
@Override public boolean equals(Object object) { if ((object == null) || !(object instanceof SimpleMarshalledValue)) return false; @SuppressWarnings("unchecked") SimpleMarshalledValue<T> value = (SimpleMarshalledValue<T>) object; if ((this.object != null) && (value.object != null)) { return this.object.equals(value.object); } try { byte[] us = this.getBytes(); byte[] them = value.getBytes(); return ((us != null) && (them != null)) ? Arrays.equals(us, them) : (us == them); } catch (IOException e) { throw new IllegalStateException(e); } }
private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); byte[] bytes = this.getBytes(); if (bytes != null) { out.writeInt(bytes.length); out.write(bytes); } else { out.writeInt(0); } }
@Override public void writeObject(ObjectOutput output, SimpleMarshalledValue<T> object) throws IOException { byte[] bytes = object.getBytes(); IndexSerializer.VARIABLE.writeInt(output, (bytes != null) ? bytes.length : 0); if (bytes != null) { output.write(bytes); } }
@Override public HashableMarshalledValue<T> readObject(ObjectInput input) throws IOException, ClassNotFoundException { SimpleMarshalledValue<T> value = this.externalizer.readObject(input); int hashCode = input.readInt(); return new HashableMarshalledValue<>(value.getBytes(), hashCode); }