Dear Sir,
Thanks for ur previous replies.Sir , I had gone through the java.io source code and for BufferedInputStream it is specified that this stream by default specifies 2048 bytes.Sir can i override this method and provide more buffering.Can i apply the same for BufferedOutputStream
Following is the source code of VFSIOService class which does reading and writing:
public class VFSIOService {
/**
* This is the constant which holds the byte size
* which can be used while reading and writing.
*/
public static final int BYTE_SIZE = 1024;
/**
* This is the constant which represents start offset in the array
*/
public static final int START_OFFSET = 0;
/**
* This is the constuctor .
*/
public VFSIOService() {
}
/**
* This method writes the file to the hard disk.
* This method calls the security service if the vault type is secure
*
* @param FileTOThe File object contains file related details
* @return void
* @throws VFSException
*/
public void addFile(FileTO fileTO) throws VFSException
{
//create the file object from absolute path
File file = null;
File fileParentPath = null;
BufferedOutputStream bout = null;
file = new File(fileTO.getAbsolutePath());
//get the parent path of the file
fileParentPath = new File(file.getParent());
//check parent path exists
if (fileParentPath.exists() == false)
{
//create directories
fileParentPath.mkdirs();
}
try
{
// Check the vault type
if (fileTO.getVaultType() == StorageTypeConst.TYPE_SECURE)
{
//get the security facade home
SecurityFacadeHome securityServiceHome = (SecurityFacadeHome)
ServiceLocator.getHome(JNDIConst.JNDI_SECURITYFACADE_HOME,
SecurityFacadeHome.class);
//get the remote reference
SecurityFacade securityServiceremote = securityServiceHome.create();
// call the encode method
securityServiceremote.encode(fileTO);
}
else if (fileTO.getVaultType() ==
StorageTypeConst.TYPE_REGULAR)
{
// create buffered output stream to the file
bout = new BufferedOutputStream(new
FileOutputStream(file),BYTE_SIZE);
//write the bytes to the file
bout.write(fileTO.getFileBytes(),
START_OFFSET,fileTO.getFileBytes().length);
}
else
{
//delete the above created folder
fileParentPath.delete();
// throw exception for invalid valut type
throw new VFSException(ExceptionMsg.VFS.FILE_CREATION_ERROR);
}
}
catch(ServiceLocatorException exception)
{
throw new VFSException(ExceptionMsg.VFS.FILE_CREATION_ERROR);
}
catch(SecurityException exception)
{
throw new VFSException(ExceptionMsg.VFS.FILE_CREATION_ERROR);
}
catch(CreateException exception)
{
throw new VFSException(ExceptionMsg.VFS.FILE_CREATION_ERROR);
}
catch(FileNotFoundException exception)
{
throw new VFSException(ExceptionMsg.VFS.FILE_CREATION_ERROR);
}
catch(IOException exception)
{
// deleting the destination file
if(file != null)
file.delete();
throw new VFSException(ExceptionMsg.VFS.FILE_CREATION_ERROR);
}
finally
{
try{
if(bout != null)
bout.close();
}
catch(IOException exception)
{
// deleting the destination file
if(file != null)
file.delete();
throw new VFSException(ExceptionMsg.VFS.FILE_CREATION_ERROR);
}
}
}
/**
* This method writes the file to the hard disk and it calls
* the security service if the vault type is secure. This method is used when
* the file has to be overwritten on the disk
*
* @param FileTOThe File object contains file related details
* @param originalFilePathThe path of existing file which is to be deleted
* @return void
* @throws VFSException
*/
public void addFile(FileTO fileTO,
String originalFilePath)
throws VFSException
{
try{
// call the above addFile method
addFile(fileTO);
//Check the absolutePath and originalFilePath are not same
if(!fileTO.getAbsolutePath().trim().equals(originalFilePath.trim()))
{
if(deleteFile(originalFilePath) == false )
{
throw new VFSException(ExceptionMsg.VFS.FILE_CREATION_ERROR);
}
}
}
catch(VFSException exception)
{
throw new VFSException(ExceptionMsg.VFS.FILE_CREATION_ERROR);
}
}
/**
* This method deletes the original file from the hard disk.
*
* @param originalFilePathThe original file path which is to be deleted
* @return boolean
*/
private boolean deleteFile(String originalFilePath)
{
File file = new File(originalFilePath );
//returns true if delete is successful else false
return file.delete();
}
/**
* This method reads the file from the hard disk.
* This method calls the security service if the vault type is secure
*
* @param FileTOThe File object contains file related details
* @return FileTO
* @throws VFSException
*/
public FileTO downLoadFile(FileTO fileTO) throws VFSException
{
BufferedInputStream bis = null;
try
{
//create file object from absolute path
File file = new File(fileTO.getAbsolutePath());
// Check for file existence
if( !file.exists() )
{
throw new VFSException(ExceptionMsg.VFS.FILE_DOWNLOAD_ERROR);
}
else
{
// Check the vault type
if (fileTO.getVaultType() == StorageTypeConst.TYPE_SECURE)
{
//get the security facade home
SecurityFacadeHome securityServiceHome = (SecurityFacadeHome)
ServiceLocator.getHome(JNDIConst.JNDI_SECURITYFACADE_HOME,
SecurityFacadeHome.class);
//get the remote reference
SecurityFacade securityServiceRemote =
securityServiceHome.create();
fileTO = securityServiceRemote.decode(fileTO);
}
else if (fileTO.getVaultType() ==
StorageTypeConst.TYPE_REGULAR)
{
bis = null;
// create input and output stream
bis = new BufferedInputStream(new
FileInputStream(file));
// create the byte array of file length
byte fileBytes[] = new byte[(int)file.length()];
// read the file and copy its contents into array
bis.read(fileBytes,START_OFFSET,fileBytes.length);
// set the file bytes read to fileTO file bytes
fileTO.setFileBytes(fileBytes);
}
else
{
// throw exception if vault type is invalid
throw new VFSException(ExceptionMsg.VFS.INVALID_VAULT_TYPE);
}
}//end of else
return fileTO;
}
catch(SecurityException exception)
{
throw new VFSException(ExceptionMsg.VFS.FILE_DOWNLOAD_ERROR);
}
catch(ServiceLocatorException exception)
{
throw new VFSException(ExceptionMsg.VFS.FILE_DOWNLOAD_ERROR);
}
catch(FileNotFoundException exception)
{
throw new VFSException(ExceptionMsg.VFS.FILE_DOWNLOAD_ERROR);
}
catch(IOException exception){
throw new VFSException(ExceptionMsg.
VFS.FILE_DOWNLOAD_ERROR);
}
catch(CreateException exception)
{
throw new VFSException(ExceptionMsg.
VFS.FILE_DOWNLOAD_ERROR);
}
finally
{
try{
if(bis != null)
bis.close();
}catch(IOException exception){
throw new VFSException(ExceptionMsg
.VFS.FILE_DOWNLOAD_ERROR);
}
}
}
}