package org.zhios.website.admin.data;

public class User {
	public byte userlen;
	public byte usernamelen;
	public byte passwordlen;
	public byte rolelen;
	public byte username[];
	public byte password[];
	public byte role[];
	public boolean setUserByString(String pusername,String ppassword,String prole){
		username=pusername.getBytes();
		password=ppassword.getBytes();
		role=prole.getBytes();
		
		Integer unlen=username.length;
		Integer pwlen=password.length;
		Integer rlen=role.length;
		if((unlen>64)||(pwlen>64)||(rlen>64)){
			username=null;
			password=null;
			role=null;
			return false;
		}
		
		usernamelen=unlen.byteValue();
		passwordlen=pwlen.byteValue();
		rolelen=rlen.byteValue();
		Integer ulen=unlen+pwlen+rlen+3;	//'3=1+1+1' is the memory space used by length bytes.
		userlen=ulen.byteValue();

		return true;
	}
	public boolean setUserByByte(byte userbuffer[]){
		if(userbuffer.length>=200) return false;
		
		Integer offset=0;
		usernamelen=userbuffer[0];
		offset++;
		int unlen=getUsernameLen();
		username=new byte[unlen];
		for(int i=0;i<unlen;i++){username[i]=userbuffer[i+offset];}
		
		offset+=unlen;	//'1' is the memory space used by length byte.
		passwordlen=userbuffer[offset];
		offset++;
		int pwlen=getPasswordLen();
		password=new byte[pwlen];
		for(int i=0;i<pwlen;i++){password[i]=userbuffer[i+offset];}
		
		offset+=pwlen;
		rolelen=userbuffer[offset];
		offset++;
		int rlen=getRoleLen();
		role=new byte[rlen];
		for(int i=0;i<rlen;i++){role[i]=userbuffer[i+offset];}
		
		offset+=rlen;
		userlen=offset.byteValue();
		
		return true;
	}
	public String getUsername(){
		return new String(username);
	}
	public String getPassword(){
		return new String(password);
	}
	public String getRole(){
		return new String(role);
	}
	public int getUsernameLen(){
		Byte len=usernamelen;
		return len.intValue();
	}
	public int getPasswordLen(){
		Byte len=passwordlen;
		return len.intValue();
	}
	public int getRoleLen(){
		Byte len=rolelen;
		return len.intValue();
	}
}
