33import android .text .TextUtils ;
44import android .util .Log ;
55
6+ import com .danikula .videocache .sourcestorage .SourceInfoStorage ;
7+ import com .danikula .videocache .sourcestorage .SourceInfoStorageFactory ;
8+
69import java .io .BufferedInputStream ;
710import java .io .IOException ;
811import java .io .InputStream ;
912import java .io .InterruptedIOException ;
1013import java .net .HttpURLConnection ;
1114import java .net .URL ;
1215
16+ import static com .danikula .videocache .Preconditions .checkNotNull ;
1317import static com .danikula .videocache .ProxyCacheUtils .DEFAULT_BUFFER_SIZE ;
1418import static com .danikula .videocache .ProxyCacheUtils .LOG_TAG ;
1519import static java .net .HttpURLConnection .HTTP_MOVED_PERM ;
2630public class HttpUrlSource implements Source {
2731
2832 private static final int MAX_REDIRECTS = 5 ;
29- public final String url ;
33+ private final SourceInfoStorage sourceInfoStorage ;
34+ private SourceInfo sourceInfo ;
3035 private HttpURLConnection connection ;
3136 private InputStream inputStream ;
32- private volatile int length = Integer .MIN_VALUE ;
33- private volatile String mime ;
3437
3538 public HttpUrlSource (String url ) {
36- this (url , ProxyCacheUtils . getSupposablyMime ( url ));
39+ this (url , SourceInfoStorageFactory . newEmptySourceInfoStorage ( ));
3740 }
3841
39- public HttpUrlSource (String url , String mime ) {
40- this .url = Preconditions .checkNotNull (url );
41- this .mime = mime ;
42+ public HttpUrlSource (String url , SourceInfoStorage sourceInfoStorage ) {
43+ this .sourceInfoStorage = checkNotNull (sourceInfoStorage );
44+ SourceInfo sourceInfo = sourceInfoStorage .get (url );
45+ this .sourceInfo = sourceInfo != null ? sourceInfo :
46+ new SourceInfo (url , Integer .MIN_VALUE , ProxyCacheUtils .getSupposablyMime (url ));
4247 }
4348
4449 public HttpUrlSource (HttpUrlSource source ) {
45- this .url = source .url ;
46- this .mime = source .mime ;
47- this .length = source .length ;
50+ this .sourceInfo = source .sourceInfo ;
51+ this .sourceInfoStorage = source .sourceInfoStorage ;
4852 }
4953
5054 @ Override
5155 public synchronized int length () throws ProxyCacheException {
52- if (length == Integer .MIN_VALUE ) {
56+ if (sourceInfo . length == Integer .MIN_VALUE ) {
5357 fetchContentInfo ();
5458 }
55- return length ;
59+ return sourceInfo . length ;
5660 }
5761
5862 @ Override
5963 public void open (int offset ) throws ProxyCacheException {
6064 try {
6165 connection = openConnection (offset , -1 );
62- mime = connection .getContentType ();
66+ String mime = connection .getContentType ();
6367 inputStream = new BufferedInputStream (connection .getInputStream (), DEFAULT_BUFFER_SIZE );
64- length = readSourceAvailableBytes (connection , offset , connection .getResponseCode ());
68+ int length = readSourceAvailableBytes (connection , offset , connection .getResponseCode ());
69+ this .sourceInfo = new SourceInfo (sourceInfo .url , length , mime );
70+ this .sourceInfoStorage .put (sourceInfo .url , sourceInfo );
6571 } catch (IOException e ) {
66- throw new ProxyCacheException ("Error opening connection for " + url + " with offset " + offset , e );
72+ throw new ProxyCacheException ("Error opening connection for " + sourceInfo . url + " with offset " + offset , e );
6773 }
6874 }
6975
7076 private int readSourceAvailableBytes (HttpURLConnection connection , int offset , int responseCode ) throws IOException {
7177 int contentLength = connection .getContentLength ();
7278 return responseCode == HTTP_OK ? contentLength
73- : responseCode == HTTP_PARTIAL ? contentLength + offset : length ;
79+ : responseCode == HTTP_PARTIAL ? contentLength + offset : sourceInfo . length ;
7480 }
7581
7682 @ Override
@@ -90,29 +96,31 @@ public void close() throws ProxyCacheException {
9096 @ Override
9197 public int read (byte [] buffer ) throws ProxyCacheException {
9298 if (inputStream == null ) {
93- throw new ProxyCacheException ("Error reading data from " + url + ": connection is absent!" );
99+ throw new ProxyCacheException ("Error reading data from " + sourceInfo . url + ": connection is absent!" );
94100 }
95101 try {
96102 return inputStream .read (buffer , 0 , buffer .length );
97103 } catch (InterruptedIOException e ) {
98- throw new InterruptedProxyCacheException ("Reading source " + url + " is interrupted" , e );
104+ throw new InterruptedProxyCacheException ("Reading source " + sourceInfo . url + " is interrupted" , e );
99105 } catch (IOException e ) {
100- throw new ProxyCacheException ("Error reading data from " + url , e );
106+ throw new ProxyCacheException ("Error reading data from " + sourceInfo . url , e );
101107 }
102108 }
103109
104110 private void fetchContentInfo () throws ProxyCacheException {
105- Log .d (LOG_TAG , "Read content info from " + url );
111+ Log .d (LOG_TAG , "Read content info from " + sourceInfo . url );
106112 HttpURLConnection urlConnection = null ;
107113 InputStream inputStream = null ;
108114 try {
109115 urlConnection = openConnection (0 , 10000 );
110- length = urlConnection .getContentLength ();
111- mime = urlConnection .getContentType ();
116+ int length = urlConnection .getContentLength ();
117+ String mime = urlConnection .getContentType ();
112118 inputStream = urlConnection .getInputStream ();
113- Log .i (LOG_TAG , "Content info for `" + url + "`: mime: " + mime + ", content-length: " + length );
119+ this .sourceInfo = new SourceInfo (sourceInfo .url , length , mime );
120+ this .sourceInfoStorage .put (sourceInfo .url , sourceInfo );
121+ Log .i (LOG_TAG , "Source info fetched: " + sourceInfo );
114122 } catch (IOException e ) {
115- Log .e (LOG_TAG , "Error fetching info from " + url , e );
123+ Log .e (LOG_TAG , "Error fetching info from " + sourceInfo . url , e );
116124 } finally {
117125 ProxyCacheUtils .close (inputStream );
118126 if (urlConnection != null ) {
@@ -125,7 +133,7 @@ private HttpURLConnection openConnection(int offset, int timeout) throws IOExcep
125133 HttpURLConnection connection ;
126134 boolean redirected ;
127135 int redirectCount = 0 ;
128- String url = this .url ;
136+ String url = this .sourceInfo . url ;
129137 do {
130138 Log .d (LOG_TAG , "Open connection " + (offset > 0 ? " with offset " + offset : "" ) + " to " + url );
131139 connection = (HttpURLConnection ) new URL (url ).openConnection ();
@@ -151,18 +159,18 @@ private HttpURLConnection openConnection(int offset, int timeout) throws IOExcep
151159 }
152160
153161 public synchronized String getMime () throws ProxyCacheException {
154- if (TextUtils .isEmpty (mime )) {
162+ if (TextUtils .isEmpty (sourceInfo . mime )) {
155163 fetchContentInfo ();
156164 }
157- return mime ;
165+ return sourceInfo . mime ;
158166 }
159167
160168 public String getUrl () {
161- return url ;
169+ return sourceInfo . url ;
162170 }
163171
164172 @ Override
165173 public String toString () {
166- return "HttpUrlSource{url ='" + url + "}" ;
174+ return "HttpUrlSource{sourceInfo ='" + sourceInfo + "}" ;
167175 }
168176}
0 commit comments