public byte[] serialize() { if (stringWrapperList.size() != 0) { int counter = 0; int dataLength = 0; // calculate serializedStringWrapperListLength for (StringWrapper stringWrapper : stringWrapperList) { byte[] serializedStringWrapper = stringWrapper.serialize(); byte[] stringWrapperLengthByteArray = DataLengthByteArrayMaker.getByteArray(serializedStringWrapper.length); dataLength += stringWrapperLengthByteArray.length + serializedStringWrapper.length; } byte[] serializedStringWrapperList = new byte[dataLength]; for (StringWrapper stringWrapper : stringWrapperList) { byte[] serializedStringWrapper = stringWrapper.serialize(); int serializedStringWrapperLength = serializedStringWrapper.length; byte[] stringWrapperLengthByteArray = DataLengthByteArrayMaker.getByteArray(serializedStringWrapperLength); System.arraycopy( stringWrapperLengthByteArray, 0, serializedStringWrapperList, counter, stringWrapperLengthByteArray.length); counter += stringWrapperLengthByteArray.length; System.arraycopy( serializedStringWrapper, 0, serializedStringWrapperList, counter, serializedStringWrapper.length); counter += serializedStringWrapper.length; } return serializedStringWrapperList; } return new byte[0]; }
public void deserialize(byte[] serializedStringWrapperList) { if (serializedStringWrapperList.length != 0) { int counter = 0; int serializedStringWrapperByteArrayLength = 0; while (true) { if (counter == serializedStringWrapperList.length) { break; } int firstLengthByte = serializedStringWrapperList[counter]; int numOfByteForLength = 1; if ((firstLengthByte & 0x80) == 0) { serializedStringWrapperByteArrayLength = serializedStringWrapperList[counter]; } else { counter++; numOfByteForLength = firstLengthByte & 0x0f; byte[] serializedStringWrapperLengthByteArray = new byte[numOfByteForLength]; int serializedDataWrapperLengthByteArrayCounter = 0; for (int i = counter; i < counter + numOfByteForLength; i++) { serializedStringWrapperLengthByteArray[serializedDataWrapperLengthByteArrayCounter++] = serializedStringWrapperList[i]; } serializedStringWrapperByteArrayLength = ByteArrayToInteger.getInt(serializedStringWrapperLengthByteArray); } counter += numOfByteForLength; byte[] stringWrapperByteArray = new byte[serializedStringWrapperByteArrayLength]; int stringWrapperByteArrayCounter = 0; for (int i = counter; i < counter + serializedStringWrapperByteArrayLength; i++) { stringWrapperByteArray[stringWrapperByteArrayCounter++] = serializedStringWrapperList[i]; } counter += serializedStringWrapperByteArrayLength; StringWrapper stringWrapper = new StringWrapper(); stringWrapper.deserialize(stringWrapperByteArray); stringWrapperList.add(stringWrapper); } } }