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: ValueTableEntry.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;

/**
 * ValueTableEntry
 */

public final class ValueTableEntry {
   public static final int SIZE = 8;

   private int offset;
   private int len;

   public ValueTableEntry() {
   }

   public ValueTableEntry(int offset, int len) {
      this.offset = offset;
      this.len = len;
   }

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

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

   /**
    * getOffset returns the offset into the data stream for
    * this Value.
    *
    * @return The Offset
    */
   public int getOffset() {
      return offset;
   }

   /**
    * setOffset sets the offset into the data stream for
    * this Value.
    *
    * @param offset The Offset
    */
   public void setOffset(int offset) {
      this.offset = offset;
   }

   /**
    * getLength returns the length of the Value in the data
    * stream.
    *
    * @return The Length
    */
   public int getLength() {
      return len;
   }

   /**
    * setLength sets the length of the Value in the data
    * stream.
    *
    * @param len The Length
    */
   public void setLength(int len) {
      this.len = len;
   }

   public void readFrom(byte[] bytes, int off) {
      offset = ByteArray.readInt(bytes, off);
      len = ByteArray.readInt(bytes, off+4);
   }

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

   public void writeTo(byte[] bytes, int off) {
      ByteArray.writeInt(bytes, off, offset);
      ByteArray.writeInt(bytes, off+4, len);
   }

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

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