Skip to content

Commit

Permalink
inital files
Browse files Browse the repository at this point in the history
  • Loading branch information
Ngorror committed Apr 2, 2015
1 parent 16cce8b commit 45da1d6
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BinaryFileWriter
==============

Binary File Writer for android for Cordova / PhoneGap.

### Supported Platforms

- Android
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": "2.0.0",
"name": "com.phonegap.plugins.binaryfilewriter",
"cordova_name": "BinaryFileWriter",
"description": "Binary File Writer for Android",
"license": "MIT",
"repo": "https://github.com/ngorror/BinaryFileWriter",
"issue": "https://github.com/ngorror/BinaryFileWriter/issues",
"platforms": [
"android"
],
"engines": [
{
"name": "cordova",
"version": ">=3.0.0"
}
]
}
33 changes: 33 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?><plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="com.phonegap.plugins.binaryfilewritter"
version="2.0.0">

<name>BinaryFileWriter</name>
<description>BinaryFileWriter for android</description>
<license>MIT</license>

<repo>https://github.com/ngorror/BinaryFileWriter</repo>
<issue>https://github.com/ngorror/BinaryFileWriter/issues</issue>

<engines>
<engine name="cordova" version=">=3.0.0" />
</engines>

<js-module src="www/binaryfilewriter.js" name="BinaryFileWriter">
<clobbers target="cordova.plugins.binaryfilewriter" />
</js-module>

<!-- android -->
<platform name="android">

<source-file src="src/android/com/phonegap/plugins/binaryfilewriter/BinaryFileWriter.java" target-dir="src/com/phonegap/plugins/binaryfilewriter" />

<config-file target="res/xml/config.xml" parent="/*">
<feature name="BinaryFileWriter">
<param name="android-package" value="com.phonegap.plugins.binaryfilewriter.BinaryFileWriter" />
</feature>
</config-file>
</platform>

</plugin>
122 changes: 122 additions & 0 deletions src/android/BinaryFileWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* PhoneGap BinaryFileWriter plugin for Android
*
*
* @author Antonio Hernandez <[email protected]>
*
*/

package com.phonegap.plugins.binaryfilewriter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import org.apache.cordova.FileUtils;
import java.io.*;
import java.net.MalformedURLException;
import java.nio.channels.FileChannel;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;


/**
* This plugin allows to write binary data to a file.
*/
public class BinaryFileWriter extends CordovaPlugin {

@Override
public PluginResult execute(String action, JSONArray args, CallbackContext callbackContext) {

PluginResult.Status status = PluginResult.Status.OK;
String result = "";

try {

if (action.equals("writeBinaryArray")) {
long fileSize = this.writeBinaryArray(args.getString(0), args.getJSONArray(1), args.getInt(2));
return new PluginResult(status, fileSize);
}

return new PluginResult(status, result);

} catch (FileNotFoundException e) {
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_FOUND_ERR);
} catch (JSONException e) {
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NO_MODIFICATION_ALLOWED_ERR);
} catch (IOException e) {
return new PluginResult(PluginResult.Status.ERROR, FileUtils.INVALID_MODIFICATION_ERR);
}
}

/**
* Write the contents of a binary array to a file.
*
* @param filename The name of the file.
* @param data The contents of the file (Array of bytes).
* @param offset The position to begin writing the file.
* @throws FileNotFoundException, IOException, JSONException
*/
public long writeBinaryArray(String filename, JSONArray data, int offset) throws FileNotFoundException, IOException, JSONException {

filename = stripFileProtocol(filename);

boolean append = false;
if (offset > 0) {
truncateFile(filename, offset);
append = true;
}

byte[] rawData = new byte[data.length()];
for (int i = 0; i < data.length(); i++) {
rawData[i] = (byte)data.getInt(i);
}

ByteArrayInputStream in = new ByteArrayInputStream(rawData);
FileOutputStream out = new FileOutputStream(filename, append);
byte buff[] = new byte[rawData.length];
in.read(buff, 0, buff.length);
out.write(buff, 0, rawData.length);
out.flush();
out.close();

return data.length();
}

/**
* This method removes the "file://" from the passed in filePath
*
* @param filePath to be checked.
* @return
*/
private String stripFileProtocol(String filePath) {
if (filePath.startsWith("file://")) {
filePath = filePath.substring(7);
}
return filePath;
}

/**
* Truncate the file to size
*
* @param filename
* @param size
* @throws FileNotFoundException, IOException
*/
private long truncateFile(String filename, long size) throws FileNotFoundException, IOException {
filename = stripFileProtocol(filename);

RandomAccessFile raf = new RandomAccessFile(filename, "rw");

if (raf.length() >= size) {
FileChannel channel = raf.getChannel();
channel.truncate(size);
return size;
}

return raf.length();
}

}
91 changes: 91 additions & 0 deletions www/binaryfilewriter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* PhoneGap BinaryFileWriter plugin
*
* @author Antonio Hernández <[email protected]>
*
*/

document.addEventListener("deviceready", function() {

/**
* Write the contents of a binary array to a file.
*
* @param binaryArray The contents of the file (Array of bytes).
*/
FileWriter.prototype.writeBinaryArray = function(binaryArray) {

// Throw an exception if we are already writing a file
if (this.readyState === FileWriter.WRITING) {
throw new FileError(FileError.INVALID_STATE_ERR);
}

// WRITING state
this.readyState = FileWriter.WRITING;

var me = this;

// If onwritestart callback
if (typeof me.onwritestart === "function") {
me.onwritestart(new ProgressEvent("writestart", {"target":me}));
}

//cordova.exec()
//PhoneGap.exec()
// Write file
cordova.exec(
// Success callback
function(r) {
// If DONE (cancelled), then don't do anything
if (me.readyState === FileWriter.DONE) {
return;
}

// position always increases by bytes written because file would be extended
me.position += r;
// The length of the file is now where we are done writing.

me.length = me.position;

// DONE state
me.readyState = FileWriter.DONE;

// If onwrite callback
if (typeof me.onwrite === "function") {
me.onwrite(new ProgressEvent("write", {"target":me}));
}

// If onwriteend callback
if (typeof me.onwriteend === "function") {
me.onwriteend(new ProgressEvent("writeend", {"target":me}));
}
},
// Error callback
function(e) {
// If DONE (cancelled), then don't do anything
if (me.readyState === FileWriter.DONE) {
return;
}

// DONE state
me.readyState = FileWriter.DONE;

// Save error
me.error = new FileError(e);

// If onerror callback
if (typeof me.onerror === "function") {
me.onerror(new ProgressEvent("error", {"target":me}));
}

// If onwriteend callback
if (typeof me.onwriteend === "function") {
me.onwriteend(new ProgressEvent("writeend", {"target":me}));
}
},
"BinaryFileWriter",
"writeBinaryArray",
[this.fileName, binaryArray, this.position]
);
};

}, false);

0 comments on commit 45da1d6

Please sign in to comment.