All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.roncoo.fastdfs.DownloadStream Maven / Gradle / Ivy

The newest version!
package com.roncoo.fastdfs;

import java.io.IOException;
import java.io.OutputStream;

/**
 * Download file by stream (download callback class)
 *
 * @author zhouzezhong
 * @author Happy Fish
 * @author YuQing
 * @version Version 1.11
 */
public class DownloadStream implements DownloadCallback {
	private OutputStream out;
	private long currentBytes = 0;

	public DownloadStream(OutputStream out) {
		super();
		this.out = out;
	}

	/**
	 * recv file content callback function, may be called more than once when the file downloaded
	 *
	 * @param fileSize file size
	 * @param data     data buff
	 * @param bytes    data bytes
	 * @return 0 success, return none zero(errno) if fail
	 */
	@Override
	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;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy