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(new URL(url).openConnection(), 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 1 computer(s): tvejysmllsmz

No comments. add comment

Snippet ID: #1008030
Snippet name: loadBinaryPageToFile
Eternal ID of this version: #1008030/6
Text MD5: ee695f6663c8da81f93b6c124e8e899a
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-04-21 01:26:51
Source code size: 1068 bytes / 36 lines
Pitched / IR pitched: No / No
Views / Downloads: 12 / 1
Version history: 5 change(s)
Referenced in: [show]