package org.jerrymouse.cc.site.store.ftp;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.jerrymouse.cc.site.store.StoreManager;

public class FtpStoreManager extends StoreManager {

	FtpStoreConfig ftpFileConfig;

	@Override
	public byte[] getBytes(String filename) {
		byte[] b = null;
		try {
			b = IOUtils.toByteArray(getInputStream(filename));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b;
	}

	public InputStream getInputStream(String filename) {
		FTPClient ftpClient = new FTPClient();
		InputStream in = null;
		try {
			ftpClient.connect(ftpFileConfig.getHost());
			ftpClient.login(ftpFileConfig.getUsername(), ftpFileConfig
					.getPassword());
			ftpClient.changeWorkingDirectory(ftpFileConfig.getPath());
			in = new BufferedInputStream(
					ftpClient.retrieveFileStream(filename), ftpClient
							.getBufferSize());
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return in;
	}

	public void setFtpFileConfig(FtpStoreConfig ftpFileConfig) {
		this.ftpFileConfig = ftpFileConfig;
	}

	@Override
	public void putBytes(String filename, byte[] bytes) {
		// TODO Auto-generated method stub

	}

}
