@@ -15,17 +15,16 @@ Licensed to the Apache Software Foundation (ASF) under one
1515 KIND, either express or implied. See the License for the
1616 specific language governing permissions and limitations
1717 under the License.
18- */
18+ */
1919package org .apache .cordova .core ;
2020
2121import android .database .Cursor ;
2222import android .net .Uri ;
2323import android .os .Environment ;
2424import android .provider .MediaStore ;
25+ import android .util .Base64 ;
2526import android .util .Log ;
2627
27- import org .apache .commons .codec .binary .Base64 ;
28- import org .apache .cordova .DirectoryManager ;
2928import org .apache .cordova .api .CallbackContext ;
3029import org .apache .cordova .api .CordovaPlugin ;
3130import org .apache .cordova .api .PluginResult ;
@@ -135,7 +134,7 @@ else if (action.equals("readAsBinaryString")) {
135134 this .readFileAs (args .getString (0 ), start , end , callbackContext , null , PluginResult .MESSAGE_TYPE_BINARYSTRING );
136135 }
137136 else if (action .equals ("write" )) {
138- long fileSize = this .write (args .getString (0 ), args .getString (1 ), args .getInt (2 ));
137+ long fileSize = this .write (args .getString (0 ), args .getString (1 ), args .getInt (2 ), args . getBoolean ( 3 ) );
139138 callbackContext .sendPluginResult (new PluginResult (PluginResult .Status .OK , fileSize ));
140139 }
141140 else if (action .equals ("truncate" )) {
@@ -951,7 +950,7 @@ public void run() {
951950 break ;
952951 default : // Base64.
953952 String contentType = FileHelper .getMimeType (filename , cordova );
954- byte [] base64 = Base64 .encodeBase64 (bytes );
953+ byte [] base64 = Base64 .encode (bytes , Base64 . DEFAULT );
955954 String s = "data:" + contentType + ";base64," + new String (base64 , "US-ASCII" );
956955 result = new PluginResult (PluginResult .Status .OK , s );
957956 }
@@ -1000,11 +999,12 @@ private byte[] readAsBinaryHelper(String filename, int start, int end) throws IO
1000999 * @param filename The name of the file.
10011000 * @param data The contents of the file.
10021001 * @param offset The position to begin writing the file.
1002+ * @param isBinary True if the file contents are base64-encoded binary data
10031003 * @throws FileNotFoundException, IOException
10041004 * @throws NoModificationAllowedException
10051005 */
10061006 /**/
1007- public long write (String filename , String data , int offset ) throws FileNotFoundException , IOException , NoModificationAllowedException {
1007+ public long write (String filename , String data , int offset , boolean isBinary ) throws FileNotFoundException , IOException , NoModificationAllowedException {
10081008 if (filename .startsWith ("content://" )) {
10091009 throw new NoModificationAllowedException ("Couldn't write to file given its content URI" );
10101010 }
@@ -1017,7 +1017,12 @@ public long write(String filename, String data, int offset) throws FileNotFoundE
10171017 append = true ;
10181018 }
10191019
1020- byte [] rawData = data .getBytes ();
1020+ byte [] rawData ;
1021+ if (isBinary ) {
1022+ rawData = Base64 .decode (data , Base64 .DEFAULT );
1023+ } else {
1024+ rawData = data .getBytes ();
1025+ }
10211026 ByteArrayInputStream in = new ByteArrayInputStream (rawData );
10221027 FileOutputStream out = new FileOutputStream (filename , append );
10231028 byte buff [] = new byte [rawData .length ];
0 commit comments