Exemplo n.º 1
0
  /** decode singlepart article */
  public void decodeSinglepart(NntpArticleHeader header) {
    header.setStatus(NntpArticleHeader.STATUS_DECODING);
    YDecoder yDec = new YDecoder();
    File cachefile =
        new File(
            cacheDir
                + File.separator
                + header.getServer()
                + "."
                + header.getGroup()
                + "."
                + header.getID());
    try {
      FileInputStream fis = new FileInputStream(cachefile);
      if (!yDec.checkYenc(cachefile)) {
        throw new NotYencException();
      }
      File path = new File(downloadDir + File.separator + header.getGroup());
      checkPath(path);
      File output = new File(path + File.separator + yDec.getFilename(cachefile));
      FileOutputStream fos = new FileOutputStream(output);
      yDec.setInputStream(fis);
      yDec.setOutputStream(fos);

      // now call decode
      try {
        yDec.decode();
        fos.flush();
        fos.close();
      } catch (IOException ioe) {
        log.warn("io exception: " + ioe.getMessage());
        output.delete();
        fos.close();
        fis.close();
        cleanupCache(header);
        return;
      }
    } catch (NotYencException nye) {
      File path = new File(downloadDir + File.separator + header.getGroup());
      checkPath(path);
      uudecodeSinglepart(header, cachefile);
    } catch (IOException ioe) {
      log.warn("IO Exception: " + ioe.getMessage());
      ioe.printStackTrace();
      return;
    }

    cleanupCache(header);

    // header.setStatus(NntpArticleHeader.STATUS_READ);
    // should generate HeaderChangeEvent here
    log.debug("total memory after decoding " + Runtime.getRuntime().totalMemory());
  }
Exemplo n.º 2
0
  /** decode multipart article */
  public void decodeMultipart(NntpArticleHeader header) {
    header.setStatus(NntpArticleHeader.STATUS_DECODING);
    YDecoder yDec = new YDecoder();
    try {
      // order our parts
      NntpArticlePartID[] ids = header.getParts();
      int[] orderparts = new int[ids.length];
      for (int x = 0; x < ids.length; x++) {
        NntpArticlePartID partID = ids[x];
        File myfile = this.getCacheFile(header, partID);
        if (!yDec.checkYenc(myfile) || yDec.getPartNumber(myfile) == -1) {
          throw new NotYencException();
        }
        orderparts[yDec.getPartNumber(myfile) - 1] = x;
      }

      // first part ?
      NntpArticlePartID firstPart = ids[orderparts[0]];
      File firstFile = this.getCacheFile(header, firstPart);
      FileInputStream fis = new FileInputStream(firstFile);

      // test if yEnc
      if (yDec.getFilename(firstFile) == null) {
        log.error("couldn't get yEnc filename");
        throw new NotYencException();
      }
      String filename = yDec.getFilename(firstFile);
      fis.close();

      // create the path to our target
      File path = new File(downloadDir + File.separator + header.getGroup());
      checkPath(path);

      // create the target file in the path
      File target = new File(path + File.separator + filename);
      FileOutputStream fos = new FileOutputStream(target);
      yDec.setOutputStream(fos);

      // now decode in order
      // log.debug("decoding:  " + header.getSubject());
      long timer = System.currentTimeMillis();
      for (int n = 0; n < orderparts.length; n++) {
        NntpArticlePartID currentPart = ids[orderparts[n]];
        File myfile = this.getCacheFile(header, currentPart);
        fis = new FileInputStream(myfile);
        yDec.setInputStream(fis);
        try {
          yDec.decode();
        } catch (IOException ioe) {
          log.error("io exception: " + ioe.getMessage());
          cleanupCache(header);
          target.delete();
          return;
        } catch (Exception wth) {
          log.error("what the hell happened downloading article?" + wth.getMessage());
          cleanupCache(header);
          target.delete();
          return;
        } finally {
          fis.close();
        }
      } // end of for

      log.debug("decoded file in : " + ((System.currentTimeMillis() - timer) / 1000));
      // log.info("finished downloading multipart article");
      // close our target file resource
      cleanupCache(header);
      fos.flush();
      fos.close();
      header.setStatus(NntpArticleHeader.STATUS_READ);
    } catch (NotYencException nye) {
      log.debug("not a yEnc encoding, possibly uuencoded " + nye.toString());
      long timer = System.currentTimeMillis();
      uudecodeMultipart(header);
      log.debug("decoded file in : " + ((System.currentTimeMillis() - timer) / 1000));
      cleanupCache(header);
      header.setStatus(NntpArticleHeader.STATUS_READ);
      return;
    } catch (IOException ioe) {
      log.error("ioe exception caught " + ioe.getMessage());
      cleanupCache(header);
      return;
    } catch (ArrayIndexOutOfBoundsException oobe) {
      log.error("array oobe while sorting yenc parts: " + oobe.getMessage());
      cleanupCache(header);
      return;
    }
  }