Example usage for io.netty.buffer ByteBufInputStream reset

List of usage examples for io.netty.buffer ByteBufInputStream reset

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufInputStream reset.

Prototype

@Override
    public void reset() throws IOException 

Source Link

Usage

From source file:com.corundumstudio.socketio.JsonSupportWrapper.java

License:Apache License

public AckArgs readAckArgs(ByteBufInputStream src, AckCallback<?> callback) throws IOException {
    try {// w ww.ja  v a2 s. com
        return delegate.readAckArgs(src, callback);
    } catch (Exception e) {
        src.reset();
        log.error("Can't read ack args: " + src.readLine() + " for type: " + callback.getResultClass(), e);
        throw new IOException(e);
    }
}

From source file:com.corundumstudio.socketio.JsonSupportWrapper.java

License:Apache License

public <T> T readValue(String namespaceName, ByteBufInputStream src, Class<T> valueType) throws IOException {
    try {/*from   w w w  .  jav  a  2 s . c  o m*/
        return delegate.readValue(namespaceName, src, valueType);
    } catch (Exception e) {
        src.reset();
        log.error("Can't read value: " + src.readLine() + " for type: " + valueType, e);
        throw new IOException(e);
    }
}

From source file:com.corundumstudio.socketio.parser.Decoder.java

License:Apache License

private Packet decodePacket(ByteBuf buffer, UUID uuid) throws IOException {
    if (buffer.readableBytes() < 3) {
        throw new DecoderException("Can't parse " + buffer.toString(CharsetUtil.UTF_8));
    }//from  w w w .j  ava2 s  .c o m
    PacketType type = getType(buffer);

    int readerIndex = buffer.readerIndex() + 1;
    // 'null' to avoid unnecessary StringBuilder creation
    boolean hasData = false;
    StringBuilder messageId = null;
    for (readerIndex += 1; readerIndex < buffer.readableBytes(); readerIndex++) {
        if (messageId == null) {
            messageId = new StringBuilder(4);
        }
        byte msg = buffer.getByte(readerIndex);
        if (msg == Packet.SEPARATOR) {
            break;
        }
        if (msg != (byte) '+') {
            messageId.append((char) msg);
        } else {
            hasData = true;
        }
    }
    Long id = null;
    if (messageId != null && messageId.length() > 0) {
        id = Long.valueOf(messageId.toString());
    }

    // 'null' to avoid unnecessary StringBuilder creation
    StringBuilder endpointBuffer = null;
    for (readerIndex += 1; readerIndex < buffer.readableBytes(); readerIndex++) {
        if (endpointBuffer == null) {
            endpointBuffer = new StringBuilder();
        }
        byte msg = buffer.getByte(readerIndex);
        if (msg == Packet.SEPARATOR) {
            break;
        }
        endpointBuffer.append((char) msg);
    }

    String endpoint = Namespace.DEFAULT_NAME;
    if (endpointBuffer != null && endpointBuffer.length() > 0) {
        endpoint = endpointBuffer.toString();
    }

    if (buffer.readableBytes() == readerIndex) {
        buffer.readerIndex(buffer.readableBytes());
    } else {
        readerIndex += 1;
        buffer.readerIndex(readerIndex);
    }

    Packet packet = new Packet(type);
    packet.setEndpoint(endpoint);
    if (id != null) {
        packet.setId(id);
        if (hasData) {
            packet.setAck(Packet.ACK_DATA);
        } else {
            packet.setAck(true);
        }
    }

    switch (type) {
    case ERROR: {
        if (!buffer.isReadable()) {
            break;
        }
        String[] pieces = buffer.toString(CharsetUtil.UTF_8).split("\\+");
        if (pieces.length > 0 && pieces[0].trim().length() > 0) {
            ErrorReason reason = ErrorReason.valueOf(Integer.valueOf(pieces[0]));
            packet.setReason(reason);
            if (pieces.length > 1) {
                ErrorAdvice advice = ErrorAdvice.valueOf(Integer.valueOf(pieces[1]));
                packet.setAdvice(advice);
            }
        }
        break;
    }

    case MESSAGE: {
        if (buffer.isReadable()) {
            packet.setData(buffer.toString(CharsetUtil.UTF_8));
        } else {
            packet.setData("");
        }
        break;
    }

    case EVENT: {
        ByteBufInputStream in = new ByteBufInputStream(buffer);
        Event event = jsonSupport.readValue(in, Event.class);
        packet.setName(event.getName());
        if (event.getArgs() != null) {
            packet.setArgs(event.getArgs());
        }
        break;
    }

    case JSON: {
        ByteBufInputStream in = new ByteBufInputStream(buffer);
        JsonObject obj = jsonSupport.readValue(in, JsonObject.class);
        if (obj != null) {
            packet.setData(obj.getObject());
        } else {
            in.reset();
            Object object = jsonSupport.readValue(in, Object.class);
            packet.setData(object);
        }
        break;
    }

    case CONNECT: {
        if (buffer.isReadable()) {
            packet.setQs(buffer.toString(CharsetUtil.UTF_8));
        }
        break;
    }

    case ACK: {
        if (!buffer.isReadable()) {
            break;
        }
        boolean validFormat = true;
        int plusIndex = -1;
        for (int i = buffer.readerIndex(); i < buffer.readerIndex() + buffer.readableBytes(); i++) {
            byte dataChar = buffer.getByte(i);
            if (!Character.isDigit(dataChar)) {
                if (dataChar == '+') {
                    plusIndex = i;
                    break;
                } else {
                    validFormat = false;
                    break;
                }
            }
        }
        if (!validFormat) {
            break;
        }

        if (plusIndex == -1) {
            packet.setAckId(parseLong(buffer));
            break;
        } else {
            packet.setAckId(parseLong(buffer, plusIndex));
            buffer.readerIndex(plusIndex + 1);

            ByteBufInputStream in = new ByteBufInputStream(buffer);
            AckCallback<?> callback = ackManager.getCallback(uuid, packet.getAckId());
            AckArgs args = jsonSupport.readAckArgs(in, callback);
            packet.setArgs(args.getArgs());
        }
        break;
    }

    case DISCONNECT:
    case HEARTBEAT:
    case NOOP:
        break;
    }

    buffer.readerIndex(buffer.readerIndex() + buffer.readableBytes());
    return packet;
}

