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. improvements made.
  • Loading branch information
Farzad Merzadyan committed Oct 25, 2016
1 parent 667bdf7 commit 162600d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
*/
package org.appcelerator.titanium;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.content.ContextWrapper;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.io.TiBaseFile;
import org.appcelerator.titanium.io.TiFile;
import org.appcelerator.titanium.io.TiFileFactory;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiFileHelper2;
Expand Down Expand Up @@ -72,7 +76,6 @@ public TiFileProxy(String sourceUrl, String[] parts, boolean resolve)
} else {
path = TiFileHelper2.joinSegments(parts);
}

if (resolve) {
path = resolveUrl(scheme, path);
}
Expand Down Expand Up @@ -134,6 +137,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 @@ -129,6 +129,11 @@ public boolean isHidden() {
public boolean isSymbolicLink() {
return flagSymbolicLink;
}

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

public boolean copy(String destination) throws IOException
{
Expand Down Expand Up @@ -478,4 +483,4 @@ public OutputStream getExistingOutputStream()
* @module.api
*/
public abstract File getNativeFile();
}
}
71 changes: 69 additions & 2 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 @@ -28,6 +29,7 @@

import android.net.Uri;
import android.os.StatFs;
import org.appcelerator.titanium.TiFileProxy;

/**
* An extension of {@link TiBaseFile}, used for representing a file on the device's true file system.
Expand All @@ -39,17 +41,17 @@ public class TiFile extends TiBaseFile

private final File file;
private final String path;



public TiFile(File file, String path, boolean stream)
{
super(TiBaseFile.TYPE_FILE);
this.file = file;
this.path = path;
this.stream = stream;
}



/**
* @return true if the file is a plain file, false otherwise.
*/
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

0 comments on commit 162600d

Please sign in to comment.