Binary File Download using socket connection in Java ME - sockets

I am trying to download a pdf file on my mobile (using Java ME) using SocketConnection Api. The idea is to send the server a HTTP GET request, and it replies back with the data for pdf file. However, the problem I am facing is that the server initially replies back with string data (the HTTP Headers), and then the binary data. I just want to store the binary data (the pdf file).
I have written this code so far, and it works perfectly fine as far as the server replies back with string data. However, when it replies back with binary data, this code still tries to store everything as string, correctly storing the initially returned HTTP Headers (not required) and then garbled bits corresponding to the binary data of my PDF file.
public void FileDownload() {
try {
sc = (SocketConnection) Connector.open("socket://" + hostname + ":" + port);
OutputStream os = sc.openOutputStream();
os.write(("GET " + link_to_file_to_be_downloaded + " HTTP/1.0\r\n").getBytes("UTF-8"));
os.write(("HOST: " + hostname + "\r\n").getBytes("UTF-8"));
os.write(("\r\n").getBytes("UTF-8"));
os.flush();
os.close();
String url = "file:///E:/Data/" + "binary_data.pdf";
FileConnection fconn = (FileConnection) Connector.open(url, Connector.READ_WRITE);
if (!fconn.exists()) {
fconn.create();
}
OutputStream ops = fconn.openOutputStream();
byte data = 0;
in = sc.openInputStream();
data = (byte) in.read();
while (data != -1) {
ops.write(data);
data = (byte) in.read();
}
ops.flush();
ops.close();
fconn.close();
} catch (IOException ex) {
parent_class.main_form.append("Exception occured while "
+ "downloading file: " + ex.toString() + "\n");
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
parent_class.main_form.append("Exception occured while "
+ "downloading file: " + ex.toString() + "\n");
}
}
}
}
This is what gets stored in the file "binary_data.pdf" using this code -
HTTP/1.1 200 OK
Date: Sun, 25 Mar 2012 07:03:10 GMT
Server: Apache/2.2.14 (Ubuntu)
Last-Modified: Tue, 20 Mar 2012 22:00:45 GMT
ETag: "420050-12bad-4bbb3ce85fd21"
Accept-Ranges: bytes
Content-Length: 76717
Content-Type: application/pdf
Via: 1.0 www.XXX.XXX.org
Connection: close
%PDF-1.4
%????
3 0 obj <<
/Length 4077
/Filter /FlateDecode
>>
stream
x??ZYs?6~????9U.?#??Udg?M*qYJ???T-4?fq? #Z????<FT?}
lt7??n???_???4?s???????"
3????<???^?V?z??M?z??m?^????V???o??S'm6?????.??/Sx??Y?av?MB?*b^?f??/?IO??B??q??/?(??aT?a?##??,?%???Z8? ?]??-?\?]??????nw?2?;?????Z?;?[}??????&J=ml??-??V?|??:??"?(?Gf??D??~?QW?U?Z???cP?b???QX
(This operation might be simpler using the high level HttpConnection api, but I wish to understand how everything works at the most basic level, and hence I am using the SocketConnection api instead.)
In short, what I wish my app to do is simply interpret the data replied by the server correctly, either as string or binary, and then accordingly store the binary file (possibly discarding the string HTTP headers).

I found the solution. Below is the working code.
I am first storing the header response as a string. Headers are terminated by \r\n\r\n, (so, read in bytes upto these characters). Later am storing the (possibly) binary data in a file separately.
public String FileDownloadNonPersistently() {
String server_reply = new String();
try {
sc = (SocketConnection) Connector.open("socket://" + hostname + ":" + port);
os = sc.openOutputStream();
os.write(("GET " + link_to_file_to_be_downloaded +
" HTTP/1.0\r\n").getBytes("UTF-8"));
os.write(("HOST: " + hostname + "\r\n").getBytes("UTF-8"));
os.write(("\r\n").getBytes("UTF-8"));
os.flush();
os.close();
in = sc.openInputStream();
// 1. Read the response header from server separately beforehand.
byte data;
String temp_char = "";
while (!"\r\n\r\n".equals(temp_char)) {
data = (byte) in.read();
server_reply += String.valueOf((char) data);
if (((char) data) == '\r' || ((char) data) == '\n') {
temp_char += String.valueOf((char) data);
} else {
temp_char = "";
}
}
// 2. Recieving the actual data, be it text or binary
current = 0;
mybytearray = new byte[filesize];
bytesRead = in.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead = in.read(mybytearray, current,
(mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
// Store recieved data to file, if set true from options
if (tcp_save_downloaded_file == true) {
// decide an appropriate file name acc. to download link
String url = "file:///E:/Data/" + "tcp_downloaded_file.pdf";
FileConnection fconn = (FileConnection)
Connector.open(url, Connector.READ_WRITE);
if (!fconn.exists()) { // XXX. what if file already present? overwrite or append mode?
fconn.create();
}
OutputStream ops = fconn.openOutputStream();
ops.write(mybytearray, 0 , current);
ops.flush();
ops.close();
}
} catch (Exception ex) {
parent_class.main_form.append("Exception occured while "
+ "downloading file: " + ex.toString() + "\n\n");
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
parent_class.main_form.append("Exception occured while "
+ "closing inputstream "
+ "after downloading file: " + ex.toString() + "\n\n");
}
}
// XXX. see if you need to close the OutputStreams and
// SocketConnection as well.
return server_reply;
}
}

