769 byte[] outArray = output.array();
770 int outPos = output.position();
771 int outOfs = output.arrayOffset() + outPos;
772 int n;
773 if (isUpdate) {
774 n = engineUpdate(inArray, inOfs, inLen, outArray, outOfs);
775 } else {
776 n = engineDoFinal(inArray, inOfs, inLen, outArray, outOfs);
777 }
778 input.position(inLimit);
779 output.position(outPos + n);
780 return n;
781 } else if (!a1 && a2) {
782 int outPos = output.position();
783 byte[] outArray = output.array();
784 int outOfs = output.arrayOffset() + outPos;
785 byte[] inArray = new byte[getTempArraySize(inLen)];
786 int total = 0;
787 do {
788 int chunk = Math.min(inLen, inArray.length);
789 input.get(inArray, 0, chunk);
790 int n;
791 if (isUpdate || (inLen != chunk)) {
792 n = engineUpdate(inArray, 0, chunk, outArray, outOfs);
793 } else {
794 n = engineDoFinal(inArray, 0, chunk, outArray, outOfs);
795 }
796 total += n;
797 outOfs += n;
798 inLen -= chunk;
799 } while (inLen > 0);
800 output.position(outPos + total);
801 return total;
802 } else { // output is not backed by an accessible byte[]
803 byte[] inArray;
804 int inOfs;
805 if (a1) {
806 inArray = input.array();
807 inOfs = input.arrayOffset() + inPos;
808 } else {
809 inArray = new byte[getTempArraySize(inLen)];
810 inOfs = 0;
811 }
812 byte[] outArray = new byte[getTempArraySize(outLenNeeded)];
813 int outSize = outArray.length;
814 int total = 0;
815 boolean resized = false;
816 do {
817 int chunk = Math.min(inLen, outSize);
818 if ((a1 == false) && (resized == false)) {
819 input.get(inArray, 0, chunk);
820 inOfs = 0;
821 }
822 try {
823 int n;
824 if (isUpdate || (inLen != chunk)) {
825 n = engineUpdate(inArray, inOfs, chunk, outArray, 0);
826 } else {
827 n = engineDoFinal(inArray, inOfs, chunk, outArray, 0);
828 }
829 resized = false;
830 inOfs += chunk;
831 inLen -= chunk;
832 output.put(outArray, 0, n);
833 total += n;
834 } catch (ShortBufferException e) {
835 if (resized) {
836 // we just resized the output buffer, but it still
837 // did not work. Bug in the provider, abort
838 throw (ProviderException)new ProviderException
839 ("Could not determine buffer size").initCause(e);
840 }
841 // output buffer is too small, realloc and try again
842 resized = true;
843 int newOut = engineGetOutputSize(chunk);
844 outArray = new byte[newOut];
845 }
846 } while (inLen > 0);
847 input.position(inLimit);
848 return total;
849 }
850 }
851
852 /**
853 * Wrap a key.
854 *
855 * <p>This concrete method has been added to this previously-defined
856 * abstract class. (For backwards compatibility, it cannot be abstract.)
857 * It may be overridden by a provider to wrap a key.
858 * Such an override is expected to throw an IllegalBlockSizeException or
859 * InvalidKeyException (under the specified circumstances),
860 * if the given key cannot be wrapped.
861 * If this method is not overridden, it always throws an
862 * UnsupportedOperationException.
863 *
864 * @param key the key to be wrapped.
865 *
866 * @return the wrapped key.
867 *
|
769 byte[] outArray = output.array();
770 int outPos = output.position();
771 int outOfs = output.arrayOffset() + outPos;
772 int n;
773 if (isUpdate) {
774 n = engineUpdate(inArray, inOfs, inLen, outArray, outOfs);
775 } else {
776 n = engineDoFinal(inArray, inOfs, inLen, outArray, outOfs);
777 }
778 input.position(inLimit);
779 output.position(outPos + n);
780 return n;
781 } else if (!a1 && a2) {
782 int outPos = output.position();
783 byte[] outArray = output.array();
784 int outOfs = output.arrayOffset() + outPos;
785 byte[] inArray = new byte[getTempArraySize(inLen)];
786 int total = 0;
787 do {
788 int chunk = Math.min(inLen, inArray.length);
789 if (chunk > 0) {
790 input.get(inArray, 0, chunk);
791 }
792 int n;
793 if (isUpdate || (inLen != chunk)) {
794 n = engineUpdate(inArray, 0, chunk, outArray, outOfs);
795 } else {
796 n = engineDoFinal(inArray, 0, chunk, outArray, outOfs);
797 }
798 total += n;
799 outOfs += n;
800 inLen -= chunk;
801 } while (inLen > 0);
802 output.position(outPos + total);
803 return total;
804 } else { // output is not backed by an accessible byte[]
805 byte[] inArray;
806 int inOfs;
807 if (a1) {
808 inArray = input.array();
809 inOfs = input.arrayOffset() + inPos;
810 } else {
811 inArray = new byte[getTempArraySize(inLen)];
812 inOfs = 0;
813 }
814 byte[] outArray = new byte[getTempArraySize(outLenNeeded)];
815 int outSize = outArray.length;
816 int total = 0;
817 boolean resized = false;
818 do {
819 int chunk =
820 Math.min(inLen, (outSize == 0? inArray.length : outSize));
821 if (!a1 && !resized && chunk > 0) {
822 input.get(inArray, 0, chunk);
823 inOfs = 0;
824 }
825 try {
826 int n;
827 if (isUpdate || (inLen != chunk)) {
828 n = engineUpdate(inArray, inOfs, chunk, outArray, 0);
829 } else {
830 n = engineDoFinal(inArray, inOfs, chunk, outArray, 0);
831 }
832 resized = false;
833 inOfs += chunk;
834 inLen -= chunk;
835 if (n > 0) {
836 output.put(outArray, 0, n);
837 total += n;
838 }
839 } catch (ShortBufferException e) {
840 if (resized) {
841 // we just resized the output buffer, but it still
842 // did not work. Bug in the provider, abort
843 throw (ProviderException)new ProviderException
844 ("Could not determine buffer size").initCause(e);
845 }
846 // output buffer is too small, realloc and try again
847 resized = true;
848 outSize = engineGetOutputSize(chunk);
849 outArray = new byte[outSize];
850 }
851 } while (inLen > 0);
852 if (a1) {
853 input.position(inLimit);
854 }
855 return total;
856 }
857 }
858
859 /**
860 * Wrap a key.
861 *
862 * <p>This concrete method has been added to this previously-defined
863 * abstract class. (For backwards compatibility, it cannot be abstract.)
864 * It may be overridden by a provider to wrap a key.
865 * Such an override is expected to throw an IllegalBlockSizeException or
866 * InvalidKeyException (under the specified circumstances),
867 * if the given key cannot be wrapped.
868 * If this method is not overridden, it always throws an
869 * UnsupportedOperationException.
870 *
871 * @param key the key to be wrapped.
872 *
873 * @return the wrapped key.
874 *
|