CommandResponseMarshaller(ChannelCommandDispatcherFactoryConfiguration config) { this.context = config.getMarshallingContext(); this.factory = config.getChannelFactory(); this.versionSerializer = IndexSerializer.select(this.context.getCurrentVersion()); }
@Override public void objectToStream(Object object, DataOutput stream) throws Exception { int version = this.context.getCurrentVersion(); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); try (DataOutputStream output = new DataOutputStream(bytes)) { this.versionSerializer.writeInt(output, version); try (Marshaller marshaller = this.context.createMarshaller(version)) { marshaller.start(Marshalling.createByteOutput(output)); marshaller.writeObject(object); marshaller.flush(); } } byte[] buffer = bytes.toByteArray(); IndexSerializer.VARIABLE.writeInt(stream, buffer.length); stream.write(buffer); }
@Override public <R> byte[] marshal(Command<R, ? super C> command) throws IOException { int version = this.context.getCurrentVersion(); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); try (DataOutputStream output = new DataOutputStream(bytes)) { IndexSerializer.VARIABLE.writeInt(output, version); try (Marshaller marshaller = this.context.createMarshaller(version)) { marshaller.start(Marshalling.createByteOutput(output)); marshaller.writeObject(this.id); marshaller.writeObject(command); marshaller.flush(); } return bytes.toByteArray(); } } }
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); } }
CommandResponseMarshaller(ChannelCommandDispatcherFactoryConfiguration config) { this.context = config.getMarshallingContext(); this.factory = config.getChannelFactory(); this.versionSerializer = IndexSerializer.select(this.context.getCurrentVersion()); }
@Override public void objectToStream(Object object, DataOutput stream) throws Exception { int version = this.context.getCurrentVersion(); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); try (DataOutputStream output = new DataOutputStream(bytes)) { this.versionSerializer.writeInt(output, version); try (Marshaller marshaller = this.context.createMarshaller(version)) { marshaller.start(Marshalling.createByteOutput(output)); marshaller.writeObject(object); marshaller.flush(); } } byte[] buffer = bytes.toByteArray(); IndexSerializer.VARIABLE.writeInt(stream, buffer.length); stream.write(buffer); }
@Override public <R> byte[] marshal(Command<R, ? super C> command) throws IOException { int version = this.context.getCurrentVersion(); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); try (DataOutputStream output = new DataOutputStream(bytes)) { IndexSerializer.VARIABLE.writeInt(output, version); try (Marshaller marshaller = this.context.createMarshaller(version)) { marshaller.start(Marshalling.createByteOutput(output)); marshaller.writeObject(this.id); marshaller.writeObject(command); marshaller.flush(); } return bytes.toByteArray(); } } }
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); } }