The first 10 lines are the HTTP message headers. For more information on them please go to https://www.rfc-editor.org/rfc/rfc2616#page-31.
The blank line identifies where the body starts.
You can start saving the pdf content from line 12 onwards, but you should do it using a different read method.
Instead of
data = (byte) in.read();
while (data != -1) {
ops.write(data);
data = (byte) in.read();
}
please try
byte buff[] = new byte[1024];
int len = in.read(buff);
while (len > 0) {
ops.write(buff, 0, len);
len = in.read(buff);
}

Related

Karate ApacheHttpClient Response is failing with error com.intuit.karate - Runtimejava.io.IOException: Attempted read from closed stream

Reference: java.io.IOException: Attempted read from closed stream
Reference: https://github.com/karatelabs/karate/blob/master/karate-core/src/main/java/com/intuit/karate/http/ApacheHttpClient.java
KarateCore - class file: ApacheHttpClient.java is unable to process the Response its failing at the code line
CloseableHttpClient client = clientBuilder.build();
CloseableHttpResponse httpResponse;
byte [] bytes;
try {
httpResponse = client.execute(requestBuilder.build());
HttpEntity responseEntity = httpResponse.getEntity();
if (responseEntity == null || responseEntity.getContent() == null) {
bytes = Constants.ZERO_BYTES;
} else {
**InputStream is = responseEntity.getContent();**
bytes = FileUtils.toBytes(is);
}
request.setEndTimeMillis(System.currentTimeMillis());
} catch (Exception e) {
if (e instanceof ClientProtocolException && e.getCause() != null) { // better error message
throw new RuntimeException(e.getCause());
} else {
throw new RuntimeException(e);
}
}
The Code is failing at line InputStream is = responseEntity.getContent(); when trying to read from a closed stream. The exception message displayed
Error com.intuit.karate - Runtimejava.io.IOException: Attempted read from closed stream.
May be the InputStream need to updated.
I am able read the Http Response content using below code
BufferedReader br = new BufferedReader(
new InputStreamReader((httpResponse.getEntity().getContent())));
String output;
while ((output = br.readLine()) != null){
output = br,readLine();
System.out.println(output);
}
Also the able to read the response using EntityUtils as a string content
String content = EntityUtils.toString(responseEntity);
System.out.println(content);
Not sure if i am missing something in the feature:scenario file response or the ApacheHttpClient.java file need to updated to read InputStream and then convert to bytes.
Feature: Hello
Scenario: Rest API Post
Given url 'some url path'
And header Content-Type = 'application/json'
And request { username: 'abc', password: 'pwd' }
When method POST
Then status 200
And print 'Response is:', response
The expected Response is a JSON format as:
{
"accessToken": "akjdoioikf",
"expires":"2020-01-29T01:09:48Z"
}
Any suggestions, appreciated!

Using multipart/form-data in Windows metro apps

