/*******************************************************************************
 *  Copyright 2017-2018 Centre for Development of Advanced Computing(C-DAC), Pune
 *  
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package cdac.medinfo.sdk.dcm30_04.services.pdu;

import java.io.IOException;

import cdac.medinfo.sdk.dcm30_04.commoninterface.IDicomStream;
import cdac.medinfo.sdk.dcm30_04.exceptions.ParseException;




/**
 * This class represents Presentation Data Values (PDV).
 */
public class PDV  
{

	/** The header. */
	byte header = 0x0;

	/** The fragment. */
	byte[] fragment;

	/**
	 * This function sets the fragment segment provided by the user.
	 * @param fragment byte array of fragment.
	 */
	public void setFragment(byte[] fragment)
	{
		this.fragment = fragment;
	}

	/**
	 * This function returns the fragment provided by the user.
	 * @return byte[] - of Fragment.
	 */
	public byte[] getFragment()
	{
		return fragment;
	}

	/**
	 * This function returns the type of fragment i.e. whether it is command
	 * fragment or data fragment. If the first bit of header is one then it is
	 * command fragment otherwise it is data fragment.
	 * @return int - Integer value of type of fragment.
	 */
	public int getFragmentType()
	{
		if((header & 0x1) == 0x1)
			return 1;
		else
			return 0;
	}

	/**
	 * This function returns whether fragment is last fragment.
	 * @return boolean - true if fragment is last fragment. false if fragment is
	 *         not last fragment.
	 */
	public boolean isLast()
	{
		if((header & 0x2) == 0x2)
			return true;
		else
			return false;
	}

	/**
	 * This function sets the type of fragment i.e. whether it is command
	 * fragment or data fragment.
	 * @param iFragmentType integer value of type of fragment
	 */
	public void setFragmentType(int iFragmentType)
	{
		if(iFragmentType == 1)
			header |= 0x1;
		else
			header |= 0x0;
	}

	/**
	 * This function returns whether fragment is last fragment.
	 */
	public void setIsLast()
	{
		header |= 0x2;
	}

	/**
	 * The implementation of this method parses the PDV Element from the given
	 * Stream.
	 * @param objIDicomStream  Stream on which the PDV is available.
	 * @param length integer value of PDV length to be parsed.
	 * @throws IOException Signals that an IO Exception has occurred.
	 * @throws ParseException Signals that an Parse Exception has occurred.
	 */
	public void parse(IDicomStream objIDicomStream, int length)throws IOException,ParseException
	{
		header = objIDicomStream.readByte();
		//length-1 because 1 byte is for header.
		fragment = new byte[length-1];
		objIDicomStream.readBuffer(fragment);
	}

	/**
	 * The implementation of this method serializes the PDV Element from the
	 * given Stream.
	 * @param objIDicomStream  Stream on which the PDV is available.
	 * @param length integer value of PDV length to be parsed.
	 * @throws IOException Signals that an IO Exception has occurred.
	 */
	public void serialize(IDicomStream objIDicomStream,int length) throws IOException
	{
		objIDicomStream.writeByte(header);
		//length-1 because 1 byte is for header.
		objIDicomStream.writeBuffer(fragment, 0, length-1);
	}
}
