/*******************************************************************************
 *  Copyright 2017 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.hl7282.hl7streams;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import cdac.medinfo.sdk.hl7282.utils.HL7Constants;
/**
 * This class is specialized in the reading/writing operations on the byte array
 */
public class HL7BufferStream extends HL7Stream 
{
	/**
	 * Sets the ByteArrayInputStream to BufferStream. 
	 * @param objByteArrayInputStream object of ByteArrayInputStream
	 * @return int - Success or Failure.
	 */
	public int setInputStream(ByteArrayInputStream objByteArrayInputStream)
	{
		if(objByteArrayInputStream == null)
			return 0;
		objInputStream = objByteArrayInputStream;
		return HL7Constants.HL7_FUNCTION_SUCCESS;
	}
	
	/**
	 * Sets the ByteArrayOutputStream to BufferStream. 
	 * @param objByteArrayOutputStream object of ByteArrayOutputStream
	 * @return int - Succes or Failure.
	 */
	public int setOutputStream(ByteArrayOutputStream objByteArrayOutputStream)
	{
		if(objByteArrayOutputStream == null)
			return 0;
		objOutputStream = objByteArrayOutputStream;
		return HL7Constants.HL7_FUNCTION_SUCCESS;
	}
	
	/**
	 * Sets ByteArrayStreams to BufferStream. 
	 * @param objByteArrayInputStream object of ByteArrayInputStream
	 * @param objByteArrayOutputStream object of ByteArrayOutputStream
	 * @return int - Success or Failure.
	 */
	public int setBufferStream(ByteArrayInputStream objByteArrayInputStream,ByteArrayOutputStream objByteArrayOutputStream)
	{
		if(objByteArrayInputStream == null || objByteArrayOutputStream == null)
			return 0;   //Throw exception here if he tries to set any one stream null
		objInputStream = objByteArrayInputStream;
		objOutputStream = objByteArrayOutputStream;
		return HL7Constants.HL7_FUNCTION_SUCCESS;
	}
	
	/**
	 * Retrieves the internal buffer of the ByteArrayOutputStream.
	 * @return byte[] - byte array of buffer
	 */
	public byte[] toByteArray()
	{
		return ((ByteArrayOutputStream)objOutputStream).toByteArray();
	}
}
