
import java.io.*;
import java.util.*;

public class ParseRequestData{
   private String fname;
   private String boundary;
	
   String param;
   DataInputStream in;
   String str = new String();
   public ParseRequestData(InputStream instream)
   {
	this.in=new DataInputStream(instream);
	
	try{
           boundary=in.readLine();
           str = boundary;
	}catch(IOException e){
	   System.out.println(e.getMessage());
	   in=null;
	}
   }
  // 델리미터를 넘긴다.
   public String getDelimeter()
   {
	return boundary;
   }
   public String getDelimeter(InputStream instream)
   {
	this.in=new DataInputStream(instream);
	try{
	   boundary=in.readLine();
	}catch(IOException e){
	   System.out.println(e.getMessage());
	   in=null;
	}

	return boundary;
   }

   // 변수에대한 값을 읽어낸다.
   public String getParameter(String in_name)throws IOException
   {
	
	while(str!=null){
	   if(str.indexOf("name=") != -1)
	   {
		int nS=str.indexOf("name=");
		int nE=str.indexOf("\"",nS+6);
		param=str.substring(nS+6,nE);

		if(param.equals(in_name))	
		{
			StringBuffer temp = new StringBuffer();
			Vector sv = new Vector();
			String s = new String();
			
			s=in.readLine();
			while(true){
				
				s=in.readLine();
				
				StringTokenizer st = new StringTokenizer(s,":");
				String t = new String();
				
				if(st.hasMoreTokens()){
				    t = st.nextToken();
				    System.out.println("t = " + t);
				    if(t.equals("Content-Disposition")){
				        break;
				    }else{
				        sv.addElement(s);
				    }
				}else{	
				    sv.addElement(s);				   
				}
							
			}
			System.out.println("sv size = " + sv.size());
			for(int i=0;i<sv.size() -1;i++){
			      temp.append((String)sv.elementAt(i));
			      if(sv.size() >2)
				temp.append(";");
			         
			}
			
			str = s;
			s = temp.toString();
			return s;
		}
	    }
	    str = in.readLine();
        }
	return null;
   }

   public String[] saveFile(String filepath, String option)
   {
   	
   	
   	Vector fnames = new Vector();
   	String[] fnameArray = null;
   	String fname;
	
	
	int nS;
	int nE;
	
	
	try{
	   while(str !=null){
	       if(str.indexOf("filename=\"\"") != -1)
	       {
		  str=in.readLine();
		  return null;
	       }
	       
	       if(str.indexOf("filename=") != -1)
	       {
	   	
		    nS=str.indexOf("filename=");
		    nE=str.indexOf("\"",nS+10);
		    fname=str.substring(nS+10,nE);
		    if(fname.lastIndexOf("\\") != -1){
			fname=fname.substring(fname.lastIndexOf("\\")+1);
			fname= new String(fname.getBytes("ISO-8859-1"),"KSC5601");
			fnames.addElement(fname);
			
			File file = null;
			
			if((option != null) && (!option.equals(""))){
	    	           file = new File(filepath,option + "-" + fname) ;
	    	        }else{
	    	           file = new File(filepath,fname) ;
	    	        }
	    	           
		        FileOutputStream OutFile=new FileOutputStream(file);
		        UpFile(OutFile);			
				OutFile.close();
			
		    }
		
	       }   
	       str = in.readLine();
	    }
	 
	    fnameArray = new String[fnames.size()];
	    for(int i=0; i < fnames.size(); i++){
		fnameArray[i] = (String)(fnames.elementAt(i));
	    }
	
    	}catch(NullPointerException e){
    	}catch(IOException ioe){
    		System.out.println("IOException happened!!!");
        }finally{
	    fnameArray = new String[fnames.size()];
	    for(int i=0; i < fnames.size(); i++){
		fnameArray[i] = (String)(fnames.elementAt(i));
	    }
	    return fnameArray;
	}    	
	

   }
   public boolean UpFile(OutputStream Out)throws IOException
   {
	String str;
	while((str=in.readLine())!=null)
	{
	   if(str.indexOf("Content-Type") != -1)
	   {
		str=in.readLine();
		if(readParameter(Out))
		return true;
	   }
	}
	return false;
   }

   public boolean readParameter( OutputStream Out ) 
   {
	byte[] buffer=new byte[1024];
	byte[] tbuffer=new byte[boundary.length()+1];
	byte tm;
	int x=0;

	try{
	   for(;;)
	   {
 		buffer[x++]=tm=in.readByte();
		if(x==boundary.length()+1)
		{
		   int y=0;
		   String temp=new String(buffer,0,x);
		   if((y=temp.indexOf(boundary))!= -1)
		   {
			x=y;
			if(x!=0)
			   Out.write(buffer,0,x-1);
			return true;
		    }
		}
		else{
		   if((x==1023) || (tm =='\n')){
			Out.write(buffer,0,x);
			x=0;
		   }
		}  //end of if/else
	   } //end of for
			
	}catch(Exception e){
	   System.out.println("Error : "+e.toString());
	}
	return false;
   }
   
   public Vector getAttachedFileNames(){

	//To fill this method
	System.out.println("getAttachedFileName() has no body.");   	
   	return new Vector();
   	}
}
