/* 9P Protocol JAVA
 * P9P/type/stat9P.java
 * Autor: Jaime Garzon (2006) jgarzon@gmail.com
 *
 * Esta clase es una implementacion del tipo de dato
 * stat, usado en el protocolo 9P de Plan9P, espeficicado
 * en la seccion 5 del manual de Plan 9: "Plan 9 File Protocol"
 * Los metodos Write y Read, permiten escribir el valor
 * de la forma little-endian en un Buffer o leerlo de un
 * array de bytes, respectivamente.
*/

package P9P.type;
import java.io.*;

public class stat9P extends type9P {

   public int9P mode=new int9P();      //permissions and flags
   public int9P atime=new int9P();     //last access time
   public int9P mtime=new int9P();     //last modification time
   public long9P length=new long9P();  //length of file in bytes

   public short9P type=new short9P();
   public int9P dev=new int9P();

   public String9P muid=new String9P();   //user who last modified the file
   public String9P name=new String9P();   //last element of path
   public String9P uid=new String9P();    //owner name
   public String9P gid=new String9P();    //group name

   /* CONSTRUCTORES */

   public qid9P qid=new qid9P();


   /* METODOS */

   public void read(byte[] b, int i) {
      short9P size = new short9P(b,i);

      type.read(b,i+=4);
      dev.read(b,i+=2);
      qid.read(b,i+=4);
      mode.read(b,i+=13);
      atime.read(b,i+=4);
      mtime.read(b,i+=4);
      length.read(b,i+=4);
      name.read(b,i+=8);
      uid.read(b,i+=name.len());
      gid.read(b,i+=uid.len());
      muid.read(b,i+gid.len());
   }

   public void write(ByteArrayOutputStream buf) {
      short9P size = new short9P(len()-2);

      size.write(buf);
      type.write(buf);
      dev.write(buf);
      qid.write(buf);
      mode.write(buf);
      atime.write(buf);
      mtime.write(buf);
      length.write(buf);
      name.write(buf);
      uid.write(buf);
      gid.write(buf);
      muid.write(buf);
   }

   public int len() {
      return
         type.len()  + dev.len()   + qid.len()    + mode.len() +
         atime.len() + mtime.len() + length.len() + name.len() +
         uid.len()   + gid.len()   + muid.len()   + 2;
   }

   public String toString() {
      return "STAT"+" ";
   }
}