1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-04 15:45:25 +02:00

[162954] [api] Provide a way to get InputStream and OutputStream from file services. Added a getInputStream() and getOutputStream() to IFileService and implemented for SSH, FTP and Local. Dstore needs to be implemented.

This commit is contained in:
Kushal Munir 2007-02-20 04:17:18 +00:00
parent 21a7758101
commit ae03da8d1c
6 changed files with 252 additions and 5 deletions

View file

@ -22,6 +22,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.ArrayList;
@ -1313,4 +1314,20 @@ public class DStoreFileService extends AbstractDStoreService implements IFileSer
return remoteEncoding;
}
/* (non-Javadoc)
* @see org.eclipse.rse.services.files.IFileService#getInputStream(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String, boolean)
*/
public InputStream getInputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.eclipse.rse.services.files.IFileService#getOutputStream(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String, boolean)
*/
public OutputStream getOutputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException {
// TODO Auto-generated method stub
return null;
}
}

View file

@ -1070,6 +1070,83 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
return result;
}
/**
* Gets the input stream to access the contents of a remote file.
* @since 2.0
* @see org.eclipse.rse.services.files.AbstractFileService#getInputStream(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String, boolean)
*/
public InputStream getInputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException {
if (monitor != null){
if (monitor.isCanceled()) {
return null;
}
}
FTPClient ftpClient = getFTPClient();
InputStream stream = null;
try {
remoteParent = adaptPath(remoteParent);
ftpClient.changeWorkingDirectory(remoteParent);
if (isBinary) {
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
}
else {
ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
}
stream = ftpClient.retrieveFileStream(remoteFile);
}
catch (Exception e) {
throw new RemoteFileIOException(e);
}
return stream;
}
/**
* Gets the output stream to write to a remote file.
* @since 2.0
* @see org.eclipse.rse.services.files.AbstractFileService#getOutputStream(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String, boolean)
*/
public OutputStream getOutputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException {
if (monitor != null){
if (monitor.isCanceled()) {
return null;
}
}
FTPClient ftpClient = getFTPClient();
OutputStream stream = null;
try {
remoteParent = adaptPath(remoteParent);
ftpClient.changeWorkingDirectory(remoteParent);
if (isBinary) {
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
}
else {
ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
}
stream = ftpClient.storeFileStream(remoteFile);
}
catch (Exception e) {
throw new RemoteFileIOException(e);
}
return stream;
}
}

View file

@ -27,6 +27,7 @@ import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
@ -50,6 +51,7 @@ import org.eclipse.rse.services.files.AbstractFileService;
import org.eclipse.rse.services.files.IFileService;
import org.eclipse.rse.services.files.IHostFile;
import org.eclipse.rse.services.files.RemoteFileException;
import org.eclipse.rse.services.files.RemoteFileIOException;
public class LocalFileService extends AbstractFileService implements IFileService, ILocalService
{
@ -1364,5 +1366,43 @@ public class LocalFileService extends AbstractFileService implements IFileServic
}
}
/**
* Gets the input stream to access the contents of a remote file.
* @since 2.0
* @see org.eclipse.rse.services.files.AbstractFileService#getInputStream(IProgressMonitor, String, String, boolean)
*/
public InputStream getInputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException {
File file = new File(remoteParent, remoteFile);
InputStream stream = null;
try {
stream = new FileInputStream(file);
}
catch (Exception e) {
throw new RemoteFileIOException(e);
}
return stream;
}
/**
* Gets the output stream to write to a remote file.
* @since 2.0
* @see org.eclipse.rse.services.files.AbstractFileService#getOutputStream(IProgressMonitor, String, String, boolean)
*/
public OutputStream getOutputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException {
File file = new File(remoteParent, remoteFile);
OutputStream stream = null;
try {
stream = new FileOutputStream(file);
}
catch (Exception e) {
throw new RemoteFileIOException(e);
}
return stream;
}
}

View file

