Search in sources :

Example 11 with EncodeException

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");
}
Also used : EncodeException(javax.websocket.EncodeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Encoder(javax.websocket.Encoder) List(java.util.List) InstanceHandle(io.undertow.servlet.api.InstanceHandle) Map(java.util.Map)

Aggregations

EncodeException (javax.websocket.EncodeException)11 IOException (java.io.IOException)7 Encoder (javax.websocket.Encoder)6 ByteBuffer (java.nio.ByteBuffer)4 ChannelBroadcastMessage (org.everrest.websockets.message.ChannelBroadcastMessage)4 Gson (com.google.gson.Gson)3 InstanceHandle (io.undertow.servlet.api.InstanceHandle)2 OutputStream (java.io.OutputStream)2 Writer (java.io.Writer)2 CharsetEncoder (java.nio.charset.CharsetEncoder)2 List (java.util.List)2 Map (java.util.Map)2 SendResult (javax.websocket.SendResult)2 Utf8Encoder (org.apache.tomcat.util.buf.Utf8Encoder)2 MessageOutputStream (org.eclipse.jetty.websocket.common.message.MessageOutputStream)2 MessageWriter (org.eclipse.jetty.websocket.common.message.MessageWriter)2 PublishDiagnosticsParamsImpl (io.typefox.lsapi.impl.PublishDiagnosticsParamsImpl)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StringWriter (java.io.StringWriter)1 SocketTimeoutException (java.net.SocketTimeoutException)1