public static int getExpectReceiveLength(byte[] receiveData) { int expectReceiveLength = 0; TransferInputStream fis = new TransferInputStream(receiveData); if (TransferUtil.isCompress(fis.readByte())) { fis.readInt();//uncompresslength expectReceiveLength = TransferUtil.getLengthOfByte() + TransferUtil.getLengthOfInt() + TransferUtil.getLengthOfInt() + fis.readInt(); } else { expectReceiveLength = TransferUtil.getLengthOfByte() + TransferUtil.getLengthOfInt() + fis.readInt(); } return(expectReceiveLength); }
private static byte[] getOutputByCompress(byte[] toByteArray) { int unCompressedLength = 0; int compressedLength = 0; byte[] input = toByteArray; unCompressedLength = input.Length; MemoryStream memoryStream = new MemoryStream(); Deflater compressor = new Deflater(); DeflaterOutputStream defos = new DeflaterOutputStream(memoryStream, compressor); defos.Write(input, 0, input.Length); defos.Flush(); defos.Finish(); byte[] output = memoryStream.ToArray(); compressedLength = output.Length; memoryStream.Close(); defos.Close(); //set compress flag and compressedLength, unCompressedLength byte[] sendData = new byte[output.Length + TransferUtil.getLengthOfByte() + TransferUtil.getLengthOfInt() + TransferUtil.getLengthOfInt()]; sendData[0] = TransferObject.COMPRESS_FLAG; //0:normal; 1:compress TransferOutputStream fos = new TransferOutputStream(sendData); fos.skipAByte(); fos.writeInt(unCompressedLength); fos.writeInt(compressedLength); Array.Copy(output, 0, sendData, TransferUtil.getLengthOfByte() + TransferUtil.getLengthOfInt() + TransferUtil.getLengthOfInt(), output.Length); return(sendData); }