26
26
* Implementations have to be thread-safe.
27
27
*
28
28
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
29
+ * @author Leo Link (mr[dot]leolink[at]gmail[dot]com)
29
30
* @since 1.4.0
30
31
*/
31
32
public interface ImageDownloader {
@@ -42,17 +43,29 @@ public interface ImageDownloader {
42
43
InputStream getStream (String imageUri , Object extra ) throws IOException ;
43
44
44
45
/** Represents supported schemes(protocols) of URI. Provides convenient methods for work with schemes and URIs. */
45
- public enum Scheme {
46
- HTTP ("http" ), HTTPS ("https" ), FILE ("file" ), CONTENT ("content" ), ASSETS ("assets" ), DRAWABLE ("drawable" ), UNKNOWN ("" );
46
+ enum Scheme {
47
+ HTTP ("http" , true ),
48
+ HTTPS ("https" , true ),
49
+ FILE ("file" , true ),
50
+ CONTENT ("content" , true ),
51
+ ASSETS ("assets" , true ),
52
+ DRAWABLE ("drawable" , true ),
53
+ BASE64 ("^data:image/[a-zA-Z]{3,};base64,([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)" , false ),
54
+ UNKNOWN ("" , false );
47
55
48
56
private String scheme ;
49
- private String uriPrefix ;
57
+ private String pattern ;
50
58
51
- Scheme (String scheme ) {
52
- this .scheme = scheme ;
53
- uriPrefix = scheme + "://" ;
59
+ Scheme (String scheme , boolean isScheme ) {
60
+ this .scheme = scheme ;
61
+ if (isScheme ) {
62
+ this .pattern = "^" + scheme + "://.+?$" ;
63
+ } else {
64
+ this .pattern = scheme ;
65
+ }
54
66
}
55
67
68
+
56
69
/**
57
70
* Defines scheme of incoming URI
58
71
*
@@ -71,20 +84,24 @@ public static Scheme ofUri(String uri) {
71
84
}
72
85
73
86
private boolean belongsTo (String uri ) {
74
- return uri .toLowerCase (Locale .US ).startsWith ( uriPrefix );
87
+ return uri .toLowerCase (Locale .US ).matches ( pattern );
75
88
}
76
89
77
90
/** Appends scheme to incoming path */
78
91
public String wrap (String path ) {
79
- return uriPrefix + path ;
92
+ return getUriPrefix () + path ;
80
93
}
81
94
82
95
/** Removed scheme part ("scheme://") from incoming URI */
83
96
public String crop (String uri ) {
84
97
if (!belongsTo (uri )) {
85
98
throw new IllegalArgumentException (String .format ("URI [%1$s] doesn't have expected scheme [%2$s]" , uri , scheme ));
86
99
}
87
- return uri .substring (uriPrefix .length ());
100
+ return uri .substring (getUriPrefix () .length ());
88
101
}
102
+
103
+ private String getUriPrefix () {
104
+ return scheme + "://" ;
105
+ }
89
106
}
90
107
}
0 commit comments