Sometimes FileNotFoundException at Url - inputstream

Sorry for bad English.
My problem is so similar this issue "FileNotFoundException at URL"
but sometimes it gives error and doesnt continue but sometimes it works perfectly. This problem is in first connection.What can be the problem that causes it works sometimes and doesnt work sometimes?
10-10 02:19:41.128 9667-9819/? W/System.err﹕ java.io.FileNotFoundException: http://cdn59.my.mail.ru/v/59908302.mp4?slave[]=s%3Ahttp%3A%2F%2Fvideo-cephgw1.i%3A8080%2Frados%2F59908302-v&p=f&expire_at=1476068400&touch=1475880462&reg=76&sign=dd01f023e682705a105440ab5e93f5cb38cfeadd
10-10 02:19:41.128 9667-9819/? W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
10-10 02:19:41.128 9667-9819/? W/System.err﹕ at makgun.kturk.MRDownloader$1.doInBackground(MRDownloader.java:105)
10-10 02:19:41.128 9667-9819/? W/System.err﹕ at makgun.kturk.MRDownloader$1.doInBackground(MRDownloader.java:39)
10-10 02:19:41.128 9667-9819/? W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
10-10 02:19:41.128 9667-9819/? W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-10 02:19:41.128 9667-9819/? W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
10-10 02:19:41.128 9667-9819/? W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-10 02:19:41.128 9667-9819/? W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-10 02:19:41.128 9667-9819/? W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
MRDownloader:
#Override
protected String doInBackground(String... urls) {
try {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" +name;
File file = new File(path);
int total;
if (file.exists())total=Integer.parseInt(String.valueOf(file.length()));
else total=0;
//New Added
if (urls[0].startsWith("//"))urls[0]="http:"+urls[0];
//New Added finished
URL url = new URL(urls[0]);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
OutputStream output = new FileOutputStream(path, true);
urlConnection.addRequestProperty("Range", "bytes=" + file.length() + "-");
if(!Cookie.isEmpty())
urlConnection.addRequestProperty("Cookie", "video_key=" +Cookie);
urlConnection.setRequestProperty("User-Agent","Mozilla/5.0");
urlConnection.setRequestProperty("Accept","*/*");
urlConnection.setConnectTimeout(7000);
urlConnection.setReadTimeout(7000);
urlConnection.connect();
int lenghtOfFile = urlConnection.getContentLength();
PD.setMax(lenghtOfFile+total);
InputStream input = new BufferedInputStream(urlConnection.getInputStream());
byte data[] = new byte[1024];
// int total=Integer.parseInt(String.valueOf(file.length()));
int count;
Log.d("MakgunLENGHT-OF-FILE", Integer.toString(lenghtOfFile));
while ((count = input.read(data)) != -1) {
total += count;
PD.setProgress(total);
output.write(data, 0, count);
if (pause){output.flush();output.close();input.close();if (PD.isShowing()) PD.dismiss();break;}
}
output.flush();
output.close();
input.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}
You can use this link to try it. TryLink.
When you request this link it generates "video_key" cookie and response body includes all of information what you need like stream URL of video. I dont copy all of java file so if you need I can add more. Thanks.

Nowadays, I have had time to understand this and finally I find out its why and when happens. I want to share this for other users. Its why is that it happens whenever server send bad request and for this site sometimes send "Service Unavaible 503" and this code failures. But adding a condition to repeat max 10 times it works like a charm. At least one click is enough and it repeats max 10 times and generally it gets 2xx response code from server after 2-3 times tried. If 2xx code received it breaks the loop. And it doesnt reapet any more. This solved my problem.
int x = 1;
while (true) {
//otherStuffs...
if (responseCode == 503) {
Log.d("makgunResponseCode503", "Now ResponseCode is 503 repeating " + x + " times");
x++;
}
else
{Log.d("makgunResponseCode", "ResponseCode is: " + responseCode);break}
if (x > 10) break;
}

Related

How to debug exceptions in TCP connection when App is restarted?

I have an application that uses Spring Integration to send messages to a vendor application over TCP and receive and process responses. The vendor sends messages without a length header or an message-ending token and the message contains carriage returns so I have implemented a custom deserializer. The messages are sent as XML strings so I have to process the input stream, looking for a specific closing tag to know when the message is complete. The application works as expected until the vendor application is restarted or a port switch occurs on my application, at which time the CPU usage on my application spikes and the application becomes unresponsive. The application throws a SocketException: o.s.integration.handler.LoggingHandler : org.springframework.messaging.MessagingException: Send Failed; nested exception is java.net.SocketException: Connection or outbound has closed when the socket closes. I have set the SocketTimeout to be 1 minute.
Here is the connection factory implementation:
#Bean
public AbstractClientConnectionFactory tcpConnectionFactory() {
TcpNetClientConnectionFactory factory = new TcpNetClientConnectionFactory(this.serverIp,
Integer.parseInt(this.port));
return getAbstractClientConnectionFactory(factory, keyStoreName, trustStoreName,
keyStorePassword, trustStorePassword, hostVerify);
}
private AbstractClientConnectionFactory getAbstractClientConnectionFactory(
TcpNetClientConnectionFactory factory, String keyStoreName, String trustStoreName,
String keyStorePassword, String trustStorePassword, boolean hostVerify) {
TcpSSLContextSupport sslContextSupport = new DefaultTcpSSLContextSupport(keyStoreName,
trustStoreName, keyStorePassword, trustStorePassword);
DefaultTcpNetSSLSocketFactorySupport tcpSocketFactorySupport =
new DefaultTcpNetSSLSocketFactorySupport(sslContextSupport);
factory.setTcpSocketFactorySupport(tcpSocketFactorySupport);
factory.setTcpSocketSupport(new DefaultTcpSocketSupport(hostVerify));
factory.setDeserializer(new MessageSerializerDeserializer());
factory.setSerializer(new MessageSerializerDeserializer());
factory.setSoKeepAlive(true);
factory.setSoTimeout(60000);
return factory;
}
Here is the deserialize method:
private String readUntil(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
String s = "";
byte[] closingTag = CLOSING_MESSAGE_TAG.getBytes(ASCII);
try {
Integer bite;
while (true) {
bite = inputStream.read();
byteArrayOutputStream.write(bite);
byte[] bytes = byteArrayOutputStream.toByteArray();
int start = bytes.length - closingTag.length;
if (start > closingTag.length) {
byte[] subarray = Arrays.copyOfRange(bytes, start, bytes.length);
if (Arrays.equals(subarray, closingTag)) {
s = new String(bytes, ASCII);
break;
}
}
}
} catch (SocketTimeoutException e) {
logger.error("Expected SocketTimeoutException thrown");
} catch (Exception e) {
logger.error("Exception thrown when deserializing message {}", s);
throw e;
}
return s;
}
Any help in identifying the cause of the CPU spike or a suggested fix would be greatly appreciated.
EDIT #1
Adding serialize method.
#Override
public void serialize(String string, OutputStream outputStream) throws IOException {
if (StringUtils.isNotEmpty(string) && StringUtils.startsWith(string, OPENING_MESSAGE_TAG) &&
StringUtils.endsWith(string, CLOSING_MESSAGE_TAG)) {
outputStream.write(string.getBytes(UTF8));
outputStream.flush();
}
}
the inbound-channel-adapter uses the ConnectionFactory
<int-ip:tcp-inbound-channel-adapter id="tcpInboundChannelAdapter"
channel="inboundReceivingChannel"
connection-factory="tcpConnectionFactory"
error-channel="errorChannel"
/>
EDIT #2
Outbound Channel Adapter
<int-ip:tcp-outbound-channel-adapter
id="tcpOutboundChannelAdapter"
channel="sendToTcpChannel"
connection-factory="tcpConnectionFactory"/>
Edit #3
We have added in the throw for the Exception and are still seeing the CPU spike, although it is not as dramatic. Could we still be receiving bytes from socket in the inputStream.read() method? The metrics seem to indicate that the read method is consuming server resources.
#Artem Bilan Thank you for your continued feedback on this. My server metrics seem to indicate that they deserialize method is what is consuming the CPU. I was thinking that the SendFailed error occurs because of the vendor restarting their application.
Thus far, I have been unable to replicate this issue other than in production. The only exception I can find in production logs is the SocketException mentioned above.
Thank you.

Does HttpGet, HttpPost abort() method aborts the request even if it is taking more time to establish the connection

I have a scenario where in certain cases request need to be terminated based on alternate configuration. From https://www.baeldung.com/httpclient-timeout I understood that we can set hard time out. However not sure how to test this.
Does the below code aborts the request with in given time even if there is a scenario of connection or socket or read timeout
HttpGet getMethod = new HttpGet(
"http://localhost:8080/httpclient-simple/api/bars/1");
int hardTimeout = 5; // seconds
TimerTask task = new TimerTask() {
#Override
public void run() {
if (getMethod != null) {
getMethod.abort();
}
}
};
new Timer(true).schedule(task, hardTimeout * 1000);
HttpResponse response = httpClient.execute(getMethod);
For instance if connection time out is set to 10 seconds and it is taking more than 10 seconds then does it terminate in 5 seconds. Similarly for other timeout scenarios.
If Apache httpclient library does not support this, is there an alternative?
Thanks in advance.
Look here for setting connection and read timeouts with apache http client.

Response to post request to AWS "breaks the pipe", cannot read

I am trying to post a video file to AWS using Android, using HttpUrlConnection. I am formatting the header and body per the instructions here http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html and here http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOST.html
I seem to be able to write the request body and file data to the connection output stream, in the sense that it lets me finish the writing block (when I had some errors in my header before, this did not happen). Nonetheless, when I check to see if the file is on AWS, it is not.
The response stream seems to be the best way to figure out why. However, when I attempt to open and input stream on the connection to get the response, this results in the "broken pipe error."
So, here is the code:
URL url = new URL(action);
HttpURLConnection conn = null;
conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Host",bucket);
conn.setRequestProperty("User-Agent","Java/1.7.0_60");
conn.setRequestProperty("Accept","text/html,application/xhtml+xml, application/xml,application/json;q=0.9,*/*;q=0.8");
conn.setRequestProperty("Acccept-Language", "en-US,en;q=0.5");
conn.setRequestProperty("Accept-Encoding","gzip, deflate");
conn.setRequestProperty("Accept-Charset","UTF-8");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
File fileObj = new File(file);
String closingString = crlf+twohyphens + boundary + twohyphens + crlf;
//get content length and set
int totalLength = body.getBytes().length + (int)fileObj.length() + closingString.getBytes().length;
conn.setRequestProperty("Content-Length",Integer.toString(totalLength));
//set body content fields
DataOutputStream request = new DataOutputStream(conn.getOutputStream());
request.writeBytes(body);
//get mp4 data and write to request:
InputStream input = new BufferedInputStream(new FileInputStream(file));
int data = input.read();
while(data != -1) {
//TODO loading animation
request.write(data);
data = input.read();
}
input.close();
Log.d("Writing","of file to output stream complete.");
request.writeBytes(closingString);
request.flush();
request.close();
Log.d("Writing","closing string to output stream complete. Closed stream");
//Response
/******** it seems to break on the following line: *************/
InputStream responseStream = new BufferedInputStream(conn.getInputStream());
BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
String line = "";
StringBuilder stringBuilder = new StringBuilder();
while ((line = responseStreamReader.readLine()) != null)
{
stringBuilder.append(line).append("\n");
}
responseStreamReader.close();
String responseString = stringBuilder.toString();
Log.d("server response",responseString);
Response response = new Response(0,"test");
conn.disconnect();
Note: the "body" string includes everything in the post body content (ie the required form fields, formatted as in the example links), except the actual file content, which must go last.
Here are the logs:
D/Writing: of file to output stream complete
D/Writing: closing string to output stream complete. Closed stream
W/System.err﹕ java.net.SocketException: sendto failed: EPIPE (Broken pipe)
W/System.err﹕ at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:546)
W/System.err﹕ at libcore.io.IoBridge.sendto(IoBridge.java:515)
W/System.err﹕ at java.net.PlainSocketImpl.write(PlainSocketImpl.java:504)
W/System.err﹕ at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:37)
W/System.err﹕ at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:266)
W/System.err﹕ at com.android.okio.Okio$1.write(Okio.java:73)
W/System.err﹕ at com.android.okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:116)
W/System.err﹕ at com.android.okio.RealBufferedSink.write(RealBufferedSink.java:44)
W/System.err﹕ at com.android.okhttp.internal.http.RetryableSink.writeToSocket(RetryableSink.java:77)
W/System.err﹕ at com.android.okhttp.internal.http.HttpConnection.writeRequestBody(HttpConnection.java:259)
W/System.err﹕ at com.android.okhttp.internal.http.HttpTransport.writeRequestBody(HttpTransport.java:84)
W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:771)
W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:379)
W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323)
W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:190)
W/System.err﹕ at android.synq.fm.connectivity.WebService.post(WebService.java:244)
W/System.err﹕ at android.synq.fm.connectivity.AwsPoster.doInBackground(AwsPoster.java:36)
W/System.err﹕ at android.synq.fm.connectivity.AwsPoster.doInBackground(AwsPoster.java:14)
W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
W/System.err﹕ Caused by: android.system.ErrnoException: sendto failed: EPIPE (Broken pipe)
W/System.err﹕ at libcore.io.Posix.sendtoBytes(Native Method)
W/System.err﹕ at libcore.io.Posix.sendto(Posix.java:176)
W/System.err﹕ at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:278)
W/System.err﹕ at libcore.io.IoBridge.sendto(IoBridge.java:513)
Please note that the log line:
W/System.err﹕ at android.synq.fm.connectivity.WebService.post(WebService.java:244)
corresponds to the following line in the code:
InputStream responseStream = new BufferedInputStream(conn.getInputStream());
So, my question is two-fold. Why does this not seem to successfully upload the file, even though it lets it be written to the connection output stream? And why does my attempt to get the request response "break the pipe"? Is there a special way to get POST responses from AWS that is not covered in the docs or that I somehow missed? (Please note this is my first time working with POST requests and AWS.)
Thanks in advance.

