More tweaks to FileTransfer.

Some clean-up and moved IO out of critical sections.
This commit is contained in:
Andrew Grieve 2012-10-02 11:24:52 -04:00
parent ff25be8839
commit ca9539b5b6

View File

@ -347,11 +347,11 @@ public class FileTransfer extends CordovaPlugin {
DataOutputStream dos = null; DataOutputStream dos = null;
try { try {
dos = new DataOutputStream( conn.getOutputStream() );
synchronized (context) { synchronized (context) {
if (context.aborted) { if (context.aborted) {
throw new IOException("Request aborted"); return;
} }
dos = new DataOutputStream( conn.getOutputStream() );
context.currentOutputStream = dos; context.currentOutputStream = dos;
} }
//We don't want to change encoding, we just want this to write for all Unicode. //We don't want to change encoding, we just want this to write for all Unicode.
@ -402,11 +402,11 @@ public class FileTransfer extends CordovaPlugin {
int responseCode = conn.getResponseCode(); int responseCode = conn.getResponseCode();
InputStream inStream = null; InputStream inStream = null;
try { try {
inStream = getInputStream(conn);
synchronized (context) { synchronized (context) {
if (context.aborted) { if (context.aborted) {
throw new IOException("Request aborted"); return;
} }
inStream = getInputStream(conn);
context.currentInputStream = inStream; context.currentInputStream = inStream;
} }
@ -420,6 +420,7 @@ public class FileTransfer extends CordovaPlugin {
} }
responseString = out.toString("UTF-8"); responseString = out.toString("UTF-8");
} finally { } finally {
context.currentInputStream = null;
safeClose(inStream); safeClose(inStream);
} }
@ -429,10 +430,6 @@ public class FileTransfer extends CordovaPlugin {
// send request and retrieve response // send request and retrieve response
result.setResponseCode(responseCode); result.setResponseCode(responseCode);
result.setResponse(responseString); result.setResponse(responseString);
context.currentInputStream = null;
synchronized (activeRequests) {
activeRequests.remove(objectId);
}
context.sendPluginResult(new PluginResult(PluginResult.Status.OK, result.toJSONObject())); context.sendPluginResult(new PluginResult(PluginResult.Status.OK, result.toJSONObject()));
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
@ -475,12 +472,11 @@ public class FileTransfer extends CordovaPlugin {
try { try {
stream.close(); stream.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
} }
} }
} }
private InputStream getInputStream(HttpURLConnection conn) throws IOException { private static InputStream getInputStream(HttpURLConnection conn) throws IOException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
return new DoneHandlerInputStream(conn.getInputStream()); return new DoneHandlerInputStream(conn.getInputStream());
} }
@ -488,7 +484,7 @@ public class FileTransfer extends CordovaPlugin {
} }
// always verify the host - don't check for certificate // always verify the host - don't check for certificate
final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { private final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) { public boolean verify(String hostname, SSLSession session) {
return true; return true;
} }
@ -533,7 +529,7 @@ public class FileTransfer extends CordovaPlugin {
} }
} }
private JSONObject createFileTransferError(int errorCode, String source, String target, HttpURLConnection connection) { private static JSONObject createFileTransferError(int errorCode, String source, String target, HttpURLConnection connection) {
Integer httpStatus = null; Integer httpStatus = null;
@ -553,7 +549,7 @@ public class FileTransfer extends CordovaPlugin {
* @param errorCode the error * @param errorCode the error
* @return JSONObject containing the error * @return JSONObject containing the error
*/ */
private JSONObject createFileTransferError(int errorCode, String source, String target, Integer httpStatus) { private static JSONObject createFileTransferError(int errorCode, String source, String target, Integer httpStatus) {
JSONObject error = null; JSONObject error = null;
try { try {
error = new JSONObject(); error = new JSONObject();
@ -576,7 +572,7 @@ public class FileTransfer extends CordovaPlugin {
* @param defaultString the default to be used if the arg does not exist * @param defaultString the default to be used if the arg does not exist
* @return String with the retrieved value * @return String with the retrieved value
*/ */
private String getArgument(JSONArray args, int position, String defaultString) { private static String getArgument(JSONArray args, int position, String defaultString) {
String arg = defaultString; String arg = defaultString;
if (args.length() >= position) { if (args.length() >= position) {
arg = args.optString(position); arg = args.optString(position);
@ -686,11 +682,11 @@ public class FileTransfer extends CordovaPlugin {
InputStream inputStream = null; InputStream inputStream = null;
try { try {
inputStream = getInputStream(connection);
synchronized (context) { synchronized (context) {
if (context.aborted) { if (context.aborted) {
throw new IOException("Request aborted"); return;
} }
inputStream = getInputStream(connection);
context.currentInputStream = inputStream; context.currentInputStream = inputStream;
} }
@ -708,6 +704,7 @@ public class FileTransfer extends CordovaPlugin {
context.sendPluginResult(progressResult); context.sendPluginResult(progressResult);
} }
} finally { } finally {
context.currentInputStream = null;
safeClose(inputStream); safeClose(inputStream);
safeClose(outputStream); safeClose(outputStream);
} }