Skip to content

Commit

Permalink
timob-23493: Using write method in TiFile instead
Browse files Browse the repository at this point in the history
  • Loading branch information
fmerzadyan committed Nov 14, 2016
1 parent 8e088f9 commit 035db6d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import android.content.Context;
import android.content.ContextWrapper;
import android.os.Environment;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
Expand Down Expand Up @@ -141,12 +140,8 @@ public boolean getWritable()
@Kroll.method
public boolean append(Object data)
{
try {
return ((TiFile) tbf).append(data);
} catch (IOException e) {
Log.e(TAG, "append failed: ", e);
}
return false;
Object[] objectArray = {data};
return write(objectArray, true);
}

@Kroll.method
Expand Down Expand Up @@ -295,22 +290,21 @@ public double spaceAvailable()
}

@Kroll.method
public boolean write(Object[] args)
public boolean write(Object[] args, boolean append)
{
try {

if (args != null && args.length > 0) {
boolean append = false;
if (args.length > 1 && args[1] instanceof Boolean) {
append = ((Boolean)args[1]).booleanValue();
}

if (args[0] instanceof TiBlob) {
tbf.write((TiBlob)args[0], append);
((TiFile)tbf).write((TiBlob)args[0], append);
} else if (args[0] instanceof String) {
tbf.write((String)args[0], append);
((TiFile)tbf).write((String)args[0], append);
} else if (args[0] instanceof TiFileProxy) {
tbf.write(((TiFileProxy)args[0]).read(), append);
((TiFile)tbf).write(((TiFileProxy)args[0]).read(), append);
} else {
Log.i(TAG, "Unable to write to an unrecognized file type");
return false;
Expand All @@ -326,6 +320,12 @@ public boolean write(Object[] args)
}
}

@Kroll.method
public boolean write(Object[] args)
{
return write(args, false);
}

@Kroll.method
public void writeLine(String data)
throws IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,6 @@ 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: 0 additions & 67 deletions android/titanium/src/java/org/appcelerator/titanium/io/TiFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package org.appcelerator.titanium.io;

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

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 @@ -97,71 +95,6 @@ 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 035db6d

Please sign in to comment.