package com.dbxml.xml.dtsm;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: DocumentTableEntry.java,v 1.2 2004/02/12 00:17:58 bradford Exp $
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.dbxml.util.ByteArray;

/**
 * DocumentTableEntry
 */

public final class DocumentTableEntry {
   public static final int SIZE = 5;

   private byte typeID;
   private int valID;

   public DocumentTableEntry() {
   }

   public DocumentTableEntry(byte typeID, int valID) {
      this.typeID = typeID;
      this.valID = valID;
   }

   public DocumentTableEntry(byte[] bytes, int offset) {
      readFrom(bytes, offset);
   }

   public DocumentTableEntry(InputStream is) throws IOException {
      readFrom(is);
   }

   /**
    * getTypeID returns the Type ID associated with this
    * DocumentTableEntry.
    *
    * @see com.dbxml.xml.dtsm.Constants
    *
    * @return The Type ID
    */
   public byte getTypeID() {
      return typeID;
   }

   /**
    * setTypeID sets the Type ID associated with this
    * DocumentTableEntry.
    *
    * @see com.dbxml.xml.dtsm.Constants
    *
    * @param typeID The Type ID
    */
   public void setTypeID(byte typeID) {
      this.typeID = typeID;
   }

   /**
    * getValID returns the Value ID associated with this
    * DocumentTableEntry.  The Value ID may be a ValueEntry
    * index or a SymbolTable index depending on the Type
    * ID.
    *
    * @return The Value ID
    */
   public int getValID() {
      return valID;
   }

   /**
    * setValID sets the Value ID associated with this
    * DocumentTableEntry.  The Value ID may be a ValueEntry
    * index or a SymbolTable index depending on the Type
    * ID.
    *
    * @param valID The Value ID
    */
   public void setValID(int valID) {
      this.valID = valID;
   }

   public void readFrom(byte[] bytes, int offset) {
      this.typeID = bytes[offset];
      valID = ByteArray.readInt(bytes, offset+1);
   }

   public void readFrom(InputStream is) throws IOException {
      typeID = (byte)is.read();
      int ch1 = is.read();
      int ch2 = is.read();
      int ch3 = is.read();
      int ch4 = is.read();
      valID = (ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0);
   }

   public void writeTo(byte[] bytes, int offset) {
      bytes[offset] = typeID;
      ByteArray.writeInt(bytes, offset+1, valID);
   }

   public void writeTo(OutputStream os) throws IOException {
      os.write(typeID);
      os.write((valID >>> 24) & 0xFF);
      os.write((valID >>> 16) & 0xFF);
      os.write((valID >>> 8) & 0xFF);
      os.write((valID >>> 0) & 0xFF);
   }

   public byte[] getBytes() {
      byte[] b = new byte[SIZE];
      writeTo(b, 0);
      return b;
   }
}