@ -7,7 +7,8 @@
*
* Contributors:
* Martin Oberhuber (Wind River) - initial API and implementation
* Dave Dykstal (IBM) - fixing bug 162510: correctly process filter strings
* Dave Dykstal (IBM) - fixing bug 162510: correctly process filter strings
* Kushal Munir (IBM) - for API bug
*******************************************************************************/
package org.eclipse.rse.internal.services.ssh.files;
@ -765,4 +766,71 @@ public class SftpFileService extends AbstractFileService implements IFileService
return false;
}
}
/**
* Gets the input stream to access the contents of a remote file. Clients should call {@link #disconnect()} after closing the input stream.
* @since 2.0
* @see org.eclipse.rse.services.files.AbstractFileService#getInputStream(IProgressMonitor, String, String, boolean)
*/
public InputStream getInputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException {
InputStream stream = null;
try {
String remotePath = remoteParent + '/' + remoteFile;
int mode = ChannelSftp.OVERWRITE;
MyProgressMonitor sftpMonitor = new MyProgressMonitor(monitor);
getChannel("SftpFileService.getInputStream " + remoteFile); //check the session is healthy //$NON-NLS-1$
ChannelSftp channel = (ChannelSftp)fSessionProvider.getSession().openChannel("sftp"); //$NON-NLS-1$
channel.connect();
stream = channel.get(remotePath, sftpMonitor, mode);
Activator.trace("SftpFileService.getInputStream " + remoteFile + " ok"); //$NON-NLS-1$ //$NON-NLS-2$
}
catch (Exception e) {
Activator.trace("SftpFileService.getInputStream " + remoteFile + " failed: " + e.toString()); //$NON-NLS-1$ //$NON-NLS-2$
throw makeSystemMessageException(e);
}
return stream;
}
/**
* Gets the output stream to write to a remote file. Clients should call {@link #disconnect()} after closing the output stream.
* @since 2.0
* @see org.eclipse.rse.services.files.AbstractFileService#getOutputStream(IProgressMonitor, String, String, boolean)
*/
public OutputStream getOutputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
OutputStream stream = null;
try {
SftpProgressMonitor sftpMonitor = new MyProgressMonitor(monitor);
int mode = ChannelSftp.OVERWRITE;
String dst = remoteParent;
if (remoteFile != null) {
if (!dst.endsWith("/")) { //$NON-NLS-1$
dst += '/';
}
dst += remoteFile;
}
getChannel("SftpFileService.getOutputStream " + remoteFile); //check the session is healthy //$NON-NLS-1$
ChannelSftp channel = (ChannelSftp)fSessionProvider.getSession().openChannel("sftp"); //$NON-NLS-1$
channel.connect();
stream = channel.put(dst, sftpMonitor, mode);
Activator.trace("SftpFileService.getOutputStream " + remoteFile + " ok"); //$NON-NLS-1$ //$NON-NLS-2$
}
catch (Exception e) {
Activator.trace("SftpFileService.getOutputStream " + remoteFile + " failed: " + e.toString()); //$NON-NLS-1$ //$NON-NLS-2$
throw makeSystemMessageException(e);
}
return stream;
}
}

View file

@ -16,6 +16,9 @@
package org.eclipse.rse.services.files;
import java.io.InputStream;
import java.io.OutputStream;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.rse.services.clientserver.messages.SystemMessage;
@ -102,4 +105,20 @@ public abstract class AbstractFileService implements IFileService
public String getEncoding(IProgressMonitor monitor) throws SystemMessageException {
return System.getProperty("file.encoding"); //$NON-NLS-1$
}
/**
* The default implementation returns <code>null</code>. Clients can override to return an input stream to the file.
* @see org.eclipse.rse.services.files.IFileService#getInputStream(IProgressMonitor, String, String, boolean)
*/
public InputStream getInputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException {
return null;
}
/**
* The default implementation returns <code>null</code>. Clients can override to return an output stream to the file.
* @see org.eclipse.rse.services.files.IFileService#getOutputStream(IProgressMonitor, String, String, boolean)
*/
public OutputStream getOutputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException {
return null;
}
}

View file

@ -18,8 +18,10 @@ package org.eclipse.rse.services.files;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.rse.services.IService;
import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
@ -293,4 +295,28 @@ public interface IFileService extends IService
* @since 2.0
*/
public String getEncoding(IProgressMonitor monitor) throws SystemMessageException;
/**
* Gets the input stream to access the contents a remote file. Clients should close the input stream when done.
* @param monitor the progress monitor.
* @param remoteParent the absolute path of the parent.
* @param remoteFile the name of the remote file.
* @param isBinary <code>true</code> if the file is a binary file, <code>false</code> otherwise.
* @return the input stream to access the contents of the remote file.
* @throws SystemMessageException if an error occurs.
* @since 2.0
*/
public InputStream getInputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException;
/**
* Gets the output stream to write to a remote file. Clients should close the output stream when done.
* @param monitor the progress monitor.
* @param remoteParent the absolute path of the parent.
* @param remoteFile the name of the remote file.
* @param isBinary <code>true</code> if the file is a binary file, <code>false</code> otherwise.
* @return the input stream to access the contents of the remote file.
* @throws SystemMessageException if an error occurs.
* @since 2.0
*/
public OutputStream getOutputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException;
}