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

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.Serializable;

import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;

/**
 * A FileStream can obtain input bytes from a file  or output bytes to a file in a file system. It depends on which 
 * stream you have set into the FileStream. What files are available depends
 * on the host environment.
 *  
 * For example:
 * 
 *  To read from a file stream in which FileInputStream is set: 
 * 
 *      FileInputStream fis = new FileInputStream("t.tmp");
 *      DicomStream ds = new FileStream();
 *      ds.setInputStream(fis);
 *
 *       int i = ds.readInt();
 *       ds.close();
 *       
 *  To write to a file stream in which FileOutputStream is set: 
 * 
 *      FileOutputStream fis = new FileOutputStream("t.tmp");
 *      DicomStream ds = new FileStream();
 *      ds.setOutputStream(fis);
 *      int i = 20;
 *      ds.writeInt(i);
 *      ds.close();
 *      
 *  FileStream can act as inputstream as well as outputstream at the same time by using setStream(inpustream,outputstream method.
 *  In this case, programmer can use it for reading as well as writing also. FileStream inherits default functionality of mark, reset
 *  and skipping of bytes over the underlying input stream
 *  
 */
public class DicomFileStream extends DicomStream implements Serializable
{
	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Default Constructor for FileStream.
	 */
	public DicomFileStream()
	{

	}

	/**
	 * Sets the Input Stream.
	 * @param objFileInputStream - Object of FileInputStream.
	 * @return int - Integer value shows success.
	 */
	public int setInputStream(FileInputStream objFileInputStream)
	{
		BufferedInputStream objBufferedInputStream = null;

		if(objFileInputStream != null)
			objBufferedInputStream = new BufferedInputStream(objFileInputStream);
		objInputStream  =objBufferedInputStream;
		return DicomConstants.DCM_FUNCTION_STATUS_SUCCESS;
	}

	/**
	 * Sets the Output Stream.
	 * @param objFileOutputStream - Object of FileOutputStream.
	 * @return int - Integer value shows success.
	 */
	public int setOutputStream(FileOutputStream objFileOutputStream)
	{
		BufferedOutputStream objBufferedOutputStream = null;
		if(objFileOutputStream != null)
			objBufferedOutputStream = new BufferedOutputStream(objFileOutputStream);
		objOutputStream = objBufferedOutputStream;
		return DicomConstants.DCM_FUNCTION_STATUS_SUCCESS;
	}

	/**
	 * Sets the Stream.
	 * @param objFileInputStream - Object of FileInputStream.
	 * @param objFileOutputStream - Object of FileOutputStream.
	 * @return int - Integer value shows success.
	 */
	public int setStream(FileInputStream objFileInputStream, FileOutputStream objFileOutputStream)
	{
		BufferedInputStream objBufferedInputStream = null;
		BufferedOutputStream objBufferedOutputStream = null;
		if(objFileInputStream == null || objFileOutputStream == null)
			return DicomConstants.DCM_FUNCTION_STATUS_FAIL;   // throw exception if any of the stream is set to null
		objBufferedInputStream = new BufferedInputStream(objFileInputStream);
		objBufferedOutputStream = new BufferedOutputStream(objFileOutputStream);
		objInputStream  =objBufferedInputStream;
		objOutputStream = objBufferedOutputStream;
		return DicomConstants.DCM_FUNCTION_STATUS_SUCCESS;
	}
}
