---------------------
public int integerFromByteArray(byte [] byteArray) {
System.out.println("integer conversion from byte array ---------------------------------");
int k = 0;
int tempRes = 0;
int res=0;
for (int i=byteArray.length -1 ; i>=0; i--) {
tempRes = (int) byteArray[i];
int tempForAnding = tempRes;
System.out.println("byteArray = " + (3-i));
int j = 0;
do{
k = (3-i)*8 + j;
int temp = tempForAnding & 1;
if (temp == 1){
//if (k==0)// since 2 to the power 0 = 1
// res = 1;
//else {
res += 1 << k; // 2 to the power 2 = do a plus 4 = left shift 2
System.out.print("1");
//}
} else
System.out.print("0");
j++;
tempForAnding = tempRes >> j;
//System.out.println("after shifting " + j + " times " + tempRes);
}while (j < 8);
System.out.println(" ");
}
return res;
}
// reza 18.2.2008
public void copyBytes(byte [] dest, byte [] src, int stIndex, int len) {
int k = 0;
for (int i = stIndex; i < stIndex + len; i++) {
dest[k] = src[i];
System.out.println("byte["+ k + "] == " + dest[k] );
k++;
}
}
// reza 18.2.2008
public void preparingImage(byte [] result){
int noOfImages = -1;
int byteArrayIndex = 0;
int imageSize = -1;
byte [] temp = null;
byte [] imageArray = new byte[5000];
temp = new byte[4];
copyBytes(temp, result, 0, 4);
byteArrayIndex += 4;
noOfImages = integerFromByteArray(temp);
System.out.println("noOfImages =========================== " + noOfImages);
get_testForm();
if ( noOfImages > 0) {
for(int n = 0; n < noOfImages; n++) {
copyBytes(temp,result,byteArrayIndex,4);
byteArrayIndex += 4;
imageSize = integerFromByteArray(temp);
System.out.println(n + " image size " + " is ================== " + imageSize);
copyBytes(imageArray, result, byteArrayIndex, imageSize);
byteArrayIndex += imageSize;
try{
Image image = Image.createImage(imageArray,0,imageSize);
testform.append(image);
} catch (Exception ex) {
System.out.println(ex);
}
}
}
System.out.println("byteArrayIndex ====== " + byteArrayIndex);
}
// reza 18.2.2008 // this form shows the image the command action code is not given
// the callFileServlet command calls the servlet using http connection in response receives the // the byte array
Form testform;
Command callFileServlet;
public Form get_testForm() {
if (testform == null){
testform = new Form("testing multiple images");
callFileServlet = new Command("ok", Command.OK, 1);
testform.addCommand(callFileServlet);
testform.setCommandListener(this);
}
return testform;
}
J2EE Servlet code:
--------------------
package image;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FileServlet extends HttpServlet {
/** Processes requests for both HTTP
GET and POST methods.* @param request servlet request
* @param response servlet response
*/
byte [] fileBuffer = new byte[5000];
int Buffer_Size=0;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
ServletOutputStream sout = response.getOutputStream();
String FileNames [] = {"e:\\yahooonline.png","e:\\yahooonline.png","e:\\yahooonline.png","e:\\yahooonline.png","e:\\yahooonline.png"};
ProcessFiles(sout, 5, FileNames);
//sout.write(fileBuffer);
}
//
/** Handles the HTTP
GET method.* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP
POST method.* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
//
public byte [] getByteFromFile(String FileName)
{
byte [] b= null;
try
{
File file = new File(FileName);
b = new byte[(int)file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(b, 0, (int)file.length());
System.out.println("Writing File of Size :: " + file.length());
}
catch (Exception ex) {
ex.printStackTrace();
}
return b;
}
public void ProcessFiles(ServletOutputStream sout,int no_of_file, String [] fileID)
{
int size = 0;
// get byte for file number...
byte [] file_num = Convert_To_Byte(no_of_file);
try {
//Add file number to file buffer...
//appendToBuffer(file_num);
sout.write(file_num);
System.out.println("No of File :: " + no_of_file);
} catch (Exception ex) {
ex.printStackTrace();
}
//After adding no of file buffer size will increase by 4 byte...
Buffer_Size = Buffer_Size + 4;
for(int i=0; i < no_of_file; i++)
{
String fileName = fileID[i];
size = getFileSize(fileName);
System.out.println("File Size : " + size);
//Add file size to buffer
//appendToBuffer(Convert_To_Byte(size));
try {
//Add file number to file buffer...
//appendToBuffer(file_num);
sout.write(Convert_To_Byte(size));
}
catch (Exception ex) {
ex.printStackTrace();
}
//increase buffer size by 4
Buffer_Size = Buffer_Size + 4;
byte b[] = getByteFromFile(fileName);
//Add content of file to buffer....
//appendToBuffer(b);
try {
//Add file number to file buffer...
//appendToBuffer(file_num);
sout.write(b);
} catch (Exception ex) {
ex.printStackTrace();
}
//increase buffer size by file size
Buffer_Size = Buffer_Size + size;
System.out.println("Total Buffer Size So Far :: " + Buffer_Size);
}
try {
sout.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}// End of Function Process File
public int getFileSize(String FileName)
{
return (int)new File(FileName).length();
}
public byte [] Convert_To_Byte(int value)
{
//value = 255;
byte [] bytes = new byte[4];
int totalValue = 0;
for(int k = 3; k >= 0; k--) {
bytes[k] = new Integer(value).byteValue();
System.out.println(k + " :::::: " + bytes[k]);
value = value >> 8;
int reverse = (int)bytes[k];
int convert = 0;
for(int i = 0; i < 8; i++) {
convert += ((reverse >> i) & 1) << i;
}
totalValue += convert << (3 - k) * 8;
System.out.println("Value of byte " + convert);
}
System.out.println("Total Value of byte " + totalValue);
return bytes;
}
public void appendToBuffer(byte [] data )
{
int j=0;
for(int i= Buffer_Size; i < Buffer_Size + data.length; i++)
{
fileBuffer[i] = data[j++];
}
}
}
--
ఇట్లు మీ,
చంద్రశేఖర్.
No comments:
Post a Comment