/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2002 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.util;

import java.io.*;
import java.sql.Blob;
import java.sql.SQLException;
import com.silverwrist.dynamo.iface.DataItem;

public class StaticDataItem implements DataItem, Blob
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_name;
  private String m_type;
  private int m_length;
  private byte[] m_data;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public StaticDataItem(String name, String type, int length, byte[] data)
  {
    m_name = name;
    m_type = type;
    m_length = length;
    m_data = data;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface DataItem
   *--------------------------------------------------------------------------------
   */

  public String getName()
  {
    return m_name;

  } // end getName

  public String getMimeType()
  {
    return m_type;

  } // end getMimeType

  public int getSize()
  {
    return m_length;

  } // end getSize

  public InputStream getDataStream() throws IOException
  {
    return new ByteArrayInputStream(m_data);

  } // end getDataStream

  public Blob getBlob() throws SQLException
  {
    return (Blob)this;

  } // end getBlob

  /*--------------------------------------------------------------------------------
   * Implementations from interface Blob
   *--------------------------------------------------------------------------------
   */

  public long length()
  {
    return (long)m_length;

  } // end length

  public byte[] getBytes(long pos, int length) throws SQLException
  {
    int real_length = Math.min(length,(int)(m_data.length - pos));
    byte[] rc = new byte[real_length];
    System.arraycopy(m_data,(int)pos,rc,0,real_length);
    return rc;

  } // end getBytes

  public InputStream getBinaryStream() throws SQLException
  {
    return new ByteArrayInputStream(m_data);

  } // end getBinaryStream

  public long position(byte[] pattern, long start) throws SQLException
  {
    throw new SQLException("method not implemented");

  } // end position

  public long position(Blob pattern, long start) throws SQLException
  {
    return position(pattern.getBytes(0,(int)(pattern.length())),start);

  } // end position

  public int setBytes(long pos, byte[] bytes) throws SQLException
  {
    throw new SQLException("read-only Blob value");

  } // end setBytes

  public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException
  {
    throw new SQLException("read-only Blob value");

  } // end setBytes

  public OutputStream setBinaryStream(long pos) throws SQLException
  {
    throw new SQLException("read-only Blob value");

  } // end setBinaryStream

  public void truncate(long len) throws SQLException
  {
    throw new SQLException("read-only Blob value");

  } // end truncate

} // end class StaticDataItem



