All pastes #1531041 Raw Edit

H3R3T1C

public java v1 · immutable
#1531041 ·published 2009-08-16 01:42 UTC
rendered paste body
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package html_test;import java.io.File;/** * * @author H3R3T1C */public class Packet {    public Packet()    {    }    public String creatHeader(int type,long size,String file_name,String f_type, String user)// type of data | Size of data | If its a file data sends    {                                                                                     // file name else its a null byte | if file sends file type                                                                                          // based on int value (may change to string)        String tmp="";                                                                    // user is string for now but will possibly change        String padding="00";// used as a "null" byte                                           // user is gotten from list of users in room        if(!file_name.isEmpty())        tmp=numToHex(type)+padding+            numToHex(size)+padding+            StringtoHex(file_name)+            StringtoHex(f_type)+            StringtoHex(user);        else        tmp=numToHex(type)+padding+            numToHex(size)+padding+            StringtoHex(file_name)+            StringtoHex(f_type)+            StringtoHex(user);        return tmp;    }    public byte[] createFilePacket(File file)// converts files into byte code for sending over internet    {        byte[] data={0};// whole thing = 0 (null) byte        return data;    }    public byte[] createStringPacket(String string)// a diff method needs to be applyed    {        byte nul=0;        byte[] tmp;                return string.getBytes();    }    public String StringtoHex(String data)    {        char[] chars = data.toCharArray();        StringBuffer output = new StringBuffer();        byte padding=0;        for(int i = 0; i < chars.length; i++){        output.append(Integer.toHexString((int)chars[i]));        }        output.append(Integer.toHexString((int)padding));        output.append(Integer.toHexString((int)padding));        return output.toString();    }    public String hexToString(String data)    {        char[] chars = data.toCharArray();        int z=0;        String output="";        byte a=00;        String tmp="";        for(int i = 0; i < chars.length; i+=2){            tmp=""+chars[i]+chars[i+1];            z=Integer.parseInt(tmp,16);            output+=(char)z;        }        return output;   }    public String decodeHeader(String data)    {        String output="";        String tmp="";        int loc=0;        char[] chars=data.toCharArray();        for(int a=0;a<2;a++)        {            for(int i=loc;i<data.length();i+=2)// decodes type and size            {                if((char)chars[i]=='0'&&(char)chars[i+1]=='0')                {                    loc=i+2;                    break;                }                else                {                    tmp+=""+(char)chars[i]+(char)chars[i+1];                }            }            output+=hexToNum(tmp)+" ";            tmp="";        }        for(int a=0;a<4;a++)        {            for(int i=loc;i<data.length();i+=2)            {                if(chars[i]=='0'&&chars[i+1]=='0')                {                    loc=i+2;                    break;                }                else                {                    tmp+=""+chars[i]+chars[i+1];                }            }            output+=hexToString(tmp)+" ";            tmp="";        }        return output;    }    public String numToHex(int a)    {        String tmp="";        tmp=Integer.toHexString(a);        if(tmp.length()%2!=0)        {            tmp="0"+tmp;        }        return tmp;    }    public String numToHex(long a)    {        String tmp="";        tmp=Integer.toHexString((int)a);        if(tmp.length()%2!=0)        {            tmp="0"+tmp;        }        return tmp;    }    public String hexToNum(String data)    {        return ""+Integer.parseInt(data, 16);    }}