private final HandshakeMessage reassembleFragments(int messageSeq,SortedSet<FragmentedHandshakeMessage> fragments,int totalLength,HandshakeType type,DTLSSession session) throws HandshakeException {
  HandshakeMessage message=null;
  byte[] reassembly=new byte[]{};
  int offset=0;
  for (  FragmentedHandshakeMessage fragmentedHandshakeMessage : fragments) {
    int fragmentOffset=fragmentedHandshakeMessage.getFragmentOffset();
    int fragmentLength=fragmentedHandshakeMessage.getFragmentLength();
    if (fragmentOffset == offset) {
      reassembly=ByteArrayUtils.concatenate(reassembly,fragmentedHandshakeMessage.fragmentToByteArray());
      offset=reassembly.length;
    }
 else     if (fragmentOffset < offset && (fragmentOffset + fragmentLength) > offset) {
      int newOffset=offset - fragmentOffset;
      int newLength=fragmentLength - newOffset;
      byte[] newBytes=new byte[newLength];
      System.arraycopy(fragmentedHandshakeMessage.fragmentToByteArray(),newOffset,newBytes,0,newLength);
      reassembly=ByteArrayUtils.concatenate(reassembly,newBytes);
      offset=reassembly.length;
    }
  }
  if (reassembly.length == totalLength) {
    FragmentedHandshakeMessage wholeMessage=new FragmentedHandshakeMessage(type,totalLength,messageSeq,0,reassembly,getPeerAddress());
    reassembly=wholeMessage.toByteArray();
    KeyExchangeAlgorithm keyExchangeAlgorithm=KeyExchangeAlgorithm.NULL;
    boolean receiveRawPublicKey=false;
    if (session != null) {
      keyExchangeAlgorithm=session.getKeyExchange();
      receiveRawPublicKey=session.receiveRawPublicKey();
    }
    message=HandshakeMessage.fromByteArray(reassembly,keyExchangeAlgorithm,receiveRawPublicKey,getPeerAddress());
  }
  return message;
}

private final HandshakeMessage reassembleFragments ( int messageSeq , SortedSet < FragmentedHandshakeMessage > fragments , int totalLength , HandshakeType type , DTLSSession session ) throws HandshakeException { HandshakeMessage message = null ; byte [ ] reassembly = new byte [ ] { } ; int offset = 0 ; for ( FragmentedHandshakeMessage fragmentedHandshakeMessage : fragments ) { int fragmentOffset = fragmentedHandshakeMessage . getFragmentOffset ( ) ; int fragmentLength = fragmentedHandshakeMessage . getFragmentLength ( ) ; if ( fragmentOffset == offset ) { reassembly = ByteArrayUtils . concatenate ( reassembly , fragmentedHandshakeMessage . fragmentToByteArray ( ) ) ; offset = reassembly . length ; } else if ( fragmentOffset < offset && ( fragmentOffset + fragmentLength ) > offset ) { int newOffset = offset - fragmentOffset ; int newLength = fragmentLength - newOffset ; byte [ ] newBytes = new byte [ newLength ] ; System . arraycopy ( fragmentedHandshakeMessage . fragmentToByteArray ( ) , newOffset , newBytes , 0 , newLength ) ; reassembly = ByteArrayUtils . concatenate ( reassembly , newBytes ) ; offset = reassembly . length ; } } if ( reassembly . length == totalLength ) { FragmentedHandshakeMessage wholeMessage = new FragmentedHandshakeMessage ( type , totalLength , messageSeq , 0 , reassembly , getPeerAddress ( ) ) ; reassembly = wholeMessage . toByteArray ( ) ; KeyExchangeAlgorithm keyExchangeAlgorithm = KeyExchangeAlgorithm . NULL ; boolean receiveRawPublicKey = false ; if ( session != null ) { keyExchangeAlgorithm = session . getKeyExchange ( ) ; receiveRawPublicKey = session . receiveRawPublicKey ( ) ; } message = HandshakeMessage . fromByteArray ( reassembly , keyExchangeAlgorithm , receiveRawPublicKey , getPeerAddress ( ) ) ; } return message ; }
reassembles handshake message fragments into the original message .
