diff --git a/AndroidAsync/src/com/koushikdutta/async/http/HybiParser.java b/AndroidAsync/src/com/koushikdutta/async/http/HybiParser.java index 071fe6e96..6fcab23c7 100644 --- a/AndroidAsync/src/com/koushikdutta/async/http/HybiParser.java +++ b/AndroidAsync/src/com/koushikdutta/async/http/HybiParser.java @@ -35,6 +35,7 @@ import com.koushikdutta.async.DataEmitterReader; import com.koushikdutta.async.callback.DataCallback; + import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; @@ -351,7 +352,8 @@ private byte[] frame(int opcode, byte [] data, int errorCode, int dataOffset, in frame[2] = (byte) (length / 256); frame[3] = (byte) (length & BYTE); } else { - + // Original codes: + /* frame[1] = (byte) (masked | 127); frame[2] = (byte) (( length / _2_TO_56_) & BYTE); frame[3] = (byte) (( length / _2_TO_48_) & BYTE); @@ -361,13 +363,35 @@ private byte[] frame(int opcode, byte [] data, int errorCode, int dataOffset, in frame[7] = (byte) (( length / _2_TO_16_) & BYTE); frame[8] = (byte) (( length / _2_TO_8_) & BYTE); frame[9] = (byte) (length & BYTE); + */ + + // Fixed #437. I guess there are some code rendering issues that + // probably affect the arraycopy process of the frame byte array. + // So I wrote S32_TO_8BYTES function to bypass it. It doesn't force + // to cast int typ to byte and assigns byte values return from the + // function so that no code rendering will occur around frame. + // Therefore, we will have a correct byte array of frames. + // Rogerus Rex scripsit. 14/4/2016 + byte[] len=S32_TO_8BYTES(length); + frame[1] = (byte) (masked | 127); + frame[2] = len[0]; + frame[3] = len[1]; + frame[4] = len[2]; + frame[5] = len[3]; + frame[6] = len[4]; + frame[7] = len[5]; + frame[8] = len[6]; + frame[9] = len[7]; + + } if (errorCode > 0) { frame[offset] = (byte) ((errorCode / 256) & BYTE); frame[offset+1] = (byte) (errorCode & BYTE); } - + + System.arraycopy(buffer, dataOffset, frame, offset + insert, dataLength - dataOffset); if (mMasking) { @@ -375,13 +399,30 @@ private byte[] frame(int opcode, byte [] data, int errorCode, int dataOffset, in (byte) Math.floor(Math.random() * 256), (byte) Math.floor(Math.random() * 256), (byte) Math.floor(Math.random() * 256), (byte) Math.floor(Math.random() * 256) }; + + System.arraycopy(mask, 0, frame, header, mask.length); mask(frame, mask, offset); + } + return frame; } + private static byte[] S32_TO_8BYTES(int s32){ + byte[] ret=new byte[8]; + ret[0]=0; + ret[1]=0; + ret[2]=0; + ret[3]=0; + ret[4]=Integer.valueOf(((s32&0xFF000000) >>> 24) & 0xFF).byteValue(); + ret[5]=Integer.valueOf(((s32&0x00FF0000) >>> 16) & 0xFF).byteValue(); + ret[6]=Integer.valueOf(((s32&0x0000FF00) >>> 8 ) & 0xFF).byteValue(); + ret[7]=Integer.valueOf( s32&0x000000FF ).byteValue(); + return ret; + } + public void close(int code, String reason) { if (mClosed) return; sendFrame(frame(OP_CLOSE, reason, code));