public void readString(byte[] str, int strBufferSize) { if (!readCheck(sizeof(int))) { return; } // 先读入字符串长度 int readLen = 0; read(ref readLen); if (!readCheck(readLen)) { return; } // 如果存放字符串的空间大小不足以放入当前要读取的字符串,则只拷贝能容纳的长度,但是下标应该正常跳转 if (strBufferSize <= readLen) { BinaryUtility.memcpy(str, mBuffer, 0, mIndex, strBufferSize - 1); mIndex += readLen; // 加上结束符 str[strBufferSize - 1] = 0; } else { BinaryUtility.memcpy(str, mBuffer, 0, mIndex, readLen); mIndex += readLen; // 加上结束符 str[readLen] = 0; } }
//---------------------------------------------------------------------------------------------------------------------------------- protected void receiveData(short[] data, int dataCount) { // 缓冲区还能直接放下数据 if (mCurDataCount + dataCount <= mBufferSize) { BinaryUtility.memcpy(mReceivedData, data, mCurDataCount, 0, dataCount); mCurDataCount += dataCount; } // 数据量超出缓冲区 else { int copyDataCount = mBufferSize - mCurDataCount; BinaryUtility.memcpy(mReceivedData, data, mCurDataCount, 0, copyDataCount); // 数据存满后通知回调 mRecordCallback(mReceivedData, mBufferSize); // 清空缓冲区,存储新的数据 int remainCount = dataCount - copyDataCount; // 缓冲区无法存放剩下的数据,则将多余的数据丢弃 if (remainCount > mBufferSize) { BinaryUtility.memcpy(mReceivedData, data, 0, copyDataCount, mBufferSize - copyDataCount); mCurDataCount = mBufferSize - copyDataCount; } else { BinaryUtility.memcpy(mReceivedData, data, 0, copyDataCount, remainCount); mCurDataCount = remainCount; } } }
protected void addDataToInputBuffer(byte[] data, int count) { // 缓冲区足够放下数据时才处理 if (count <= mInputBuffer.Length - mInputDataSize) { BinaryUtility.memcpy(mInputBuffer, data, mInputDataSize, 0, count); mInputDataSize += count; } }
public void writeBuffer(byte[] buffer, int bufferSize) { if (!writeCheck(bufferSize)) { return; } BinaryUtility.memcpy(mBuffer, buffer, mIndex, 0, bufferSize); mIndex += bufferSize; }
public void write(float value) { int writeLen = sizeof(float); if (!writeCheck(writeLen)) { return; } BinaryUtility.memcpy(mBuffer, BinaryUtility.toBytes(value), mIndex, 0, writeLen); mIndex += writeLen; }
public void endWaveStream() { mDataSize = mWaveDataSerializer.getDataSize(); mDataBuffer = new byte[mDataSize]; BinaryUtility.memcpy(mDataBuffer, mWaveDataSerializer.getBuffer(), 0, 0, mDataSize); mWaveDataSerializer = null; int mixDataCount = getMixPCMDataCount(); mMixPCMData = new short[mixDataCount]; generateMixPCMData(mMixPCMData, mixDataCount, mSoundChannels, mDataBuffer, mDataSize); refreshFileSize(); }
public void loadData(string filePath, bool forceCover) { // 根据文件名查找工厂类型 string fileName = StringUtility.getFileNameNoSuffix(filePath, true); DATA_TYPE type = getDataTypeByDataName(fileName); if (type == DATA_TYPE.DT_MAX) { UnityUtility.logError("error : can not find data file define, file name : " + fileName + ", filePath : " + filePath); return; } // 如果该数据已经存在,并且需要覆盖,则先删除数据 if (mDataStructList.ContainsKey(type)) { if (forceCover) { destroyData(type); } else { return; } } // 打开文件 int fileSize = 0; byte[] fileBuffer = null; FileUtility.openFile(filePath, ref fileBuffer, ref fileSize); // 解析文件 List <Data> dataList = new List <Data>(); int dataSize = getDataSize(type); byte[] dataBuffer = new byte[dataSize]; int dataCount = fileSize / dataSize; for (int i = 0; i < dataCount; ++i) { Data newData = createData(type); if (newData == null) { UnityUtility.logError("error : can not create data ,type : " + type); return; } BinaryUtility.memcpy(dataBuffer, fileBuffer, 0, i * dataSize, dataSize); newData.read(dataBuffer, dataSize); dataList.Add(newData); } mDataStructList.Add(type, dataList); }
public void read(ref short value) { int readLen = sizeof(short); if (!readCheck(readLen)) { return; } byte[] dest = BinaryUtility.toBytes(value); BinaryUtility.memcpy(dest, mBuffer, 0, mIndex, readLen); value = BinaryUtility.bytesToShort(dest); mIndex += readLen; }
public void writeString(string str) { int strLen = str.Length; if (!writeCheck(strLen + sizeof(int))) { return; } // 先写入字符串长度 write(strLen); BinaryUtility.memcpy(mBuffer, BinaryUtility.stringToBytes(str), mIndex, 0, strLen); mIndex += strLen; }
public static void generateMixPCMData(short[] mixPCMData, int mixDataCount, short channelCount, short[] dataBuffer, int bufferSize) { // 如果单声道,则直接将mDataBuffer的数据拷贝到mMixPCMData中 if (channelCount == 1) { BinaryUtility.memcpy(mixPCMData, dataBuffer, 0, 0, MathUtility.getMin(bufferSize, mixDataCount)); } // 如果有两个声道,则将左右两个声道的平均值赋值到mMixPCMData中 else if (channelCount == 2) { for (int i = 0; i < mixDataCount; ++i) { mixPCMData[i] = (short)((dataBuffer[2 * i + 0] + dataBuffer[2 * i + 1]) * 0.5f); } } }
public void readBuffer(byte[] buffer, int bufferSize, int readLen) { if (!readCheck(readLen)) { return; } // 如果存放数据的空间大小不足以放入当前要读取的数据,则只拷贝能容纳的长度,但是下标应该正常跳转 if (bufferSize <= readLen) { BinaryUtility.memcpy(buffer, mBuffer, 0, mIndex, bufferSize); mIndex += readLen; } else { BinaryUtility.memcpy(buffer, mBuffer, 0, mIndex, readLen); mIndex += readLen; } }
public void setRecordData(short[] data, int dataSize) { // 检测数据量是否正确 if (mBlockBufferSize != dataSize) { return; } // 由于缓冲区大小和每次获取的数据数量都是固定的,所以只要有数据就需要移动 // 将已有的数据移到缓冲区头部,然后将新的数据加入尾部 if (mAllPCMCount > 0) { BinaryUtility.memmove(mAllPCMData, 0, dataSize, mRecorderDataBlockSize - dataSize); BinaryUtility.memcpy(mAllPCMData, data, mRecorderDataBlockSize - dataSize, 0, dataSize); } else { BinaryUtility.memset(mAllPCMData, (short)0, mRecorderDataBlockSize); BinaryUtility.memcpy(mAllPCMData, data, 0, 0, dataSize); mAllPCMCount = dataSize; } }
public override byte[] toBytes() { mCmdID = 2; mKeyID = 1; mValueLength = sizeof(byte); int payloadLen = sizeof(byte) + sizeof(byte) + sizeof(byte) + mValueLength; byte[] payload = new byte[payloadLen]; int index = 0; BinaryUtility.writeByte(payload, ref index, mCmdID); BinaryUtility.writeByte(payload, ref index, mKeyID); BinaryUtility.writeByte(payload, ref index, mValueLength); BinaryUtility.writeByte(payload, ref index, mFriction); mHeader = new PacketHeader(GameDefine.REPORT_OUT); mHeader.mPayloadLength = (ushort)payloadLen; mHeader.mCRC16 = BinaryUtility.crc16(0xFF, payload, payloadLen); byte[] headerData = mHeader.toBytes(); byte[] packetData = new byte[payload.Length + headerData.Length]; BinaryUtility.memcpy(packetData, headerData, 0); BinaryUtility.memcpy(packetData, payload, headerData.Length); return(packetData); }
public void setValue(byte[] value) { int minCount = value.Length < mValue.Length ? value.Length : mValue.Length; BinaryUtility.memcpy(mValue, value, 0, 0, minCount); }
public override void readData(byte[] data, int dataSize) { mData = new byte[dataSize]; BinaryUtility.memcpy(mData, data, 0, 0, dataSize); }