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

59
LINES

< > BotCompany Repo | #1002996 // Java Resume HTTP Test (works!)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (771L/6K/19K).

1  
!752
2  
3  
static boolean actualCopy = true;
4  
static int maxCopy = 1024*1024*1024;
5  
6  
p {
7  
  File dest = new File("/home/stefan/Downloads/Brilliance.mp4");
8  
  //print(bytesToHex(firstBytesOfFile(dest, 10)));
9  
  int downloaded = (int) dest.length();
10  
  print("Existing: " + downloaded);
11  
  
12  
  URL url = new URL("http://cdl37.convert2mp3.net/download.php?id=youtube_pvcsa2ZesZY&key=bV7xjw9SuB4X&d=y");
13  
  
14  
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
15  
  
16  
  connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
17  
  
18  
  InputStream in = connection.getInputStream();
19  
  try {
20  
    print("Length: " + connection.getContentLength());
21  
    int status = connection.getResponseCode();
22  
    print("Code: " + status);
23  
    if (status != HttpURLConnection.HTTP_PARTIAL)
24  
      fail("No partial download possible!");
25  
    
26  
    if (actualCopy) {
27  
      FileOutputStream fos = new FileOutputStream(dest, true); // append
28  
      
29  
      print("Copied: " + copyStream(downloaded, downloaded+maxCopy, in, fos));
30  
      fos.close();
31  
    } else {
32  
      new ByteArrayOutputStream bos;
33  
      print("Tested: " + copyStream(downloaded, downloaded+10, in, bos));
34  
      byte[] bytes = bos.toByteArray();
35  
      print(bytesToHex(subArray(bytes, 0, 10)));
36  
    }
37  
  } finally {    
38  
    in.close();
39  
  }
40  
}  
41  
42  
static long copyStream(int downloaded, long maxDownload, InputStream in, OutputStream out) ctex {
43  
  byte[] buf = new byte[65536];
44  
  long total = downloaded;
45  
  while (total < maxDownload) {
46  
    int n = in.read(buf);
47  
    if (n <= 0) return total;
48  
    out.write(buf, 0, n);
49  
    total += n;
50  
    print("Downloaded: " + toK(total) + " K");
51  
  }
52  
  ret total;
53  
}
54  
55  
static byte[] subArray(byte[] b, int start, int end) {
56  
  byte[] x = new byte[end-start];
57  
  System.arraycopy(b, start, x, 0, end-start);
58  
  ret x;
59  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002996
Snippet name: Java Resume HTTP Test (works!)
Eternal ID of this version: #1002996/1
Text MD5: 615eeaba6aff39690254052184b5e86e
Transpilation MD5: 767676e9cdd5eb92e0ef62e6733bf10b
Author: stefan
Category: javax / networking
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-04-17 03:04:33
Source code size: 1850 bytes / 59 lines
Pitched / IR pitched: No / No
Views / Downloads: 661 / 740
Referenced in: [show references]