package org.csource.fastdfs;

import java.io.IOException;
import java.io.OutputStream;
import org.csource.fastdfs.DownloadCallback;

public class DownloadStream implements DownloadCallback
{
	// 定义输出流
	private OutputStream out;
	// 定义当前字节
	private long currentBytes = 0;
	
	// 构造函数，传入输出流
	public DownloadStream(OutputStream out)
	{
		super();
		this.out = out;
	}


	public int recv(long fileSize, byte[] data, int bytes)
	{
		try
		{
			// 将数据写入输出流
			out.write(data, 0, bytes);
		}
		catch(IOException ex)
		{
			ex.printStackTrace(); 
			return -1;
		}
		
		// 更新当前字节
		currentBytes +=	bytes;
		// 如果当前字节等于文件大小，则重置当前字节
		if (this.currentBytes == fileSize)
		{
			this.currentBytes = 0;
		}
		
		return 0;
	}
}