I'm trying to create a way to upload text data (database key) along with an .png image from a Windows 8.1 App, through a multipart/form-data HTTP POST request, to a PHP script hosted on a webserver (which saves the image locally using the database key as a filename)
I've had a look at packet sniffer and it's all coming out the way I think it should, but the problem is that the PHP $_POST value is not passing through and is null on the php script side.
Here is the C# code on the Windows Metro app side:
private async void UploadImage() {
string newLine = Environment.NewLine;
WebRequest request = WebRequest.Create("http://www.mywebsite.com/receiveimage.php");
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "POST";
string boundary = "---------------------BUIUFBILPQMZ81C12CCC2EVV2RJ";
request.ContentType = "multipart/form-data; boundary=" + boundary;
// Open pre-saved MY_FILE.png file from local disk and store in fh
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile fh = await localFolder.GetFileAsync("MY_FILE.png");
// Header strings and values
string Prefix1 = boundary + newLine + "Content-Disposition: form-data; name=\"SERIAL_NO\"" + newLine + newLine; // SERIAL_NO is the php $_POST variable
string Prefix2 = boundary + newLine + "Content-Disposition: form-data; name=\"MY_FILE\"; filename=\"" + "MY_FILE.png\"" + newLine + "Content-Type: application/base64" + newLine + newLine;
byte[] baPrefix1 = System.Text.Encoding.UTF8.GetBytes(Prefix1);
byte[] baValue1 = System.Text.Encoding.UTF8.GetBytes("C4145-12" + newLine); // (C4145-12 is the database key)
IBuffer buffer = await FileIO.ReadBufferAsync(fh);
byte[] baPrefix2 = System.Text.Encoding.UTF8.GetBytes(Prefix1);
byte[] baValue2 = System.Text.Encoding.UTF8.GetBytes(System.Convert.ToBase64String(buffer.ToArray()).Replace("+", "%2B") + newLine);
byte[] combinedData = new byte[baPrefix1.Length + baValue1.Length + baPrefix2.Length + baValue2.Length];
System.Buffer.BlockCopy(baPrefix1, 0, combinedData, 0, baPrefix1.Length);
System.Buffer.BlockCopy(baValue1, 0, combinedData, baPrefix1.Length, baValue1.Length);
System.Buffer.BlockCopy(baPrefix2, 0, combinedData, baPrefix1.Length + baValue1.Length, baPrefix2.Length);
System.Buffer.BlockCopy(baValue2, 0, combinedData, baPrefix1.Length + baValue1.Length + baPrefix2.Length, baValue2.Length);
Stream dataStream = await request.GetRequestStreamAsync();
dataStream.Write(combinedData, 0, combinedData.Length);
dataStream.Dispose();
using (WebResponse response = await request.GetResponseAsync())
{
// "STATUS" is just a text block
STATUS.Text = (((HttpWebResponse)response).StatusDescription);
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseFromServer = reader.ReadToEnd();
STATUS.Text = responseFromServer;
}
}
The PHP script on the server works with other forms but can't recognize the value passed in from this script, so I know it's not an issue with that. I'm convinced it's a problem with the header formatting. I've experimented with the amount of new lines but to no avail. Any ideas?

Why does this text decoded in 2 different ways not match in GWT?

