Our Daily Bread

what I need to do ...

  • Continious learning -unlearn as necessary
  • Reach out to family & loves ones
  • Maintain fitness - strong core and clear IPPT
  • Pick up parenting skills
  • Save up and plan for renovation
  • Build passive business & investment income
  • Back up Data
  • Manage $ Cash flow
  • Learn sinan mori

Wednesday, July 30, 2008

Sending Image via socket in j2me

Despite looking at several examples and forum i did not manage to resolve this for quite some time.I was blindly following the examples.File operation is suppose to be one of the basic fundamental concept in programming.After scrutinizing the code with understanding ... i set out to observe the bytes that are exchange and manage to nail down the problem.=) I am not satisfied yet got to ensure that it work for other formats and large files.

Nevertheless the following is the code snippets.

client side
=================
try {
FileConnection fileConn =
(FileConnection) Connector.open(imgPath, Connector.READ);
InputStream fis = fileConn.openInputStream();
long overallSize = fileConn.fileSize();
int FRAME_SIZE = 10 * 1024;
int length = 0;
while (length < overallSize) {
byte[] data = new byte[FRAME_SIZE];
int readAmount = fis.read(data, 0, FRAME_SIZE);
_out.writeInt(readAmount);
System.out.println("Amount of data send over>>" + readAmount);
_out.write(data, 0, readAmount);
_out.flush();
length += readAmount;
}
System.out.println("All data send over>> -1");
_out.writeInt(-1);
_out.flush();
fis.close();
fileConn.close();

} catch (IOException e) {
e.printStackTrace();

} catch (Exception e) {
e.printStackTrace();

}

output:
--------
Amount of data send over>>10240
Amount of data send over>>10240
Amount of data send over>>2567
All data send over>> -1


Server Side:
================
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
int FRAME_SIZE = 10 * 1024;
int moreBytes = in.readInt();
System.out.println("Amount of data received >>" + moreBytes);
int readAmount = 0;
byte[] data = new byte[FRAME_SIZE];
while (moreBytes != -1) {
in.readFully(data, 0, moreBytes);
out.write(data, 0, moreBytes);
out.flush();
moreBytes = in.readInt();
System.out.println("Amount of data received >>" + moreBytes);
}
System.out.println("Done");

output:
--------------
Amount of data received >>10240
Amount of data received >>10240
Amount of data received >>2567
Amount of data received >>-1
Done

1 comment:

xombrax said...

hi man, that's logic but it isn't for J2ME, the type FileOutputStream doesn't exist, only for J2EE, J2SE but no in J2ME if you have the project with that class, send me to riosx.gonzalez.omar@gmail.com, because i need to make a infrastructure client J2SE and server J2ME. Thanks i'm going to wait your answer.

Quotes

  • You are your thoughts.The thoughts in your head are what institute the laws of attraction. You think therefore you are. - Joe Vitalli, The Laws of Attraction
  • Don't react blatantly in Anger and become a Zero - Papati
  • Don't miss out in the learning values of problems by over-looking the root causes but starting at the occurences - Raj
  • You can do almost anything if you have a steady income. Little or much, what matters is that you can count it, month after month.Without the regular flow of funds, you will be constantly distracted from you goals - Norm & Bo
  • Time is greater than money, you can never really buy time. Don't let time slip away. - Raj
  • Ignore technology advancement and you will either be left behind or you have to fork out more - Raj
  • Without passion nothing happens in life but without compassion the wrong things happen - Jan Eliasson
  • The poorer you are the more you need to plan and act wisely. Any undesired outcome you have little resource to manoeuvre.
  • “Life changes when you least expect it to. The future is uncertain. So, seize this day, seize this moment, and make the most of it.”
  • To get to where you want to go in life you must start from where you are - Tan
  • Maturity does not come with age,it comes with the acceptance of responsibilities - Tan

ACM TechNews