From source file:com.corundumstudio.socketio.parser.JsonSupportWrapper.java

License:Apache License

public AckArgs readAckArgs(ByteBufInputStream src, Class<?> argType) throws IOException {
    try {/* ww w .  j  a v  a2s .co  m*/
        return delegate.readAckArgs(src, argType);
    } catch (IOException e) {
        src.reset();
        log.error("Can't read ack args: " + src.readLine() + " for type: " + argType, e);
        return null;
    } catch (RuntimeException e) {
        src.reset();
        log.error("Can't read ack args: " + src.readLine() + " for type: " + argType, e);
        return null;
    }
}

From source file:com.corundumstudio.socketio.parser.JsonSupportWrapper.java

License:Apache License

public <T> T readValue(ByteBufInputStream src, Class<T> valueType) throws IOException {
    try {//from ww  w . j  a  v a  2 s  . c o m
        return delegate.readValue(src, valueType);
    } catch (IOException e) {
        src.reset();
        log.error("Can't read value 1: " + src.readLine() + " for type: " + valueType, e);
        return null;
    } catch (RuntimeException e) {
        src.reset();
        log.error("Can't read value 2: " + src.readLine() + " for type: " + valueType, e);
        return null;
    }
}

From source file:whitespell.net.websockets.socketio.parser.Decoder.java

License:Apache License

