本文整理了Java中android.util.Base64.encode()
方法的一些代码示例,展示了Base64.encode()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Base64.encode()
方法的具体详情如下:
包路径:android.util.Base64
类名称:Base64
方法名:encode
[英]Base64-encode the given data and return a newly allocated byte[] with the result.
[中]Base64对给定数据进行编码,并返回新分配的字节[]和结果。
代码示例来源:origin: igniterealtime/Smack
@Override
public byte[] encode(byte[] input, int offset, int len) {
return Base64.encode(input, offset, len, BASE64_ENCODER_FLAGS);
}
代码示例来源:origin: kaaproject/kaa
@Override
public byte[] encodeBase64(byte[] binaryData) {
return android.util.Base64.encode(binaryData, android.util.Base64.DEFAULT);
}
代码示例来源:origin: JessYanCoding/MVPArms
/**
* 将对象储存到sharepreference
*
* @param key
* @param device
* @param <T>
*/
public static <T> boolean saveDeviceData(Context context, String key, T device) {
if (mSharedPreferences == null) {
mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try { //Device为自定义类
// 创建对象输出流,并封装字节流
ObjectOutputStream oos = new ObjectOutputStream(baos);
// 将对象写入字节流
oos.writeObject(device);
// 将字节流编码成base64的字符串
String oAuth_Base64 = new String(Base64.encode(baos
.toByteArray(), Base64.DEFAULT));
mSharedPreferences.edit().putString(key, oAuth_Base64).apply();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
代码示例来源:origin: smuyyh/BookReader
public void putObject(String key, Object object) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(baos);
out.writeObject(object);
String objectVal = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
editor.putString(key, objectVal);
editor.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
代码示例来源:origin: JZ-Darkal/AndroidHttpCapture
/**
* writeObject 方法负责写入特定类的对象的状态,以便相应的 readObject 方法可以还原它
* 最后,用Base64.encode将字节文件转换成Base64编码保存在String中
*
* @param object 待加密的转换为String的对象
* @return String 加密后的String
*/
private static String Object2String(Object object) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(object);
String string = new String(Base64.encode(byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
objectOutputStream.close();
return string;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
代码示例来源:origin: ACRA/acra
@SuppressWarnings("WeakerAccess")
protected void configureHeaders(@NonNull HttpURLConnection connection, @Nullable String login, @Nullable String password,
@Nullable Map<String, String> customHeaders, @NonNull T t) throws IOException {
// Set Headers
connection.setRequestProperty("User-Agent", String.format("Android ACRA %1$s", BuildConfig.VERSION_NAME)); //sent ACRA version to server
connection.setRequestProperty("Accept",
"text/html,application/xml,application/json,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
connection.setRequestProperty("Content-Type", getContentType(context, t));
// Set Credentials
if (login != null && password != null) {
final String credentials = login + ':' + password;
final String encoded = new String(Base64.encode(credentials.getBytes(ACRAConstants.UTF8), Base64.NO_WRAP), ACRAConstants.UTF8);
connection.setRequestProperty("Authorization", "Basic " + encoded);
}
if (senderConfiguration.compress()) {
connection.setRequestProperty("Content-Encoding", "gzip");
}
if (customHeaders != null) {
for (final Map.Entry<String, String> header : customHeaders.entrySet()) {
connection.setRequestProperty(header.getKey(), header.getValue());
}
}
}
代码示例来源:origin: cymcsg/UltimateAndroid
/**
* Base64-encode the given data and return a newly allocated byte[] with the result.
*
* @param context
* @param type
* @return
*/
public static byte[] Base64encodingByte(byte[] context, int type) {
byte[] result;
if (getSdkVersion() > 17) {
result = Base64.encode(context, type);
} else {
result = com.marshalchen.ua.common.commonUtils.urlUtils.Base64.encodeBytesToBytes(context);
}
return result;
}
代码示例来源:origin: andpor/react-native-sqlite-storage
@SuppressLint("NewApi")
private void bindRow(WritableMap row, String key, Cursor cur, int i) {
int curType = cur.getType(i);
switch (curType) {
case Cursor.FIELD_TYPE_NULL:
row.putNull(key);
break;
case Cursor.FIELD_TYPE_INTEGER:
row.putDouble(key, cur.getLong(i));
break;
case Cursor.FIELD_TYPE_FLOAT:
row.putDouble(key, cur.getDouble(i));
break;
case Cursor.FIELD_TYPE_BLOB:
row.putString(key, new String(Base64.encode(cur.getBlob(i), Base64.DEFAULT)));
break;
case Cursor.FIELD_TYPE_STRING:
default: /* (not expected) */
row.putString(key, cur.getString(i));
break;
}
}
代码示例来源:origin: andpor/react-native-sqlite-storage
@SuppressLint("NewApi")
private void bindRow(WritableMap row, String key, Cursor cur, int i) {
int curType = cur.getType(i);
switch (curType) {
case Cursor.FIELD_TYPE_NULL:
row.putNull(key);
break;
case Cursor.FIELD_TYPE_INTEGER:
row.putDouble(key, cur.getLong(i));
break;
case Cursor.FIELD_TYPE_FLOAT:
row.putDouble(key, cur.getDouble(i));
break;
case Cursor.FIELD_TYPE_BLOB:
row.putString(key, new String(Base64.encode(cur.getBlob(i), Base64.DEFAULT)));
break;
case Cursor.FIELD_TYPE_STRING:
default:
row.putString(key, cur.getString(i));
break;
}
}
代码示例来源:origin: limedroid/XDroidMvp
public static byte[] encode(byte[] plain) {
return Base64.encode(plain, Base64.DEFAULT);
}
代码示例来源:origin: serso/android-checkout
@Nonnull
static String encrypt(@Nonnull String message, @Nonnull String salt) {
return new String(Base64.encode(xor(message, salt).getBytes(), 0));
}
代码示例来源:origin: kingthy/TVRemoteIME
public static String generateAppKey(String str, short s, byte b) {
if (str == null) {
XLLog.e(TAG, "generateAppKey, appName invalid");
return null;
}
int length = str.length();
byte[] bArr = new byte[(((length + 1) + 2) + 1)];
byte[] bytes = str.getBytes();
for (int i = 0; i < bytes.length; i++) {
bArr[i] = bytes[i];
}
bArr[length] = (byte) 0;
bArr[length + 1] = (byte) (s & 255);
bArr[length + 2] = (byte) ((s >> 8) & 255);
bArr[length + 3] = b;
return new String(Base64.encode(bArr, 0)).trim();
}
代码示例来源:origin: kingthy/TVRemoteIME
String peerid = getPeerid();
String guid = getGuid();
XLLog.i(TAG, "Peerid:" + new String(Base64.encode(peerid.getBytes(), 0)));
XLLog.i(TAG, "Guid:" + new String(Base64.encode(guid.getBytes(), 0)));
if (mAllowExecution) {
i2 = XLUtil.getNetworkTypeComplete(context);
代码示例来源:origin: yaozs/YzsLib
/**
* Base64编码
*
* @param input 要编码的字节数组
* @return Base64编码后的字符串
*/
public static byte[] base64Encode(byte[] input) {
return Base64.encode(input, Base64.NO_WRAP);
}
代码示例来源:origin: 0xm1nam0/RxCore
/**
* Base64URL安全编码
* <p>将Base64中的URL非法字符�?,/=转为其他字符, 见RFC3548</p>
*
* @param input 要Base64URL安全编码的字符串
* @return Base64URL安全编码后的字符串
*/
public static byte[] base64UrlSafeEncode(String input) {
return Base64.encode(input.getBytes(), Base64.URL_SAFE);
}
代码示例来源:origin: yaozs/YzsLib
/**
* Base64URL安全编码
* <p>将Base64中的URL非法字符�?,/=转为其他字符, 见RFC3548</p>
*
* @param input 要Base64URL安全编码的字符串
* @return Base64URL安全编码后的字符串
*/
public static byte[] base64UrlSafeEncode(String input) {
return Base64.encode(input.getBytes(), Base64.URL_SAFE);
}
代码示例来源:origin: NightscoutFoundation/xDrip
public static String base64encodeBytes(byte[] input) {
try {
return new String(Base64.encode(input, Base64.NO_WRAP), "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Got unsupported encoding: " + e);
return "encode-error";
}
}
代码示例来源:origin: jamorham/xDrip-plus
public static String base64encodeBytes(byte[] input) {
try {
return new String(Base64.encode(input, Base64.NO_WRAP), "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Got unsupported encoding: " + e);
return "encode-error";
}
}
代码示例来源:origin: jamorham/xDrip-plus
public static String base64encodeBytes(byte[] input) {
try {
return new String(Base64.encode(input, Base64.NO_WRAP), "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Got unsupported encoding: " + e);
return "encode-error";
}
}
代码示例来源:origin: NightscoutFoundation/xDrip
public static String base64encode(String input) {
try {
return new String(Base64.encode(input.getBytes("UTF-8"), Base64.NO_WRAP), "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Got unsupported encoding: " + e);
return "encode-error";
}
}
内容来源于网络,如有侵权,请联系作者删除!