You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by GitBox <gi...@apache.org> on 2018/02/26 22:51:32 UTC

[GitHub] AnthonyWard commented on issue #217: CB-13245: (android) Fix bug caused by negative length reported for an asset

AnthonyWard commented on issue #217: CB-13245: (android) Fix bug caused by negative length reported for an asset
URL: https://github.com/apache/cordova-plugin-file/pull/217#issuecomment-368679789
 
 
   This tripped me up the past few days. I also set up an app to demo this bug:
   
   https://github.com/AnthonyWard/cordova-file-plugin-bug
   
   One line of code it seems can fix it more "upstream" than this, but I don't know if it's a good idea:
   
   `cordova-android` -> `CordovaResourceApi.java` -> `OpenForReadResult` there is one path (in the `catch`) that leaves the length as -1 causing the defect
   
   ```java
   case URI_TYPE_ASSET: {
                   String assetPath = uri.getPath().substring(15);
                   AssetFileDescriptor assetFd = null;
                   InputStream inputStream;
                   long length = -1;
                   try {
                       assetFd = assetManager.openFd(assetPath);
                       inputStream = assetFd.createInputStream();
                       length = assetFd.getLength();
                   } catch (FileNotFoundException e) {
                       // Will occur if the file is compressed.
                       inputStream = assetManager.open(assetPath);
                   }
                   String mimeType = getMimeTypeFromPath(assetPath);
                   return new OpenForReadResult(uri, inputStream, mimeType, length, assetFd);
               }
   ```
   
   Adding `length = inputStream.available();` in the catch fixes the issue for me, but I'm not a java developer so can't evaluate if that's a bad idea for another reason.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org