11package io .socket .parser ;
22
33import io .socket .emitter .Emitter ;
4+ import io .socket .hasbinary .HasBinary ;
45import org .json .JSONException ;
56import org .json .JSONTokener ;
67
@@ -77,6 +78,10 @@ public static class Encoder {
7778 public Encoder () {}
7879
7980 public void encode (Packet obj , Callback callback ) {
81+ if ((obj .type == EVENT || obj .type == ACK ) && HasBinary .hasBinary (obj .data )) {
82+ obj .type = obj .type == EVENT ? BINARY_EVENT : BINARY_ACK ;
83+ }
84+
8085 logger .fine (String .format ("encoding packet %s" , obj ));
8186
8287 if (BINARY_EVENT == obj .type || BINARY_ACK == obj .type ) {
@@ -88,31 +93,23 @@ public void encode(Packet obj, Callback callback) {
8893 }
8994
9095 private String encodeAsString (Packet obj ) {
91- StringBuilder str = new StringBuilder ();
92- boolean nsp = false ;
93-
94- str .append (obj .type );
96+ StringBuilder str = new StringBuilder ("" + obj .type );
9597
9698 if (BINARY_EVENT == obj .type || BINARY_ACK == obj .type ) {
9799 str .append (obj .attachments );
98100 str .append ("-" );
99101 }
100102
101103 if (obj .nsp != null && obj .nsp .length () != 0 && !"/" .equals (obj .nsp )) {
102- nsp = true ;
103104 str .append (obj .nsp );
105+ str .append ("," );
104106 }
105107
106108 if (obj .id >= 0 ) {
107- if (nsp ) {
108- str .append ("," );
109- nsp = false ;
110- }
111109 str .append (obj .id );
112110 }
113111
114112 if (obj .data != null ) {
115- if (nsp ) str .append ("," );
116113 str .append (obj .data );
117114 }
118115
@@ -171,11 +168,11 @@ public void add(byte[] obj) {
171168 }
172169
173170 private static Packet decodeString (String str ) {
174- Packet <Object > p = new Packet <Object >();
175171 int i = 0 ;
176172 int length = str .length ();
177173
178- p .type = Character .getNumericValue (str .charAt (0 ));
174+ Packet <Object > p = new Packet <Object >(Character .getNumericValue (str .charAt (0 )));
175+
179176 if (p .type < 0 || p .type > types .length - 1 ) return error ();
180177
181178 if (BINARY_EVENT == p .type || BINARY_ACK == p .type ) {
0 commit comments