DownloadStream.java 944 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package org.csource.fastdfs;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. /**
  5. * Download file by stream (download callback class)
  6. * @author zhouzezhong & Happy Fish / YuQing
  7. * @version Version 1.11
  8. */
  9. public class DownloadStream implements DownloadCallback
  10. {
  11. private OutputStream out;
  12. private long currentBytes = 0;
  13. public DownloadStream(OutputStream out)
  14. {
  15. super();
  16. this.out = out;
  17. }
  18. /**
  19. * recv file content callback function, may be called more than once when the file downloaded
  20. * @param fileSize file size
  21. * @param data data buff
  22. * @param bytes data bytes
  23. * @return 0 success, return none zero(errno) if fail
  24. */
  25. public int recv(long fileSize, byte[] data, int bytes)
  26. {
  27. try
  28. {
  29. out.write(data, 0, bytes);
  30. }
  31. catch(IOException ex)
  32. {
  33. ex.printStackTrace();
  34. return -1;
  35. }
  36. currentBytes += bytes;
  37. if (this.currentBytes == fileSize)
  38. {
  39. this.currentBytes = 0;
  40. }
  41. return 0;
  42. }
  43. }