下面列出了怎么用android.util.Base64InputStream的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Just to test if using the ByteArrayInputStream isn't missing something
*/
@Test
public void itDoesNotAddLineBreaks_whenBase64InputStream() throws IOException {
byte[] exampleString = "dGVzdA==".getBytes();
Base64InputStream inputStream =
new Base64InputStream(new ByteArrayInputStream(exampleString), Base64.NO_WRAP);
assertEquals("dGVzdA==", invokeConversion(inputStream));
}
/**
* @see http
* ://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/
* apache /xmlgraphics/util/uri/DataURIResolver.java
*/
private static InputStream openDataUriStream(String uri) {
int commaPos = uri.indexOf(',');
if (commaPos < 0) {
PXLog.w(TAG, "Data uri is malformed: " + uri);
return null;
}
String header = uri.substring(0, commaPos);
String data = uri.substring(commaPos + 1);
if (header.endsWith(";base64")) {
byte[] bytes = data.getBytes();
ByteArrayInputStream encodedStream = new ByteArrayInputStream(bytes);
return new Base64InputStream(encodedStream, Base64.DEFAULT);
} else {
String encoding = "UTF-8";
final int charsetpos = header.indexOf(";charset=");
if (charsetpos > 0) {
encoding = header.substring(charsetpos + 9);
}
try {
return new ByteArrayInputStream(URLDecoder.decode(data, encoding)
.getBytes(encoding));
} catch (Exception e) {
PXLog.e(TAG, e, "Unable to decode data uri contents: " + uri);
}
}
return null;
}