-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
144 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 0 additions & 37 deletions
37
Meatspace/src/main/java/com/romainpiel/lib/gif/GIFUtils.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
Meatspace/src/main/java/com/romainpiel/lib/utils/StringUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.romainpiel.lib.utils; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* Meatspace | ||
* User: romainpiel | ||
* Date: 28/08/2013 | ||
* Time: 11:50 | ||
*/ | ||
public class StringUtils { | ||
|
||
/** | ||
* Utility method to build a [spearator] separated string of a collection | ||
* @param collection collection to read | ||
* @param separator separator | ||
* @param <E> generic parameter of the collection | ||
* @return a string representation of the collection using the given separator | ||
*/ | ||
public static <E> String join(Collection<E> collection, String separator) { | ||
StringBuilder result = new StringBuilder(); | ||
for(E object : collection) { | ||
result.append(object.toString()); | ||
result.append(separator); | ||
} | ||
return result.length() > 0 ? result.substring(0, result.length() - 1): ""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
Meatspace/src/main/java/com/romainpiel/model/GifMedia.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.romainpiel.model; | ||
|
||
import android.util.Base64; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParseException; | ||
import com.romainpiel.lib.utils.Debug; | ||
import com.romainpiel.lib.utils.StringUtils; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.lang.reflect.Type; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* MeatspaceProject | ||
* User: romainpiel | ||
* Date: 08/12/2013 | ||
* Time: 09:13 | ||
*/ | ||
public class GifMedia { | ||
|
||
private static final String GIF_PREFIX = "data:image/gif;base64,"; | ||
|
||
private byte[] bytes; | ||
|
||
public byte[] getBytes() { | ||
return bytes; | ||
} | ||
|
||
public static JsonDeserializer<GifMedia> getDeserializer() { | ||
return new JsonDeserializer<GifMedia>() { | ||
@Override | ||
public GifMedia deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { | ||
String raw = jsonElement.getAsString(); | ||
GifMedia result = new GifMedia(); | ||
result.bytes = mediaToGIFbytes(raw); | ||
return result; | ||
} | ||
}; | ||
} | ||
|
||
public static String mediaFromGIFbytes(byte[] bytes) { | ||
if (bytes == null) { | ||
return null; | ||
} | ||
|
||
String result = null; | ||
try { | ||
result = GIF_PREFIX + new String(Base64.encode(bytes, Base64.DEFAULT), "UTF-8"); | ||
} catch (UnsupportedEncodingException e) { | ||
Debug.out(e); | ||
} | ||
return result; | ||
} | ||
|
||
public static byte[] mediaToGIFbytes(String media) { | ||
if (media == null) { | ||
return null; | ||
} | ||
|
||
try { | ||
return Base64.decode(unescape(media), Base64.DEFAULT); | ||
} catch (IllegalArgumentException e) { | ||
return null; | ||
} | ||
} | ||
|
||
public static String unescape(String template) { | ||
Map<String,String> tokens = new HashMap<String,String>(); | ||
tokens.put(GIF_PREFIX, ""); | ||
tokens.put("%2b", "+"); | ||
tokens.put("%2f", "/"); | ||
|
||
String patternString = "(" + StringUtils.join(tokens.keySet(), "|") + ")"; | ||
Pattern pattern = Pattern.compile(patternString); | ||
Matcher matcher = pattern.matcher(template); | ||
|
||
StringBuffer sb = new StringBuffer(); | ||
while(matcher.find()) { | ||
matcher.appendReplacement(sb, tokens.get(matcher.group(1))); | ||
} | ||
matcher.appendTail(sb); | ||
|
||
return sb.toString(); | ||
} | ||
} |