Servlet output stream not flushing with jrun

I'm attempting to use HTTP Streaming with a servlet. Locally I use orion as a servlet container and it works fine, but on the test server, which runs JRUN 4.0, the output is buffered even when I call flush() on the output stream. Any thoughts on why the output is being buffered and what I can do to stop it?
OutputStream os = servletResponse.getOutputStream();
while (true)
{
//attempt to write to output before doing anything else. If browser has disconnected, an IOException will be thrown so nothing else will be done
os.write(".".getBytes());
os.flush();
String response = getData();
os.write(response.getBytes());
os.flush();
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
}
}

Android Out of memory error while loading large strings from the server

Getting Out of memory error while loading large string from the server.
This is my code:
public String sendRequest(HttpRequest httpRequest){
Log.d(TAG, "Sending HTTP request to the server");
Log.d("memory", "memory size is:"+Runtime.getRuntime().maxMemory());
String response = null;
HttpHost host = new HttpHost(context.getString(R.string.HOST), -1, context.getString(R.string.SCHEME));
try {
HttpResponse httpResponse = httpClient.execute(host, httpRequest);
int status=httpResponse.getStatusLine().getStatusCode();
if (status==200) {
HttpEntity responseEntity = httpResponse.getEntity();
if (responseEntity != null) {
InputStream stream = responseEntity.getContent();
response = convertStreamToString(stream);
stream.close();
}
}
else{
response="failed:"+httpResponse.getStatusLine().toString();
}
} catch (Exception e) {
Log.v(TAG,"Request sending failed: ", e);
}
Log.d(TAG, "Completed, Sending HTTP request to the server");
return response;
}
/**
* Convert stream to string.
*
* #param is the input Stream.
* #return the string
*/
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + NEW_LINE);
}
reader.close();
} catch (IOException e) {
//do nothing.
} finally {
try {
reader.close();
} catch (IOException e) {
//do nothing.
}
}
reader=null;
return sb.toString();
}
I am getting exception at this line:
while ((line = reader.readLine()) != null) {
sb.append(line + NEW_LINE);
}
This is my logcat details:
10-10 15:01:50.270: D/dalvikvm(632): GC_FOR_ALLOC freed <1K, 37% free 24085K/38215K, paused 225ms
10-10 15:01:51.032: I/dalvikvm-heap(632): Grow heap (frag case) to 31.845MB for 8628650-byte allocation
10-10 15:01:51.929: D/dalvikvm(632): GC_CONCURRENT freed <1K, 15% free 32511K/38215K, paused 10ms+32ms
10-10 15:01:52.192: D/dalvikvm(632): GC_FOR_ALLOC freed 0K, 15% free 32511K/38215K, paused 226ms
10-10 15:01:52.192: I/dalvikvm-heap(632): Forcing collection of SoftReferences for 12942970-byte allocation
10-10 15:01:52.589: D/dalvikvm(632): GC_BEFORE_OOM freed 9K, 15% free 32502K/38215K, paused 299ms
10-10 15:01:52.589: E/dalvikvm-heap(632): Out of memory on a 12942970-byte allocation.
10-10 15:01:52.589: D/dalvikvm(632): GC_BEFORE_OOM freed 9K, 15% free 32502K/38215K, paused 299ms
10-10 15:01:52.589: E/dalvikvm-heap(632): Out of memory on a 12942970-byte allocation.
10-10 15:01:53.219: E/AndroidRuntime(632): FATAL EXCEPTION: IntentService[SurveyQuestionService]
10-10 15:01:53.219: E/AndroidRuntime(632): java.lang.OutOfMemoryError
10-10 15:01:53.219: E/AndroidRuntime(632): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
10-10 15:01:53.219: E/AndroidRuntime(632): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
10-10 15:01:53.219: E/AndroidRuntime(632): at java.lang.StringBuilder.append(StringBuilder.java:216)
10-10 15:01:53.219: E/AndroidRuntime(632): at com.handigital.products.agilesurveys.services.SurveyQuestionService.processResponse(SurveyQuestionService.java:34)
10-10 15:01:53.219: E/AndroidRuntime(632): at com.handigital.products.agilesurveys.services.AbstractIntentService.onHandleIntent(AbstractIntentService.java:44)
10-10 15:01:53.219: E/AndroidRuntime(632): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
10-10 15:01:53.219: E/AndroidRuntime(632): at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 15:01:53.219: E/AndroidRuntime(632): at android.os.Looper.loop(Looper.java:137)
10-10 15:01:53.219: E/AndroidRuntime(632): at android.os.HandlerThread.run(HandlerThread.java:60)
Any one can please help me on this.
Thanks in advance.
One way to solve this problem. In animation you can call Runtime.getRuntime().gc();
to invoke garbage collector. also in activity onDestroy() method u can call Runtime.getRuntime().gc();
You can even refer the link below,
How to resolve out of memory issue

Resources