Package net.mzalewski.goblin.transfermanagers

Source Code of net.mzalewski.goblin.transfermanagers.FileDownload

package net.mzalewski.goblin.transfermanagers;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import net.mzalewski.goblin.mailbox.GMail;
import net.mzalewski.goblin.mailbox.IMailBox;
import net.mzalewski.goblin.mailbox.MailHeader;
import net.mzalewski.goblin.moorhunt.mail.Servers;

public class FileDownload {
  private String user;
  private String passwd;
  private IMailBox mailBox;
  private char[] crc;
  private int currentPiece;
  private int totalPieces;
  private MailHeader[] mhs;
  private BufferedInputStream input;
  private FileOutputStream fos;
 
  protected FileDownload(String user, String passwd, Servers server, char[] crc, String filePath,
      int currentPiece, int totalPieces) {
    this.user = user;
    this.passwd = passwd;   
   
    if (server == Servers.GMail)
      mailBox = new GMail();
   
    for (int i = 0; i < 4; ++i)
      this.crc = crc.clone();
   
    try {
      fos = new FileOutputStream(filePath, true);
    } catch (FileNotFoundException e) {};
    this.currentPiece = currentPiece;
    this.totalPieces = totalPieces;
  }
 
  public boolean connect() {
    return mailBox.login(user, passwd);
  }
 
  private String byteToHex(char num) {
    String hex = Integer.toHexString(num);
    while (hex.length() < 2) {
      hex = '0' + hex;
    }
   
    return hex;
  }
 
  public boolean download(int byteCount) {
    if (mhs == null) {
      mhs = mailBox.getMailList();
     
      int empty = 0;
     
      for (int i = 0; i < mhs.length; ++i) {
        int beg = mhs[i].strSubject.indexOf("[");
        int end = mhs[i].strSubject.indexOf("]");
        if ((beg != -1) && (end != -1)) {
          String checksum = new String();
          checksum = byteToHex(crc[0]) + byteToHex(crc[1]) + byteToHex(crc[2])
            + byteToHex(crc[3]);
          String str = mhs[i].strSubject.substring(beg + 1, end);
          if (str.compareToIgnoreCase(checksum) != 0) {
            mhs[i] = null;
            ++empty;
          }
        }
        else {
          mhs[i] = null;
          ++empty;
        }
      }
     
      MailHeader[] mhl = new MailHeader[mhs.length - empty];
      for (int i = 0, j = 0; i < mhs.length; ++i) {
        if (mhs[i] != null) {
          mhl[j] = new MailHeader();
          mhl[j].strLink = mhs[i].strLink;
          mhl[j].strSubject = mhs[i].strSubject;
         
          ++j;
        }
      }
     
      mhs = mhl;
    }
   
    if (input == null)
      input = new BufferedInputStream(mailBox.download(mhs[currentPiece]));
   
    try {
      int c;
      byte[] buffer = new byte[8192];
      byte[] buf = new byte[8192];
      c = input.read(buffer, 0, 8192);
      for (int i = 0; i < c; ++i)
        buf[i] = (byte)buffer[i];
     
      if (c == -1) {
        input = null;
        ++currentPiece;
        if (currentPiece > totalPieces) {
          fos.close();
          return false;
        }
      }
      else   
        fos.write(buf, 0, c);
    } catch (IOException e) {};
   
    return true;
  }
}
TOP

Related Classes of net.mzalewski.goblin.transfermanagers.FileDownload

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.