/*
 * Copyright (c) 2006-2008 MiniMe. Code released under The MIT/X Window System
 * License. Full license text can be found in license.txt
 */
package minime;

import java.io.*;

import minime.core.Event;
import minime.core.EventDispatcher;
/**
 * The Util class is a utility class, it is used to handle stream, string
 * information.
 * 
 * @author aaa
 * 
 */
public class Util
{
    static final Logger LOG = Logger.getLogger("minime.Util");
    static private char[] HEXA_DIGIT = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'} ;

    /**
     * Reads a line string from reader
     * 
     * @param reader
     *            the inputstreamReader instance.
     * @return a string
     * @throws IOException
     *             If an I/O error occurs
     */
    public static String readLine(InputStreamReader reader) throws IOException
    {
        int c = reader.read();
        if (c == -1)
            return null;

        StringBuffer buf = new StringBuffer();
        while (c != -1 )
        {
        	buf.append((char) c);        	
        	if( c == '\n')
        		break;        		
            c = reader.read();
        }
        return buf.toString();
    }

    private final static int DATA_IN_BUFFER_SIZE = 512;

    static private String b = "";

    /**
     * Gets the resource data with given resource file path.
     * 
     * @param path
     *            the resource file path
     * @return the resource data
     * @throws IOException
     *             If an I/O error occurs
     */
    public static byte[] loadFromJar(String path) throws IOException
    {
        LOG.debug("loading " + path + " from jar");
        byte[] read = new byte[DATA_IN_BUFFER_SIZE];
        InputStream in = null;
        try
        {
            in = getInpustream(path);
            ByteArrayOutputStream buf = new ByteArrayOutputStream();

            while (true)
            {
                int len = in.read(read, 0, DATA_IN_BUFFER_SIZE);
                if (len == -1)
                {
                    break;
                }
                buf.write(read, 0, len);
            }
            return buf.toByteArray();
        }
        finally
        {
            if (in != null)
            {
                in.close();
            }
        }

    }

    /**
     * Read the data into an inputstream from resource file and returns this
     * inputStream object.
     * 
     * @param path
     *            the resource file path
     * @return an inputstream object
     * @throws IOException
     *             If an I/O error occurs
     */
    public static InputStream getInpustream(String path) throws IOException
    {
        return b.getClass().getResourceAsStream(path);
    }

    /**
     * Extract the name value from a line string.
     * 
     * @param line
     *            the string
     * @return the name value
     */
    public static String[] extractNameValue(String line)
    {
        line = line.trim();
        int shp = line.indexOf('#');
        if (shp != -1)
        {
            line = line.substring(0, shp);
        }

        int eq = line.indexOf('=');
        if (eq != -1)
        {
            String name = line.substring(0, eq).trim();
            String value = line.substring(eq + 1).trim();
            String values[] = new String[2];
            int comma = value.indexOf(',');
            if(comma != -1)
            {
            	values[0] = value.substring(0,comma);
            	values[1] = value.substring(comma+1).trim(); 
            }else
            {
            	values[0] = value;
            	values[1] = name;
            }

            return new String[] { name, values[0], values[1]};
        }
        return null;
    }

    public static String convertByteArrayToString(byte []array)
    {
    	byte[] result= new byte[array.length*2] ;
    	int index=0 ;
    	for(int i=0;i<array.length;i++)
    	{	
    		byte b= (byte)(0x0F & (byte)((array[i] & (byte)0xF0)>>4)) ;
    		result[index++] = (byte)HEXA_DIGIT[b] ;
    		b=(byte)(array[i] & (byte)0x0F) ;
    		result[index++] = (byte)HEXA_DIGIT[b] ;    			
    	}
    	
    	return new String(result) ;
    }
    
    public static String shortToString(short v)
    {
    	return Integer.toString((v&0xFFFF)+0x10000,16).substring(1).toUpperCase() ;
    }
    public static void  sendDelayTimeEvent(int delayTime)
    {
    	EventDispatcher.getInstance().fire(
				Event.createEvent(Event.TIMER_EVENT, 0,
						0, new Long(delayTime), delayTime));
    }
}
