/*******************************************************************************
 *  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.baselibs;

import java.io.IOException;

import cdac.medinfo.sdk.dcm30_04.commoninterface.IDicomStream;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidDataTypeException;
import cdac.medinfo.sdk.dcm30_04.exceptions.ValidationException;





/**
 * This class is the Super Class of all the Byte Datatypes.
 * It provides implementation for read from stream and write to stream functions for all its sub classes.
 */
public class DataTypeByte extends DicomValue  
{
	


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	byte[] buffer;

	/**
	 * Sets the length of the value in DicomValue. This length represents the actual value length
	 * and not the one which includes padding bytes' length.
	 * @param lMaxLength max length of Dicomdata 
	 */
	public void setMaxLength(long lMaxLength)
	{

	}

	/**
	 * Retrieves maximum allowed length by this datatype.
	 * @return long - max length of Datatype
	 */
	public long getMaxLength()
	{
		return 0;
	}

	/**
	 * Validates the data value object as per the encoding rules provided in the DICOM Standard. 
	 * @param value object representing value to be validated.
	 * @return boolean - status indicating success/failure of the validation.
	 * @throws ValidationException If validation fails according to Dicom rules.
	 */
	public boolean validate(Object value)
	{
		return true;
	}

	/**
	 * Validates the data value object as per the encoding rules provided in the DICOM Standard. 
	 * @param value object representing value to be validated.
	 * @return boolean - status indicating success/failure of the validation.
	 * @throws ValidationException If validation fails according to Dicom rules.
	 */
	public boolean validate()
	{
		return validate(value);
	}

	/**
	 * Reads the data value from the stream of specified length of bytes.
	 * @param objIDicomStream isntance of class implementing the IDicomStream interface.
	 * @param iOBLength Number of bytes to be read from the Stream.
	 * @return boolean - status if read was successful.
	 * @throws InvalidDataTypeException If DataType validation fails.
	 * @throws IOException If Input/Output operation fails.
	 */
	public boolean read(IDicomStream objIDicomStream,int iOBLength) throws InvalidDataTypeException, IOException
	{
		if(iOBLength > 0)
		{
			buffer = new byte[iOBLength];
			objIDicomStream.readBuffer(buffer);
			setValue(buffer);
		}
		return true;
	}

	/**
	 * This method writes the Data Value to the given Stream.
	 * @param objIDicomStream object of the stream onto which the DataTypeString is to be written.
	 * @return integer - status indicating success/failure of the write method.
	 * @throws IOException If Input/Output operation fails.
	 */
	public int write(IDicomStream objIDicomStream) throws IOException
	{
		if(buffer != null)
			objIDicomStream.writeBuffer(buffer,0,buffer.length-1);
		return 0;	
	}

	/**
	 * Retrieves the actual value length set in the DataTypeString. This length doesn't include the padding bytes.
	 * @return int - length of Dicomdata
	 */
	public int getValueLength()
	{
		if(buffer != null)
			return buffer.length;
		else
			return 0;
	}

	/**
	 * 	Dumps the value of Data Type Byte in readable form to the specified Stream.
	 * 	@param objIDicomStream - the Stream on which the Data is to be written.
	 * @throws IOException - Exception is thrown indicating runtime failure of writing operation.
	 */
	public void dumpDICOMInfo(IDicomStream objIDicomStream) throws IOException
	{
	}
}
