PHP code converting to Java code (help)
I am looking for java code that will insert url parameters within flash lite 1.1 swf file.
But I only found that function in PHP code. As I tried to convert it to a Java code, I was lost on some parts. I couldn't able to work it successfully.

e.g. url path: http://example.com/swf?param1=1&param2=2

using php, there's no problem, but again I need to work it in a java code as I am using google app engine. And also I already tried using quercus and it works fine in localhost but unfortunately it doesn't work when I deploy it to google app engine server. with that, I have to successfully work it through java code. Maybe someone out there know how to convert it to java code correctly.

Here's the php code.

    <?php
    
    function h32($sizeint){
    	return pack("V",$sizeint);
    }
    function h16($sizeint){
    	return pack("v",$sizeint);
    }
    
    function calctaglen($dataarray){
    	$ret = 0;
    	foreach($dataarray as $key => $value){
    	$ret += strlen($key)+strlen($value) + 11;
    
    	}
    	return $ret+1;
    }
    function maketag($dataarray){
    	$tag = "\x3f\x03";
    	$taglen = calctaglen($dataarray);
    	$tag .= h32($taglen);
    
    	foreach($dataarray as $key => $value){
    		$tag .= "\x96".h16(strlen($key)+2)."\x00".$key."\x00";
    		$tag .= "\x96".h16(strlen($value)+2)."\x00".$value."\x00";
    		$tag .= "\x1d";
    	}
    	$tag .= "\x00";
    	return $tag;
    }
    
    $doactiontag = maketag($HTTP_GET_VARS);
    
    $srcswf = "sample.swf";
    
    $fr = fopen($srcswf,"rb");
    
    $headtmp = fread($fr,9);
    $rb = ord(substr($headtmp,8,1))>>3; // rectbit
    $headlen = ceil(((( 8 - (($rb*4+5)&7) )&7)+ $rb*4 + 5 )/8) + 12 + 5;
    $head = $headtmp.fread($fr,$headlen-9);
    
    $oldsize = filesize($srcswf);
    $newsize = $oldsize+strlen($doactiontag);
    $newhead = substr($head,0,4).h32($newsize).substr($head,8);
    
    $tail = fread($fr, $oldsize-$headlen);
    fclose($fr);
    
    header("Content-Type: application/x-shockwave-flash");
    print $newhead.$doactiontag.$tail;
    
    ?>


and here's the java servlet I made.


    public class SwfServlet extends HttpServlet {
    
        /** logger */
        protected static final Logger logger = Logger.getLogger(SwfServlet.class.getName());
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
            doGetPost(req, res);
        }
    
        public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
            doGetPost(req, res);
        }
    
        private void doGetPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("application/x-shockwave-flash");
            response.setHeader("Content-Type", "application/x-shockwave-flash");
    
            File f = new File("sample.swf");
    
            // Read file
            FileInputStream fis = null;
            OutputStream out = null;
            try {
                out = response.getOutputStream();
                fis = new FileInputStream(f);
    
               out = response.getOutputStream();
                fis = new FileInputStream(f);
                byte[] headtmp = new byte[9];
                fis.read(headtmp);
                
                //
                for (int n = 0; n < headtmp.length; n++) {
                    logger.info("headtmp data[" + n + "]: " + headtmp[n]);
                }
                String headtmpStr = new String(headtmp);
                logger.info("head string : " + headtmpStr);
                int rb = headtmp[8] >> 3;
                long headlen = Math.round(Math.ceil((((8 - ((rb * 4 + 5) & 7)) & 7) + rb * 4 + 5) / 8)) + 12 + 5;
                int headsize = (int) headlen - 9;
    
                byte[] sampleTag = getParamTag(request);
                logger.info("Head length : " + headlen);
                long oldsize = f.length();
                long newsize = oldsize + sampleTag.length;
    
                logger.info("New size : " + newsize);
                // write the new length
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(bos);
                dos.writeLong(newsize);
                // etc.
                byte[] b = bos.toByteArray();
                byte[] newhead = new byte[headtmp.length + b.length];
                System.arraycopy(headtmp, 0, newhead, 0, 4);
                System.arraycopy(b, 0, newhead, 5, b.length);
                newhead[newhead.length - 1] = headtmp[headtmp.length - 1];
                
                out.write(newhead); // write new head
                byte[] headSizebyte = new byte[headsize];
                fis.read(headSizebyte);
                out.write(headSizebyte);
                out.write(sampleTag); // write the parameters
                int content = 0;
                // write the remaining contents
                while ((content = fis.read()) != -1) {
                    out.write(content);
                }
                
                fis.close();
                out.close();
            } catch (IOException ex) {
                logger.log(Level.SEVERE, null, ex);
            } finally {
    
            }
        }
    
        protected byte[] getParamsTag(HttpServletRequest req) {
            byte[] sample = new byte[4];
            sample[0] = 63;
            sample[1] = 3;
            // process the parameters here. can't process it correctly.
            // return 0 as no params yet.
            sample[2] = 0;
            sample[3] = 0;
            return sample;
        }
    
    }
