Exemplo n.º 1
0
  /** Download InputStream, use FileCreator to create output file */
  private static boolean download(InputStream in, FileCreator creator, String libname) {
    boolean success = false;
    java.io.BufferedOutputStream bout = creator.create(libname);
    if (bout == null) {
      System.err.println("Not possible to create a file for downloading");
      creator.done(success);
      return success;
    }

    BufferedInputStream bin = new BufferedInputStream(in);
    byte data[] = new byte[BUF_SZ];

    while (true) {
      int count = 0;
      try {
        count = bin.read(data, 0, BUF_SZ);
      } catch (IOException e) {
        System.err.println(e);
        creator.done(success);
        return success;
      }
      if (count == -1) {
        break;
      }

      try {
        bout.write(data, 0, count);
      } catch (IOException e) {
        System.err.println(e);
        creator.done(success);
        return success;
      }
      //          break;
    }

    try {
      bout.close();
    } catch (IOException e) {
      System.err.println(e);
      creator.done(success);
      return success;
    }

    success = true;
    creator.done(success);
    return success;
  }