Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

36
LINES

< > TinyBrain | #1008030 - loadBinaryPageToFile

JavaX fragment (include)

static void loadBinaryPageToFile(S url, File file) ctex {
  print("Loading " + url);
  loadBinaryPageToFile(openConnection(new URL(url)), file);
}

static void loadBinaryPageToFile(URLConnection con, File file) ctex {
  setHeaders(con);
  loadBinaryPageToFile_noHeaders(con, file);
}

static void loadBinaryPageToFile_noHeaders(URLConnection con, File file) ctex {
  File ftemp = new File(f2s(file) + "_temp");
  FileOutputStream buf = newFileOutputStream(mkdirsFor(ftemp));
  InputStream inputStream = con.getInputStream();
  try {
    long len = 0;
    try { len = con.getContentLengthLong(); } catch (Throwable e) { printStackTrace(e); }
    int n = 0;
    while (true) {
      int ch = inputStream.read();
      if (ch < 0)
        break;
      buf.write(ch);
      if (++n % 100000 == 0)
        println("  " + n + (len != 0 ? "/" + len : "") + " bytes loaded.");
    }
    buf.close();
    buf = null;
    file.delete();
    ftemp.renameTo(file);
    inputStream.close();
  } finally {
    if (buf != null) buf.close();
  }
}

Author comment

Began life as a copy of #1000837

download  show line numbers  debug dex   

Travelled to 2 computer(s): cfunsshuasjs, tvejysmllsmz

No comments. add comment

Snippet ID: #1008030
Snippet name: loadBinaryPageToFile
Eternal ID of this version: #1008030/7
Text MD5: 422354746be5cdc2c2bdb4262eecdb03
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-07-25 17:59:16
Source code size: 1067 bytes / 36 lines
Pitched / IR pitched: No / No
Views / Downloads: 62 / 72
Version history: 6 change(s)
Referenced in: [show]