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

34
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.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.");
  }
  inputStream.close();
  return buf.toByteArray();
}

Author comment

Began life as a copy of #2000466

download  show line numbers  debug dex   

Travelled to 3 computer(s): cfunsshuasjs, onxytkatvevr, tvejysmllsmz

No comments. add comment

Snippet ID: #1000837
Snippet name: loadBinaryPage current
Eternal ID of this version: #1000837/8
Text MD5: c768fe2d7d56c0a401d65298d96e9a4e
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-12-08 00:21:54
Source code size: 1212 bytes / 34 lines
Pitched / IR pitched: No / No
Views / Downloads: 219 / 3051
Version history: 7 change(s)
Referenced in: [show]