use of javax.websocket.EncodeException in project undertow by undertow-io.
the class Encoding method encodeBinary.
public ByteBuffer encodeBinary(final Object o) throws EncodeException {
List<InstanceHandle<? extends Encoder>> encoders = binaryEncoders.get(o.getClass());
if (encoders == null) {
for (Map.Entry<Class<?>, List<InstanceHandle<? extends Encoder>>> entry : binaryEncoders.entrySet()) {
if (entry.getKey().isAssignableFrom(o.getClass())) {
encoders = entry.getValue();
break;
}
}
}
if (encoders != null) {
for (InstanceHandle<? extends Encoder> decoderHandle : encoders) {
Encoder decoder = decoderHandle.getInstance();
if (decoder instanceof Encoder.Binary) {
return ((Encoder.Binary) decoder).encode(o);
} else {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
((Encoder.BinaryStream) decoder).encode(o, out);
return ByteBuffer.wrap(out.toByteArray());
} catch (IOException e) {
throw new EncodeException(o, "Could not encode binary", e);
}
}
}
}
throw new EncodeException(o, "Could not encode binary");
}
Aggregations