Search in sources :

Example 6 with DecodeException

use of javax.websocket.DecodeException in project jetty.project by eclipse.

the class FruitDecoder method decode.

@Override
public Fruit decode(String s) throws DecodeException {
    Pattern pat = Pattern.compile("([^|]*)|([^|]*)");
    Matcher mat = pat.matcher(s);
    if (!mat.find()) {
        throw new DecodeException(s, "Unable to find Fruit reference encoded in text message");
    }
    Fruit fruit = new Fruit();
    fruit.name = mat.group(1);
    fruit.color = mat.group(2);
    return fruit;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) DecodeException(javax.websocket.DecodeException)

Example 7 with DecodeException

use of javax.websocket.DecodeException in project jetty.project by eclipse.

the class BadDualDecoder method decode.

@Override
public Fruit decode(ByteBuffer bytes) throws DecodeException {
    try {
        int id = bytes.get(bytes.position());
        if (id != FruitBinaryEncoder.FRUIT_ID_BYTE) {
            // not a binary fruit object
            throw new DecodeException(bytes, "Not an encoded Binary Fruit object");
        }
        Fruit fruit = new Fruit();
        fruit.name = getUTF8String(bytes);
        fruit.color = getUTF8String(bytes);
        return fruit;
    } catch (BufferUnderflowException e) {
        throw new DecodeException(bytes, "Unable to read Fruit from binary message", e);
    }
}
Also used : Fruit(org.eclipse.jetty.websocket.jsr356.samples.Fruit) DecodeException(javax.websocket.DecodeException) BufferUnderflowException(java.nio.BufferUnderflowException)

Aggregations

DecodeException (javax.websocket.DecodeException)7 Decoder (javax.websocket.Decoder)4 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Binary (javax.websocket.Decoder.Binary)2 WebSocketException (org.eclipse.jetty.websocket.api.WebSocketException)2 DecoderFactory (org.eclipse.jetty.websocket.jsr356.DecoderFactory)2 Fruit (org.eclipse.jetty.websocket.jsr356.samples.Fruit)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 BufferUnderflowException (java.nio.BufferUnderflowException)1 BinaryStream (javax.websocket.Decoder.BinaryStream)1 InvalidWebSocketException (org.eclipse.jetty.websocket.api.InvalidWebSocketException)1