@@ -24,6 +24,7 @@ import java.io.File
2424import java.io.FileInputStream
2525import java.io.IOException
2626import java.io.InputStream
27+ import java.security.InvalidKeyException
2728import java.security.InvalidParameterException
2829import java.security.MessageDigest
2930import java.security.SecureRandom
@@ -77,7 +78,9 @@ class ECrypt : AnkoLogger {
7778 fun <T > encrypt (input : T , password : String , erl : ECryptResultListener ,
7879 outputFile : File = File (DEF_ENCRYPTED_FILE_PATH )) {
7980 doAsync {
80-
81+ if (password.trim().isNullOrBlank()) {
82+ erl.onFailure(" Password is null or blank." , InvalidKeyException ())
83+ }
8184 when (input) {
8285 is String -> {
8386 encrypt(input.toByteArray(), password, erl, outputFile)
@@ -142,28 +145,33 @@ class ECrypt : AnkoLogger {
142145 }
143146
144147 is InputStream -> {
148+ if (outputFile.exists()) {
149+ if (outputFile.absolutePath != DEF_ENCRYPTED_FILE_PATH ) {
150+ erl.onFailure(MSG_OUTPUT_FILE_EXISTS ,
151+ FileAlreadyExistsException (outputFile))
152+ return @doAsync
153+ }
154+ outputFile.delete()
155+ }
156+ outputFile.createNewFile()
157+
145158 val fos = outputFile.outputStream()
146159 var cos = CipherOutputStream (fos, cipher)
147- try {
148- if (outputFile.exists()) {
149- if (outputFile.absolutePath != DEF_ENCRYPTED_FILE_PATH ) {
150- erl.onFailure(MSG_OUTPUT_FILE_EXISTS ,
151- FileAlreadyExistsException (outputFile))
152- return @doAsync
153- }
154- outputFile.delete()
155- }
156- outputFile.createNewFile()
157160
161+ try {
158162 fos.write(iv)
159163 fos.write(salt)
160164 cos = CipherOutputStream (fos, cipher)
161165
162166 val buffer = ByteArray (8192 )
163- var wrote = 0
164- while ({ wrote = input.read(buffer); wrote }() > 0 ) {
165- cos.write(buffer)
166- erl.onProgress(wrote)
167+ if (input.available() <= buffer.size) {
168+ cos.write(input.readBytes())
169+ } else {
170+ var wrote = 0
171+ while ({ wrote = input.read(buffer); wrote }() > 0 ) {
172+ cos.write(buffer)
173+ erl.onProgress(wrote)
174+ }
167175 }
168176
169177 erl.onSuccess(outputFile as T )
@@ -185,7 +193,9 @@ class ECrypt : AnkoLogger {
185193 fun <T > decrypt (input : T , password : String , erl : ECryptResultListener ,
186194 outputFile : File = File (DEF_DECRYPTED_FILE_PATH )) {
187195 doAsync {
188-
196+ if (password.trim().isNullOrBlank()) {
197+ erl.onFailure(" Password is null or blank." , InvalidKeyException ())
198+ }
189199 when (input) {
190200
191201 is String -> {
@@ -259,19 +269,21 @@ class ECrypt : AnkoLogger {
259269 }
260270
261271 is InputStream -> {
272+
273+ if (outputFile.exists()) {
274+ if (outputFile.absolutePath != DEF_DECRYPTED_FILE_PATH ) {
275+ erl.onFailure(MSG_OUTPUT_FILE_EXISTS ,
276+ FileAlreadyExistsException (outputFile))
277+ return @doAsync
278+ }
279+ outputFile.delete()
280+ }
281+ outputFile.createNewFile()
282+
262283 val cis = CipherInputStream (input, cipher)
263284 val fos = outputFile.outputStream()
264- try {
265- if (outputFile.exists()) {
266- if (outputFile.absolutePath != DEF_DECRYPTED_FILE_PATH ) {
267- erl.onFailure(MSG_OUTPUT_FILE_EXISTS ,
268- FileAlreadyExistsException (outputFile))
269- return @doAsync
270- }
271- outputFile.delete()
272- }
273- outputFile.createNewFile()
274285
286+ try {
275287 val iv = ByteArray (cipher.blockSize)
276288 val salt = ByteArray (SALT_BYTES_LENGTH )
277289
@@ -292,10 +304,14 @@ class ECrypt : AnkoLogger {
292304 cipher.init (Cipher .DECRYPT_MODE , key, ivParams)
293305
294306 val buffer = ByteArray (8192 )
295- var wrote = 0
296- while ({ wrote = cis.read(buffer); wrote }() > 0 ) {
297- fos.write(buffer)
298- erl.onProgress(wrote)
307+ if (cis.available() <= buffer.size) {
308+ fos.write(cis.readBytes())
309+ } else {
310+ var wrote = 0
311+ while ({ wrote = cis.read(buffer); wrote }() > 0 ) {
312+ fos.write(buffer)
313+ erl.onProgress(wrote)
314+ }
299315 }
300316
301317 erl.onSuccess(outputFile as T )
0 commit comments