Search in sources :

Example 1 with Fruit

use of org.eclipse.jetty.websocket.jsr356.samples.Fruit in project jetty.project by eclipse.

the class BadDualDecoder 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) Fruit(org.eclipse.jetty.websocket.jsr356.samples.Fruit) Matcher(java.util.regex.Matcher) DecodeException(javax.websocket.DecodeException)

Example 2 with Fruit

use of org.eclipse.jetty.websocket.jsr356.samples.Fruit 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)2 Fruit (org.eclipse.jetty.websocket.jsr356.samples.Fruit)2 BufferUnderflowException (java.nio.BufferUnderflowException)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1