I've been trying to track down why my Russian translations are not appearing correctly in the GWT version of my game. I've narrowed it down to something going wrong with the decoding of the file. This code works correctly outside of the GWT environment.
I create the UTF-8 byte array from a string for this test. The method below outputs two instances of the text to the log. The first uses new String(bytes) and gives the correct output, the second uses the BufferedReader and produces incorrect output. The diff of the two files can be seen here.
The classes I'm using for localisation are using the ByteBuffer approach and are therefore outputting incorrect text for the Russian translation and I'm struggling to understand why.
public void test(){
String text = "# suppress inspection \"UnusedProperty\" for whole file\n" +
"\n" +
"# Notes\n" +
"# I used the phrase \"Power Flower\" in English as it rhymes. They can be called something else in other languages.\n" +
"# They're \"fleurs magiques\" (Magic Flowers) in French.\n" +
"\n" +
"# Tutorials\n" +
"#-----------\n" +
"Tutorial_1_1=Составляй слова, проводя пальцем по буквам.Сейчас попробуй создать слово 'СОТЫ'\n" +
"Tutorial_1_2=Ты можешь складывать слова справа налево. Попробуй составить слово 'ЖАЛО' справа налево\n" +
"Tutorial_1_3=Слова могут распологаться сверху вниз, снизу вверх, справа налево, слева направо, а также по диагонали.\n" +
"Tutorial_1_4=Создавая слова, ты можешь изменять направление.Составь слово 'ВОСК'\n" +
"Tutorial_1_5=Ты даже можешь пересекать свое собственное слово. Тем не менее, используй каждую букву только один раз. А сейчас, сложи слово 'УЛЕЙ'\n" +
"Tutorial_1_6=Чем длиннее окажется твоё слово, тем больше у тебя шансов получить много очков и возможность заработать Чудо-Цветок. Составь слово 'ПЧЕЛА'\n" +
"Tutorial_1_7=Получи Чудо-Цветы за каждое слово из пяти или более букв. Они могут быть использованы в качестве любой из букв.\n" +
"Tutorial_1_8=Составь слово 'СТЕБЕЛЬ'\n" +
"Tutorial_1_9=Из разных по длине и форме слов получаются разные Чудо-Цветы.\n" +
"Tutorial_1_10=Теперь ты справишься сам. Составь еще четыре слова, чтобы уровень был пройден";
// This defaults to the default charset, which in my instance, and most probably yours is UTF-8
byte[] bytes = new byte[0];
try {
bytes = text.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String test = new String(bytes);
// This is correct
Gdx.app.log("File1", test);
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
InputStreamReader reader = null;
try {
reader = new InputStreamReader(is, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
BufferedReader br = new BufferedReader(reader);
StringBuilder fileContents = new StringBuilder();
String line;
try {
while ((line = br.readLine()) != null) {
fileContents.append(line + "\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
// This is incorrect
Gdx.app.log("File2", fileContents.toString());
}
It would appear the ByteArrayInputStream and the BufferedReader partial strings are being decoded by the UTF-8 decoder which is corrupting the result. This would appear to be a GWT issue.

Bad socket id, Stream closed error + blackberry socket connection

I am writing a program in blackberry curve 9300 using socketConnection. I get stream closed/bad socket id error when I try to read the inputstream.
Can you suggest me what could be the issue.
String url = Constants.SOCKET + serverIp + Constants.COLON + serverPort + UserInfo.getConnectionType();
//connection open
socketConne`enter code here`ction = (SocketConnection) Connector.open(url, Connector.READ_WRITE,true);
socketConnection.setSocketOption(SocketConnection.DELAY, 0);
socketConnection.setSocketOption(SocketConnection.KEEPALIVE, 1);
socketConnection.setSocketOption(SocketConnection.RCVBUF, 16834);
socketConnection.setSocketOption(SocketConnection.LINGER, 10);
outputStream = socketConnection.openDataOutputStream();
//Output stream deligated to Sender Object to send the command
sender = new Sender(outputStream);
sender.send(command);
inputStream = socketConnection.openDataInputStream();
//read inputstream
while (tobeRead < toRead && retryCount < 5) {
try {
if ((toRead - tobeRead) > 8192) {
readBufferLength = 8192;
} else {
readBufferLength = (toRead - tobeRead);
}
buffer = new byte[readBufferLength + 1];
bytesRead = inputStream.read(buffer, 0, readBufferLength);
sb.append(new String(buffer, 0, bytesRead));
tobeRead += bytesRead;
totalBytesRead += bytesRead;
} catch (Exception cce) {
}
}
say if number of bytes to read is 5192. I get say 2123 bytes then it iterates the loop, next time while reading it fails. I run this in seperate thread. I use WIFI network for connectivity

reading content of a file from server then sending it to client through socket C#

i am trying here to send the content of a text file by the server and send it to the client
this is the server
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 9050);
server.Bind(localEP);
server.Listen(10);
Console.WriteLine("Waiting for Client...");
Socket client = server.Accept();
IPAddress clientAddress = ((IPEndPoint)client.RemoteEndPoint).Address;
Console.WriteLine("Got connection from " + clientAddress);
NetworkStream stream = new NetworkStream(client);
StreamReader reader = new StreamReader(stream);
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine("Welcome to my test server");
writer.Flush();
string line = null;
while ((line = reader.ReadLine()).Length != 0)
{
Console.WriteLine("loooking for this file:" + line);
System.IO.FileInfo fi = new System.IO.FileInfo(line);
Console.WriteLine("Found");
writer.WriteLine("File Size: " + fi.Length + "\nContent:");
StreamReader tr = new StreamReader(line);
string s = null;
//string b = "";
while((s= tr.ReadLine()).Length != 0)
{
writer.WriteLine(tr.ReadLine());
writer.Flush();
}
tr.Close();
}
client.Close(); server.Close();
the part of the client where it reads from the server is this
String line = null;
line = textBox3.Text;
writer.WriteLine(line); // Send line to Server
writer.Flush();
string s = null;
// Read line from server, then echo on the screen
while((s= reader.ReadLine()).Length != 0)
{
textBox4.Text += reader.ReadLine() + "\r\n\r\n";
}
when i run the code, no errors at all, but the client get stuck, and when i stop the server, the content of the file will show,,, BTW, its a GUI application
while ((s = reader.ReadLine()) != null) {
textBox4.Text += s;
}
Sample code for StreamReader uses the construct below to detect end of stream. Also - do you really want to read two lines in that loop?
while (reader.Peek() >= 0)
{
s= reader.ReadLine();
textBox4.Text += s + Environment.NewLine + Environment.NewLine;
}
You mentioned that this is a GUI app? If so, on which thread are you doing the reading? If you are doing the read on the main thread, then the application messageloop will be frozen and nothing will show up until you stop the other side and kill the connection.