private SSLEngineResult unwrap(CustomSSLEngine customSSLEngine, byte[] encryptedDataBytes) throws IOException, RuntimeException
    {
        byte[] pendingData = customSSLEngine.read(IReaderWriter.ReadEvent.REMAINING_UNPROCESSED_DATA);
        ByteBuffer totalIncomingData = ByteBuffer.allocate(pendingData.length + encryptedDataBytes.length);
        totalIncomingData.put(pendingData);
        totalIncomingData.put(encryptedDataBytes);
        ByteBuffer unwrappedData = new BufferAllocator().getEmptyByteBuffer(customSSLEngine, SSLManager.Operation.RECEIVING);
        SSLEngineResult result;
        int totalBytesConsumed = 0;
        int totalBytesToBeConsumed = totalIncomingData.array().length;
        System.out.println("Crypto unwrap: , total bytes to be consumed : " + totalBytesToBeConsumed);
        do
        {
            result = customSSLEngine.getSSLEngine().unwrap(totalIncomingData, unwrappedData);
            totalBytesConsumed += result.bytesConsumed();
            switch (result.getStatus())
            {
                case BUFFER_UNDERFLOW:
                    System.out.println("Crypto unwrap: data - buffer underflow, totalInSize : " + pendingData.length + " encrypt : " + encryptedDataBytes.length);
                    int netSize = customSSLEngine.getSSLEngine().getSession().getPacketBufferSize();
                    System.out.println("crypto unwrap, netsize :  " + netSize + " unwrapped capacity : " + unwrappedData.capacity());
                    if(netSize > unwrappedData.capacity())
                    {
                        System.out.println("Crypto unwrap: data - buffer underflow, if true : ");
                        ByteBuffer byteBuffer = ByteBuffer.allocate(netSize);
                        totalIncomingData.flip();
                        byteBuffer.put(totalIncomingData);
                        totalIncomingData = byteBuffer;
                        break;
                    }
                    else
                    {
                        System.out.println("Crypto unwrap: data - buffer underflow, if false: ");
                        int bytesLeftOut = totalBytesToBeConsumed - totalBytesConsumed;
                        byte[] temp = new byte[bytesLeftOut];
                        int offsetInTemp = 0;
                        if (bytesLeftOut > encryptedDataBytes.length)
                        {
                            offsetInTemp = pendingData.length - (bytesLeftOut - encryptedDataBytes.length);
                            System.arraycopy(pendingData, pendingData.length - (bytesLeftOut - encryptedDataBytes.length) , temp, 0, offsetInTemp);
                        }
                        if (bytesLeftOut<= encryptedDataBytes.length)
                        {
                            System.arraycopy(encryptedDataBytes, encryptedDataBytes.length - bytesLeftOut, temp, offsetInTemp, encryptedDataBytes.length - bytesLeftOut);
                        }
                        else
                        {
                            System.arraycopy(encryptedDataBytes, 0, temp, offsetInTemp, encryptedDataBytes.length);
                        }
                        customSSLEngine.write(IReaderWriter.WriteEvent.REMAINING_UNPROCESSED_DATA, temp);
                        return result;
                    }
                    //source buffer is small so we enlarge it. Also we might have to  wait till a complete TLS/SSL packet arrives as SSL Engine does not work on partial packets.
                case BUFFER_OVERFLOW:
                    System.out.println("Crypto unwrap: data - buffer overflow");
                    //break the data into smaller chunks as the destination buffer is small and again unwrap OR we can enlarge the buffer
                    int appSize = customSSLEngine.getSSLEngine().getSession().getApplicationBufferSize();
                    ByteBuffer byteBuffer = ByteBuffer.allocate(appSize + unwrappedData.position());
                    unwrappedData.flip();
                    byteBuffer.put(unwrappedData);
                    unwrappedData = byteBuffer;
                    break;
                case OK:
                    System.out.println("Crypto unwrap: data - buffer ok");
                    customSSLEngine.write(IReaderWriter.WriteEvent.UNWRAPPED_OUTPUT, unwrappedData.array());
                    //return result;
                case CLOSED:
                    System.out.println("Crypto unwrap: data - buffer closed");
                        break;
            }
        }while (needsUnwrap(customSSLEngine, result, totalBytesConsumed, totalBytesToBeConsumed));
        System.out.println("Crypto unwrap: data out");
        return result;
    }