PHP code converting to Java code (help)
===

I am looking for java code that will insert url parameters within flash lite 1.1 swf file.
But I only found that function in PHP code. As I tried to convert it to a Java code, I was lost on some parts. I couldn't able to work it successfully.

e.g. url path: http://example.com/swf?param1=1&param2=2

using php, there's no problem, but again I need to work it in a java code as I am using google app engine. And also I already tried using quercus and it works fine in localhost but unfortunately it doesn't work when I deploy it to google app engine server. with that, I have to successfully work it through java code. Maybe someone out there know how to convert it to java code correctly.

Here's the php code.

    <?php
    
    function h32($sizeint){
    	return pack("V",$sizeint);
    }
    function h16($sizeint){
    	return pack("v",$sizeint);
    }
    
    function calctaglen($dataarray){
    	$ret = 0;
    	foreach($dataarray as $key => $value){
    	$ret += strlen($key)+strlen($value) + 11;
    
    	}
    	return $ret+1;
    }
    function maketag($dataarray){
    	$tag = "\x3f\x03";
    	$taglen = calctaglen($dataarray);
    	$tag .= h32($taglen);
    
    	foreach($dataarray as $key => $value){
    		$tag .= "\x96".h16(strlen($key)+2)."\x00".$key."\x00";
    		$tag .= "\x96".h16(strlen($value)+2)."\x00".$value."\x00";
    		$tag .= "\x1d";
    	}
    	$tag .= "\x00";
    	return $tag;
    }
    
    $doactiontag = maketag($HTTP_GET_VARS);
    
    $srcswf = "sample.swf";
    
    $fr = fopen($srcswf,"rb");
    
    $headtmp = fread($fr,9);
    $rb = ord(substr($headtmp,8,1))>>3; // rectbit
    $headlen = ceil(((( 8 - (($rb*4+5)&7) )&7)+ $rb*4 + 5 )/8) + 12 + 5;
    $head = $headtmp.fread($fr,$headlen-9);
    
    $oldsize = filesize($srcswf);
    $newsize = $oldsize+strlen($doactiontag);
    $newhead = substr($head,0,4).h32($newsize).substr($head,8);
    
    $tail = fread($fr, $oldsize-$headlen);
    fclose($fr);
    
    header("Content-Type: application/x-shockwave-flash");
    print $newhead.$doactiontag.$tail;
    
    ?>


and here's the java servlet I made.


    public class SwfServlet extends HttpServlet {
    
        /** logger */
        protected static final Logger logger = Logger.getLogger(SwfServlet.class.getName());
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
            doGetPost(req, res);
        }
    
        public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
            doGetPost(req, res);
        }
    
        private void doGetPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("application/x-shockwave-flash");
            response.setHeader("Content-Type", "application/x-shockwave-flash");
    
            File f = new File("sample.swf");
    
            // Read file
            FileInputStream fis = null;
            OutputStream out = null;
            try {
                out = response.getOutputStream();
                fis = new FileInputStream(f);
    
               out = response.getOutputStream();
                fis = new FileInputStream(f);
                byte[] headtmp = new byte[9];
                fis.read(headtmp);
                
                //
                for (int n = 0; n < headtmp.length; n++) {
                    logger.info("headtmp data[" + n + "]: " + headtmp[n]);
                }
                String headtmpStr = new String(headtmp);
                logger.info("head string : " + headtmpStr);
                int rb = headtmp[8] >> 3;
                long headlen = Math.round(Math.ceil((((8 - ((rb * 4 + 5) & 7)) & 7) + rb * 4 + 5) / 8)) + 12 + 5;
                int headsize = (int) headlen - 9;
    
                byte[] sampleTag = getParamTag(request);
                logger.info("Head length : " + headlen);
                long oldsize = f.length();
                long newsize = oldsize + sampleTag.length;
    
                logger.info("New size : " + newsize);
                // write the new length
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(bos);
                dos.writeLong(newsize);
                // etc.
                byte[] b = bos.toByteArray();
                byte[] newhead = new byte[headtmp.length + b.length];
                System.arraycopy(headtmp, 0, newhead, 0, 4);
                System.arraycopy(b, 0, newhead, 5, b.length);
                newhead[newhead.length - 1] = headtmp[headtmp.length - 1];
                
                out.write(newhead); // write new head
                byte[] headSizebyte = new byte[headsize];
                fis.read(headSizebyte);
                out.write(headSizebyte);
                out.write(sampleTag); // write the parameters
                int content = 0;
                // write the remaining contents
                while ((content = fis.read()) != -1) {
                    out.write(content);
                }
                
                fis.close();
                out.close();
            } catch (IOException ex) {
                logger.log(Level.SEVERE, null, ex);
            } finally {
    
            }
        }
    
        protected byte[] getParamsTag(HttpServletRequest req) {
            byte[] sample = new byte[4];
            sample[0] = 63;
            sample[1] = 3;
            // process the parameters here. can't process it correctly.
            // return 0 as no params yet.
            sample[2] = 0;
            sample[3] = 0;
            return sample;
        }
    
    }