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

35
LINES

< > TinyBrain | #1000837 - loadBinaryPage current

JavaX fragment (include)

static ThreadLocal<Map<S, L<S>>> loadBinaryPage_responseHeaders = new ThreadLocal;
static ThreadLocal<SS> loadBinaryPage_extraHeaders = new ThreadLocal;

static byte[] loadBinaryPage(String url) ctex {
  print("Loading " + url);
  return loadBinaryPage(loadPage_openConnection(new URL(url)));
}

static byte[] loadBinaryPage(URLConnection con) ctex {
  SS extraHeaders = getAndClearThreadLocal(loadBinaryPage_extraHeaders);
  setHeaders(con);
  for (S key : keys(extraHeaders))
    con.setRequestProperty(key, extraHeaders.get(key));
  ret loadBinaryPage_noHeaders(con);
}

static byte[] loadBinaryPage_noHeaders(URLConnection con) ctex {
  ByteArrayOutputStream buf = new ByteArrayOutputStream();
  InputStream inputStream = con.getInputStream();
  loadBinaryPage_responseHeaders.set(con.getHeaderFields());
  long len = 0;
  try { len = con.getContentLength/*Long*/(); } 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.");
  }
  inputStream.close();
  return buf.toByteArray();
}

Author comment

Began life as a copy of #2000466

download  show line numbers  debug dex   

Travelled to 5 computer(s): cfunsshuasjs, gwrvuhgaqvyk, mqqgnosmbjvj, onxytkatvevr, tvejysmllsmz

No comments. add comment

Snippet ID: #1000837
Snippet name: loadBinaryPage current
Eternal ID of this version: #1000837/10
Text MD5: 5c6dd64f2b9d51cf7c4bdd7b21f78ffb
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-04 17:35:47
Source code size: 1218 bytes / 35 lines
Pitched / IR pitched: No / No
Views / Downloads: 258 / 3112
Version history: 9 change(s)
Referenced in: [show]