DownloadStream.java 1012 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package com.kmall.common.fileserver.fastdfs;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. /**
  5. * Download file by stream (download callback class)
  6. *
  7. * @author zhouzezhong & Happy Fish / YuQing
  8. * @version Version 1.11
  9. */
  10. public class DownloadStream implements DownloadCallback {
  11. private OutputStream out;
  12. private long currentBytes = 0;
  13. public DownloadStream(OutputStream out) {
  14. super();
  15. this.out = out;
  16. }
  17. /**
  18. * recv file content callback function, may be called more than once when the file downloaded
  19. *
  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. try {
  27. out.write(data, 0, bytes);
  28. } catch (IOException ex) {
  29. ex.printStackTrace();
  30. return -1;
  31. }
  32. currentBytes += bytes;
  33. if (this.currentBytes == fileSize) {
  34. this.currentBytes = 0;
  35. }
  36. return 0;
  37. }
  38. }