Packet.java 3.17 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.socketio.impl;

import com.adobe.granite.socketio.impl.PacketType;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Packet {
    private static final Logger log = LoggerFactory.getLogger(Packet.class);
    public final PacketType type;
    public final String nsp;
    public final long id;
    public final JSONArray data;
    public final int attachments;

    public Packet(PacketType type, String nsp, long id, JSONArray data, int attachments) {
        this.type = type;
        this.nsp = nsp == null ? "/" : nsp;
        this.id = id;
        this.data = data;
        this.attachments = attachments;
    }

    public String encode() {
        if (this.type == PacketType.BINARY_EVENT || this.type == PacketType.BINARY_ACK) {
            throw new UnsupportedOperationException("binary packets not supported yet.");
        }
        StringBuilder str = new StringBuilder();
        boolean hasNsp = false;
        str.append(this.type.code);
        if (!"/".equals(this.nsp)) {
            str.append(this.nsp);
            hasNsp = true;
        }
        if (this.id >= 0) {
            if (hasNsp) {
                str.append(",");
                hasNsp = false;
            }
            str.append(this.id);
        }
        if (this.data != null) {
            if (hasNsp) {
                str.append(",");
            }
            str.append(this.data.toString());
        }
        return str.toString();
    }

    public static Packet decode(String str) {
        PacketType type;
        int d;
        int idx;
        int numAttachments = 0;
        int i = 0;
        if (((type = PacketType.valueOf(str.charAt(i++))) == PacketType.BINARY_EVENT || type == PacketType.BINARY_ACK) && (idx = str.indexOf(45)) > 0) {
            numAttachments = Integer.valueOf(str.substring(1, idx));
            i = idx + 1;
        }
        if (i == str.length()) {
            return new Packet(type, "/", -1, null, 0);
        }
        String nsp = "/";
        if ('/' == str.charAt(i)) {
            int idx2 = str.indexOf(44, i);
            if (idx2 < 0) {
                idx2 = str.length();
            }
            nsp = str.substring(i, idx2);
            i = idx2 + 1;
        }
        long id = -1;
        while (i < str.length() && (d = Character.digit(str.charAt(i), 10)) >= 0) {
            id = id < 0 ? (long)d : id * 10 + (long)d;
            ++i;
        }
        if (i < str.length() && str.charAt(i) == ',') {
            ++i;
        }
        JSONArray data = null;
        if (i < str.length()) {
            try {
                data = new JSONArray(str.substring(i));
            }
            catch (JSONException e) {
                log.error("Error decoding packet", (Throwable)e);
                type = PacketType.PARSER_ERROR;
            }
        }
        return new Packet(type, nsp, id, data, numAttachments);
    }
}