Skip to content

Commit

Permalink
created append method in TiFile and parent TiBaseFile and exposed met…
Browse files Browse the repository at this point in the history
…hod in TiFileProxy. updated yml.
  • Loading branch information
fmerzadyan committed Nov 3, 2016
1 parent 116b492 commit 8e088f9
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ public boolean getWritable()
return tbf.isWriteable();
}

@Kroll.method
public boolean append(Object data)
{
try {
return ((TiFile) tbf).append(data);
} catch (IOException e) {
Log.e(TAG, "append failed: ", e);
}
return false;
}

@Kroll.method
public boolean copy (String destination)
throws IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ public boolean isSymbolicLink() {
return flagSymbolicLink;
}

public boolean append(Object data) throws IOException {
logNotSupported("append");
return false;
}

public boolean copy(String destination) throws IOException
{
InputStream is = null;
Expand Down
67 changes: 67 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/io/TiFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package org.appcelerator.titanium.io;

import java.io.FileWriter;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
Expand All @@ -25,6 +26,7 @@

import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiBlob;
import org.appcelerator.titanium.TiFileProxy;

import android.net.Uri;
import android.os.StatFs;
Expand Down Expand Up @@ -95,6 +97,71 @@ public boolean isWriteable()
return file.canWrite();
}

@Override
public boolean append(Object data) throws IOException
{
if (data instanceof String) {
return append((String) data);
} else if (data instanceof TiFileProxy) {
return append((TiFileProxy) data);
} else if (data instanceof TiBlob) {
return append((TiBlob) data);
}
return false;
}

private boolean append(String data) throws IOException
{
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new FileWriter(file, true));
bufferedWriter.write(data);
return true;
} finally {
if (bufferedWriter != null) {
bufferedWriter.close();
}
}
}

private boolean append(TiFileProxy data) throws IOException, NullPointerException
{
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file, true);
File appendee = new File(((TiFile) data.getBaseFile()).getFile().getPath());
inputStream = new FileInputStream(appendee);
byte[] bytes = new byte[(int) appendee.length()];
// method nesting doesn't let inputStream.read bytes into outputStream
// best to keep on separate line
inputStream.read(bytes);
outputStream.write(bytes);
return true;
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}

private boolean append(TiBlob data) throws IOException
{
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file, true);
outputStream.write((data.getBytes()));
return true;
} finally {
if (outstream != null) {
outputStream.close();
}
}
}

/**
* Attempts to create a directory named by the trailing filename of this file.
* @param recursive whether to recursively create any missing parent directories in the path.
Expand Down
2 changes: 1 addition & 1 deletion apidoc/Titanium/Filesystem/File.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ methods:
type: [String,Titanium.Blob,Titanium.Filesystem.File]
returns:
type: Boolean
platforms: [iphone, ipad, mobileweb]
platforms: [android, iphone, ipad, mobileweb]
- name: copy
summary: Copies the file identified by this file object to a new path.
description: Returns `true` if the copy succeeds.
Expand Down

0 comments on commit 8e088f9

Please sign in to comment.