|
|
|
@@ -949,36 +949,30 @@ public class JuceAppActivity extends Activity |
|
|
|
|
|
|
|
public final boolean connect()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
synchronized (createStreamLock)
|
|
|
|
{
|
|
|
|
if (hasBeenCancelled.get())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
synchronized (createStreamLock)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (hasBeenCancelled.get())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
inputStream = getCancellableStream (true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ExecutionException e)
|
|
|
|
{
|
|
|
|
if (connection.getResponseCode() < 400)
|
|
|
|
catch (ExecutionException e)
|
|
|
|
{
|
|
|
|
if (connection.getResponseCode() < 400)
|
|
|
|
{
|
|
|
|
statusCode[0] = connection.getResponseCode();
|
|
|
|
connection.disconnect();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
statusCode[0] = connection.getResponseCode();
|
|
|
|
connection.disconnect();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
statusCode[0] = connection.getResponseCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized (createStreamLock)
|
|
|
|
{
|
|
|
|
if (hasBeenCancelled.get())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
@@ -989,49 +983,89 @@ public class JuceAppActivity extends Activity |
|
|
|
}
|
|
|
|
catch (ExecutionException e)
|
|
|
|
{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (java.util.Map.Entry<String, java.util.List<String>> entry : connection.getHeaderFields().entrySet())
|
|
|
|
if (entry.getKey() != null && entry.getValue() != null)
|
|
|
|
responseHeaders.append (entry.getKey() + ": "
|
|
|
|
+ android.text.TextUtils.join (",", entry.getValue()) + "\n");
|
|
|
|
for (java.util.Map.Entry<String, java.util.List<String>> entry : connection.getHeaderFields().entrySet())
|
|
|
|
if (entry.getKey() != null && entry.getValue() != null)
|
|
|
|
responseHeaders.append (entry.getKey() + ": "
|
|
|
|
+ android.text.TextUtils.join (",", entry.getValue()) + "\n");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public final void release()
|
|
|
|
static class DisconnectionRunnable implements Runnable
|
|
|
|
{
|
|
|
|
hasBeenCancelled.set (true);
|
|
|
|
public DisconnectionRunnable (HttpURLConnection theConnection,
|
|
|
|
InputStream theInputStream,
|
|
|
|
ReentrantLock theCreateStreamLock,
|
|
|
|
Object theCreateFutureLock,
|
|
|
|
Future<BufferedInputStream> theStreamFuture)
|
|
|
|
{
|
|
|
|
connectionToDisconnect = theConnection;
|
|
|
|
inputStream = theInputStream;
|
|
|
|
createStreamLock = theCreateStreamLock;
|
|
|
|
createFutureLock = theCreateFutureLock;
|
|
|
|
streamFuture = theStreamFuture;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
if (! createStreamLock.tryLock())
|
|
|
|
try
|
|
|
|
{
|
|
|
|
synchronized (createFutureLock)
|
|
|
|
if (! createStreamLock.tryLock())
|
|
|
|
{
|
|
|
|
if (streamFuture != null)
|
|
|
|
streamFuture.cancel (true);
|
|
|
|
synchronized (createFutureLock)
|
|
|
|
{
|
|
|
|
if (streamFuture != null)
|
|
|
|
streamFuture.cancel (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
createStreamLock.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
createStreamLock.lock();
|
|
|
|
}
|
|
|
|
if (connectionToDisconnect != null)
|
|
|
|
connectionToDisconnect.disconnect();
|
|
|
|
|
|
|
|
if (inputStream != null)
|
|
|
|
inputStream.close();
|
|
|
|
if (inputStream != null)
|
|
|
|
inputStream.close();
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
createStreamLock.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{}
|
|
|
|
finally
|
|
|
|
|
|
|
|
private HttpURLConnection connectionToDisconnect;
|
|
|
|
private InputStream inputStream;
|
|
|
|
private ReentrantLock createStreamLock;
|
|
|
|
private Object createFutureLock;
|
|
|
|
Future<BufferedInputStream> streamFuture;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final void release()
|
|
|
|
{
|
|
|
|
DisconnectionRunnable disconnectionRunnable = new DisconnectionRunnable (connection,
|
|
|
|
inputStream,
|
|
|
|
createStreamLock,
|
|
|
|
createFutureLock,
|
|
|
|
streamFuture);
|
|
|
|
|
|
|
|
synchronized (createStreamLock)
|
|
|
|
{
|
|
|
|
createStreamLock.unlock();
|
|
|
|
hasBeenCancelled.set (true);
|
|
|
|
|
|
|
|
connection = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
connection.disconnect();
|
|
|
|
Thread disconnectionThread = new Thread(disconnectionRunnable);
|
|
|
|
disconnectionThread.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
public final int read (byte[] buffer, int numBytes)
|
|
|
|
|