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); }
public static TransferObject byteArrayToTransferObject(byte[] byteArray) { //TransferObject to = new TransferObject(); int receiveLength = byteArray.Length; byte[] toByteArray = new byte[receiveLength - 1]; Array.Copy(byteArray, 1, toByteArray, 0, receiveLength - 1); byte flagbyte = byteArray[0]; TransferObject to = null; //check if new version of transfer object if (TransferUtil.isNewVersion(flagbyte)) { to = new NewTransferObject(); } else { to = new StandardTransferObject(); } if (TransferUtil.isCompress(flagbyte)) { toByteArray = TransferUtil.getInputByCompress(toByteArray); } else { toByteArray = TransferUtil.getInputByNormal(toByteArray); } to.setByteData(toByteArray); return(to); }
public static byte[] transferObjectToByteArray(TransferObject to) { // out byte[] toByteArray = to.getByteData(); byte[] sendData = null; // if compress flag is true if (to.isCompress()) { sendData = TransferUtil.getOutputByCompress(toByteArray); } else { sendData = TransferUtil.getOutputByNormal(toByteArray); } //set long connection flag if (to.isLongConnection()) { sendData[0] |= TransferObject.LONGCONNECTION_FLAG; } //set new version flag if (to.isNewVersion()) { sendData[0] |= TransferObject.NEWVERSION_FLAG; } byte[] newData = new byte[sendData.Length + TatalaFlag.Length]; Array.Copy(TatalaFlag, 0, newData, 0, TatalaFlag.Length); Array.Copy(sendData, 0, newData, TatalaFlag.Length, sendData.Length); sendData = newData; return(sendData); }
private static byte[] getOutputByNormal(byte[] toByteArray) { //set normal flag byte[] sendData = new byte[toByteArray.Length + TransferUtil.getLengthOfByte()]; sendData[0] = TransferObject.NORMAL_FLAG; //0:normal; 1:compress Array.Copy(toByteArray, 0, sendData, TransferUtil.getLengthOfByte(), toByteArray.Length); return(sendData); }
private static byte[] getInputByNormal(byte[] toByteArray) { TransferInputStream fis = new TransferInputStream(toByteArray); int blength = fis.readInt(); byte[] buf = new byte[blength]; Array.Copy(toByteArray, TransferUtil.getLengthOfInt(), buf, 0, blength); return(buf); }
public static Object byteArrayToReturnObject(byte[] byteArray) { byte[] toByteArray = new byte[byteArray.Length - 1]; Array.Copy(byteArray, 1, toByteArray, 0, byteArray.Length - 1); if (TransferUtil.isCompress(byteArray[0])) { toByteArray = TransferUtil.getInputByCompress(toByteArray); } else { toByteArray = TransferUtil.getInputByNormal(toByteArray); } return(TransferObject.convertReturnByteArray(toByteArray)); }
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); }