private Packet decodePacket(ByteBuf buffer, UUID uuid) throws IOException {
    if (buffer.readableBytes() < 3) {
        throw new DecoderException("Can't parse " + buffer.toString(CharsetUtil.UTF_8));
    }//  ww w . j  a v a2 s .c o  m
    PacketType type = getType(buffer);

    int readerIndex = buffer.readerIndex() + 1;
    // 'null' to avoid unnecessary StringBuilder creation
    boolean hasData = false;
    StringBuilder messageId = null;
    for (readerIndex += 1; readerIndex < buffer.readableBytes(); readerIndex++) {
        if (messageId == null) {
            messageId = new StringBuilder(4);
        }
        byte msg = buffer.getByte(readerIndex);
        if (msg == Packet.SEPARATOR) {
            break;
        }
        if (msg != (byte) '+') {
            messageId.append((char) msg);
        } else {
            hasData = true;
        }
    }
    Long id = null;
    if (messageId != null && messageId.length() > 0) {
        id = Long.valueOf(messageId.toString());
    }

    // 'null' to avoid unnecessary StringBuilder creation
    StringBuilder endpointBuffer = null;
    for (readerIndex += 1; readerIndex < buffer.readableBytes(); readerIndex++) {
        if (endpointBuffer == null) {
            endpointBuffer = new StringBuilder();
        }
        byte msg = buffer.getByte(readerIndex);
        if (msg == Packet.SEPARATOR) {
            break;
        }
        endpointBuffer.append((char) msg);
    }

    String endpoint = Namespace.DEFAULT_NAME;
    if (endpointBuffer != null && endpointBuffer.length() > 0) {
        endpoint = endpointBuffer.toString();
    }

    if (buffer.readableBytes() == readerIndex) {
        buffer.readerIndex(buffer.readableBytes());
    } else {
        readerIndex += 1;
        buffer.readerIndex(readerIndex);
    }

    Packet packet = new Packet(type);
    packet.setEndpoint(endpoint);
    if (id != null) {
        packet.setId(id);
        if (hasData) {
            packet.setAck(Packet.ACK_DATA);
        } else {
            packet.setAck(true);
        }
    }

    switch (type) {
    case ERROR: {
        if (!buffer.isReadable()) {
            break;
        }
        String[] pieces = buffer.toString(CharsetUtil.UTF_8).split("\\+");
        if (pieces.length > 0 && pieces[0].trim().length() > 0) {
            ErrorReason reason = ErrorReason.valueOf(Integer.valueOf(pieces[0]));
            packet.setReason(reason);
            if (pieces.length > 1) {
                ErrorAdvice advice = ErrorAdvice.valueOf(Integer.valueOf(pieces[1]));
                packet.setAdvice(advice);
            }
        }
        break;
    }

    case MESSAGE: {
        if (buffer.isReadable()) {
            packet.setData(buffer.toString(CharsetUtil.UTF_8));
        } else {
            packet.setData("");
        }
        break;
    }

    case EVENT: {
        ByteBufInputStream in = new ByteBufInputStream(buffer);
        Event event = jsonSupport.readValue(in, Event.class);
        packet.setName(event.getName());
        if (event.getArgs() != null) {
            packet.setArgs(event.getArgs());
        }
        break;
    }

    case JSON: {
        ByteBufInputStream in = new ByteBufInputStream(buffer);
        JsonObject obj = jsonSupport.readValue(in, JsonObject.class);
        if (obj != null) {
            packet.setData(obj.getObject());
        } else {
            in.reset();
            Object object = jsonSupport.readValue(in, Object.class);
            packet.setData(object);
        }
        break;
    }

    case CONNECT: {
        if (buffer.isReadable()) {
            packet.setQs(buffer.toString(CharsetUtil.UTF_8));
        }
        break;
    }

    case ACK: {
        if (!buffer.isReadable()) {
            break;
        }
        boolean validFormat = true;
        int plusIndex = -1;
        for (int i = buffer.readerIndex(); i < buffer.readerIndex() + buffer.readableBytes(); i++) {
            byte dataChar = buffer.getByte(i);
            if (!Character.isDigit(dataChar)) {
                if (dataChar == '+') {
                    plusIndex = i;
                    break;
                } else {
                    validFormat = false;
                    break;
                }
            }
        }
        if (!validFormat) {
            break;
        }

        if (plusIndex == -1) {
            packet.setAckId(parseLong(buffer));
            break;
        } else {
            packet.setAckId(parseLong(buffer, plusIndex));
            buffer.readerIndex(plusIndex + 1);

            ByteBufInputStream in = new ByteBufInputStream(buffer);
            AckCallback<?> callback = ackManager.getCallback(uuid, packet.getAckId());
            AckArgs args = jsonSupport.readAckArgs(in, callback.getResultClass());
            packet.setArgs(args.getArgs());
        }
        break;
    }

    case DISCONNECT:
    case HEARTBEAT:
    case NOOP:
        break;
    }

    buffer.readerIndex(buffer.readerIndex() + buffer.readableBytes());
    return packet;
}

From source file:zixin.socket.JsonSupportWrapper.java

License:Apache License

public AckArgs readAckArgs(ByteBufInputStream src, AckCallback<?> callback) throws IOException {
    try {/*from  ww  w. ja  v  a2 s. c  om*/
        return delegate.readAckArgs(src, callback);
    } catch (Exception e) {
        src.reset();
        log.error("Can't read ack args: " + src.readLine() + " for type: " + callback.getResultClass(), e);
        return null;
    }
}

From source file:zixin.socket.JsonSupportWrapper.java

License:Apache License

public <T> T readValue(String namespaceName, ByteBufInputStream src, Class<T> valueType) throws IOException {
    try {//from  w w w. java2  s .  c o m
        return delegate.readValue(namespaceName, src, valueType);
    } catch (Exception e) {
        src.reset();
        log.error("Can't read value: " + src.readLine() + " for type: " + valueType, e);
        return null;
    }
}