1
+ package org .korben .coderising .download .impl ;
2
+
3
+ import java .io .ByteArrayOutputStream ;
4
+ import java .io .IOException ;
5
+ import java .io .InputStream ;
6
+ import java .net .HttpURLConnection ;
7
+ import org .korben .coderising .download .api .Connection ;
8
+
9
+ public class ConnectionImpl implements Connection {
10
+
11
+ private static final int BUFFER_SIZE = 4096 ;
12
+ private HttpURLConnection httpConn ;
13
+ private String fileUrl ;
14
+ private InputStream inputStream ;
15
+
16
+ public ConnectionImpl (HttpURLConnection httpConn , String fileUrl ) {
17
+ this .httpConn = httpConn ;
18
+ this .fileUrl = fileUrl ;
19
+ }
20
+
21
+ @ Override
22
+ public byte [] read (int startPos , int endPos ) throws IOException {
23
+ if (endPos < startPos ) {
24
+ throw new IllegalArgumentException ("argument endPos[" + endPos + "] less than startPos[" + startPos + "]" );
25
+ }
26
+ int bytesNeed2Read = endPos - startPos + 1 ;
27
+ if (bytesNeed2Read > getContentLength ()) {
28
+ throw new IllegalArgumentException (
29
+ "endPos[" + endPos + "] is bigger than content length[" + getContentLength () + "]" );
30
+ }
31
+
32
+ inputStream = httpConn .getInputStream ();
33
+
34
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
35
+ byte [] buffer = new byte [Math .min (bytesNeed2Read , BUFFER_SIZE )];
36
+ int read ;
37
+
38
+ long startTime = System .currentTimeMillis ();
39
+ final long progressInterval = 2000 ;
40
+ while ((read = inputStream .read (buffer )) != -1 ) {
41
+ byteArrayOutputStream .write (buffer , 0 , read );
42
+
43
+ if (System .currentTimeMillis () - startTime > progressInterval ) {
44
+ startTime = System .currentTimeMillis ();
45
+ System .out .println (String .format (Thread .currentThread ().getName () +
46
+ " [%.2f%%]" , byteArrayOutputStream .size () * 100.0 / bytesNeed2Read )
47
+ );
48
+ }
49
+ }
50
+ System .out .println (String .format (Thread .currentThread ().getName () + " [%.2f%%]" , 100.0 ));
51
+ System .out .println ("bytes read: " + byteArrayOutputStream .size ());
52
+
53
+ return byteArrayOutputStream .toByteArray ();
54
+ }
55
+
56
+ @ Override
57
+ public int getContentLength () {
58
+ if (httpConn != null ) {
59
+ return httpConn .getContentLength ();
60
+ }
61
+ return 0 ;
62
+ }
63
+
64
+ @ Override
65
+ public void close () {
66
+ if (inputStream != null ) {
67
+ try {
68
+ inputStream .close ();
69
+ } catch (IOException e ) {
70
+ e .printStackTrace ();
71
+ }
72
+ }
73
+ if (httpConn != null ) {
74
+ httpConn .disconnect ();
75
+ }
76
+ }
77
+
78
+ @ Override
79
+ public String getFileName () {
80
+ String disposition = httpConn .getHeaderField ("Content-Disposition" );
81
+ if (disposition != null ) {
82
+ // extracts file name from header field
83
+ int index = disposition .indexOf ("filename=" );
84
+ if (index > 0 ) {
85
+ return disposition .substring (index + 10 ,
86
+ disposition .length () - 1 );
87
+ }
88
+ }
89
+ // extracts file name from URL
90
+ return fileUrl .substring (fileUrl .lastIndexOf ("/" ) + 1 ,
91
+ fileUrl .length ());
92
+ }
93
+ }
0 commit comments