okio.ByteString.decodeBase64()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(11.4k)|赞(0)|评价(0)|浏览(96)

本文整理了Java中okio.ByteString.decodeBase64()方法的一些代码示例,展示了ByteString.decodeBase64()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteString.decodeBase64()方法的具体详情如下:
包路径:okio.ByteString
类名称:ByteString
方法名:decodeBase64

ByteString.decodeBase64介绍

[英]Decodes the Base64-encoded bytes and returns their value as a byte string. Returns null if base64 is not a Base64-encoded sequence of bytes.
[中]对Base64编码的字节进行解码,并将其值作为字节字符串返回。如果base64不是base64编码的字节序列,则返回null。

代码示例

代码示例来源:origin: k9mail/k-9

private static ByteString decodeB(String encodedText) {
  ByteString decoded = ByteString.decodeBase64(encodedText);
  return decoded == null ? ByteString.EMPTY : decoded;
}

代码示例来源:origin: square/okhttp

Pin(String pattern, String pin) {
 this.pattern = pattern;
 this.canonicalHostname = pattern.startsWith(WILDCARD)
   ? HttpUrl.get("http://" + pattern.substring(WILDCARD.length())).host()
   : HttpUrl.get("http://" + pattern).host();
 if (pin.startsWith("sha1/")) {
  this.hashAlgorithm = "sha1/";
  this.hash = ByteString.decodeBase64(pin.substring("sha1/".length()));
 } else if (pin.startsWith("sha256/")) {
  this.hashAlgorithm = "sha256/";
  this.hash = ByteString.decodeBase64(pin.substring("sha256/".length()));
 } else {
  throw new IllegalArgumentException("pins must start with 'sha256/' or 'sha1/': " + pin);
 }
 if (this.hash == null) {
  throw new IllegalArgumentException("pins must be base64: " + pin);
 }
}

代码示例来源:origin: square/moshi

@Override public ByteString fromJson(JsonReader reader) throws IOException {
 String base64 = reader.nextString();
 return ByteString.decodeBase64(base64);
}

代码示例来源:origin: square/wire

@Override public @Nullable ByteString read(JsonReader in) throws IOException {
  if (in.peek() == JsonToken.NULL) {
   in.nextNull();
   return null;
  }
  return ByteString.decodeBase64(in.nextString());
 }
}

代码示例来源:origin: square/wire

@Override public ByteString fromJson(JsonReader in) throws IOException {
  return ByteString.decodeBase64(in.nextString());
 }
}.nullSafe();

代码示例来源:origin: square/okhttp

private List<Certificate> readCertificateList(BufferedSource source) throws IOException {
 int length = readInt(source);
 if (length == -1) return Collections.emptyList(); // OkHttp v1.2 used -1 to indicate null.
 try {
  CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
  List<Certificate> result = new ArrayList<>(length);
  for (int i = 0; i < length; i++) {
   String line = source.readUtf8LineStrict();
   Buffer bytes = new Buffer();
   bytes.write(ByteString.decodeBase64(line));
   result.add(certificateFactory.generateCertificate(bytes.inputStream()));
  }
  return result;
 } catch (CertificateException e) {
  throw new IOException(e.getMessage());
 }
}

代码示例来源:origin: googlemaps/google-maps-services-java

public UrlSigner(final String keyString) throws NoSuchAlgorithmException, InvalidKeyException {
 // Convert from URL-safe base64 to regular base64.
 String base64 = keyString.replace('-', '+').replace('_', '/');
 ByteString decodedKey = ByteString.decodeBase64(base64);
 if (decodedKey == null) {
  // NOTE: don't log the exception, in case some of the private key leaks to an end-user.
  throw new IllegalArgumentException("Private key is invalid.");
 }
 mac = Mac.getInstance(ALGORITHM_HMAC_SHA1);
 mac.init(new SecretKeySpec(decodedKey.toByteArray(), ALGORITHM_HMAC_SHA1));
}

代码示例来源:origin: com.squareup.okhttp3/okhttp

Pin(String pattern, String pin) {
 this.pattern = pattern;
 this.canonicalHostname = pattern.startsWith(WILDCARD)
   ? HttpUrl.get("http://" + pattern.substring(WILDCARD.length())).host()
   : HttpUrl.get("http://" + pattern).host();
 if (pin.startsWith("sha1/")) {
  this.hashAlgorithm = "sha1/";
  this.hash = ByteString.decodeBase64(pin.substring("sha1/".length()));
 } else if (pin.startsWith("sha256/")) {
  this.hashAlgorithm = "sha256/";
  this.hash = ByteString.decodeBase64(pin.substring("sha256/".length()));
 } else {
  throw new IllegalArgumentException("pins must start with 'sha256/' or 'sha1/': " + pin);
 }
 if (this.hash == null) {
  throw new IllegalArgumentException("pins must be base64: " + pin);
 }
}

代码示例来源:origin: com.squareup.okhttp3/okhttp

private List<Certificate> readCertificateList(BufferedSource source) throws IOException {
 int length = readInt(source);
 if (length == -1) return Collections.emptyList(); // OkHttp v1.2 used -1 to indicate null.
 try {
  CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
  List<Certificate> result = new ArrayList<>(length);
  for (int i = 0; i < length; i++) {
   String line = source.readUtf8LineStrict();
   Buffer bytes = new Buffer();
   bytes.write(ByteString.decodeBase64(line));
   result.add(certificateFactory.generateCertificate(bytes.inputStream()));
  }
  return result;
 } catch (CertificateException e) {
  throw new IOException(e.getMessage());
 }
}

代码示例来源:origin: apollographql/apollo-android

private List<Certificate> readCertificateList(BufferedSource source) throws IOException {
 int length = readInt(source);
 if (length == -1) return Collections.emptyList(); // OkHttp v1.2 used -1 to indicate null.
 try {
  CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
  List<Certificate> result = new ArrayList<>(length);
  for (int i = 0; i < length; i++) {
   String line = source.readUtf8LineStrict();
   Buffer bytes = new Buffer();
   bytes.write(ByteString.decodeBase64(line));
   result.add(certificateFactory.generateCertificate(bytes.inputStream()));
  }
  return result;
 } catch (CertificateException e) {
  throw new IOException(e.getMessage());
 }
}

代码示例来源:origin: square/okio

private Buffer decodeBase64(String s) {
 return new Buffer().write(ByteString.decodeBase64(s));
}

代码示例来源:origin: square/okio

public void run() throws Exception {
 Point point = new Point(8.0, 15.0);
 ByteString pointBytes = serialize(point);
 System.out.println(pointBytes.base64());
 ByteString goldenBytes = ByteString.decodeBase64("rO0ABXNyAB5va2lvLnNhbXBsZ"
   + "XMuR29sZGVuVmFsdWUkUG9pbnTdUW8rMji1IwIAAkQAAXhEAAF5eHBAIAAAAAAAAEAuA"
   + "AAAAAAA");
 Point decoded = (Point) deserialize(goldenBytes);
 assertEquals(point, decoded);
}

代码示例来源:origin: square/okio

@Test public void decodeBase64WithWhitespace() {
 assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64(" AA AA ").utf8());
 assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64(" AA A\r\nA ").utf8());
 assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64("AA AA").utf8());
 assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64(" AA AA ").utf8());
 assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64(" AA A\r\nA ").utf8());
 assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64("A    AAA").utf8());
 assertEquals("", ByteString.decodeBase64("    ").utf8());
}

代码示例来源:origin: k9mail/k-9

ByteString byteString = ByteString.decodeBase64(base64KeyData);
if (byteString == null) {
  Timber.e("autocrypt: error parsing base64 data");

代码示例来源:origin: square/okio

@Test public void decodeBase64() {
 assertEquals("", ByteString.decodeBase64("").utf8());
 assertEquals(null, ByteString.decodeBase64("/===")); // Can't do anything with 6 bits!
 assertEquals(ByteString.decodeHex("ff"), ByteString.decodeBase64("//=="));
 assertEquals(ByteString.decodeHex("ff"), ByteString.decodeBase64("__=="));
 assertEquals(ByteString.decodeHex("ffff"), ByteString.decodeBase64("///="));
 assertEquals(ByteString.decodeHex("ffff"), ByteString.decodeBase64("___="));
 assertEquals(ByteString.decodeHex("ffffff"), ByteString.decodeBase64("////"));
 assertEquals(ByteString.decodeHex("ffffff"), ByteString.decodeBase64("____"));
 assertEquals(ByteString.decodeHex("ffffffffffff"), ByteString.decodeBase64("////////"));
 assertEquals(ByteString.decodeHex("ffffffffffff"), ByteString.decodeBase64("________"));
 assertEquals("What's to be scared about? It's just a little hiccup in the power...",
   ByteString.decodeBase64("V2hhdCdzIHRvIGJlIHNjYXJlZCBhYm91dD8gSXQncyBqdXN0IGEgbGl0dGxlIGhpY2"
     + "N1cCBpbiB0aGUgcG93ZXIuLi4=").utf8());
 // Uses two encoding styles. Malformed, but supported as a side-effect.
 assertEquals(ByteString.decodeHex("ffffff"), ByteString.decodeBase64("__//"));
}

代码示例来源:origin: k9mail/k-9

@Nullable
@VisibleForTesting
AutocryptGossipHeader parseAutocryptGossipHeader(String headerValue) {
  Map<String,String> parameters = MimeUtility.getAllHeaderParameters(headerValue);
  String type = parameters.remove(AutocryptHeader.AUTOCRYPT_PARAM_TYPE);
  if (type != null && !type.equals(AutocryptHeader.AUTOCRYPT_TYPE_1)) {
    Timber.e("autocrypt: unsupported type parameter %s", type);
    return null;
  }
  String base64KeyData = parameters.remove(AutocryptHeader.AUTOCRYPT_PARAM_KEY_DATA);
  if (base64KeyData == null) {
    Timber.e("autocrypt: missing key parameter");
    return null;
  }
  ByteString byteString = ByteString.decodeBase64(base64KeyData);
  if (byteString == null) {
    Timber.e("autocrypt: error parsing base64 data");
    return null;
  }
  String addr = parameters.remove(AutocryptHeader.AUTOCRYPT_PARAM_ADDR);
  if (addr == null) {
    Timber.e("autocrypt: no to header!");
    return null;
  }
  if (hasCriticalParameters(parameters)) {
    return null;
  }
  return new AutocryptGossipHeader(addr, byteString.toByteArray());
}

代码示例来源:origin: square/okio

@Test public void ignoreUnnecessaryPadding() {
 assertEquals("", ByteString.decodeBase64("====").utf8());
 assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64("AAAA====").utf8());
}

代码示例来源:origin: square/okhttp

String code = requestUrl.queryParameter("code");
String stateString = requestUrl.queryParameter("state");
ByteString state = stateString != null ? ByteString.decodeBase64(stateString) : null;

代码示例来源:origin: square/wire

@Test public void decodeGolden() throws Exception {
 SimpleMessage goldenValue = new SimpleMessage.Builder()
   .required_int32(99)
   .result("tacos")
   .build();
 ByteString goldenSerialized = ByteString.decodeBase64("rO0ABXNyACdjb20uc3F1YXJldXAud2lyZS5NZXNz"
   + "YWdlU2VyaWFsaXplZEZvcm0AAAAAAAAAAAIAAlsABWJ5dGVzdAACW0JMAAxtZXNzYWdlQ2xhc3N0ABFMamF2YS9s"
   + "YW5nL0NsYXNzO3hwdXIAAltCrPMX+AYIVOACAAB4cAAAAAkoY1IFdGFjb3N2cgAtY29tLnNxdWFyZXVwLndpcmUu"
   + "cHJvdG9zLnNpbXBsZS5TaW1wbGVNZXNzYWdlAAAAAAAAAAACAAxMABRkZWZhdWx0X2ZvcmVpZ25fZW51bXQALkxj"
   + "b20vc3F1YXJldXAvd2lyZS9wcm90b3MvZm9yZWlnbi9Gb3JlaWduRW51bTtMABNkZWZhdWx0X25lc3RlZF9lbnVt"
   + "dAA6TGNvbS9zcXVhcmV1cC93aXJlL3Byb3Rvcy9zaW1wbGUvU2ltcGxlTWVzc2FnZSROZXN0ZWRFbnVtO0wAF25v"
   + "X2RlZmF1bHRfZm9yZWlnbl9lbnVtcQB+AAdMAAFvdAASTGphdmEvbGFuZy9TdHJpbmc7TAAVb3B0aW9uYWxfZXh0"
   + "ZXJuYWxfbXNndAAxTGNvbS9zcXVhcmV1cC93aXJlL3Byb3Rvcy9zaW1wbGUvRXh0ZXJuYWxNZXNzYWdlO0wADm9w"
   + "dGlvbmFsX2ludDMydAATTGphdmEvbGFuZy9JbnRlZ2VyO0wAE29wdGlvbmFsX25lc3RlZF9tc2d0AD1MY29tL3Nx"
   + "dWFyZXVwL3dpcmUvcHJvdG9zL3NpbXBsZS9TaW1wbGVNZXNzYWdlJE5lc3RlZE1lc3NhZ2U7TAAFb3RoZXJxAH4A"
   + "CUwACHBhY2thZ2VfcQB+AAlMAA9yZXBlYXRlZF9kb3VibGV0ABBMamF2YS91dGlsL0xpc3Q7TAAOcmVxdWlyZWRf"
   + "aW50MzJxAH4AC0wABnJlc3VsdHEAfgAJeHIAGWNvbS5zcXVhcmV1cC53aXJlLk1lc3NhZ2UAAAAAAAAAAAIAAHhw"
 );
 assertThat(deserialize(goldenSerialized)).isEqualTo(goldenValue);
 assertThat(serialize(goldenValue)).isEqualTo(goldenSerialized);
}

代码示例来源:origin: fabric8io/kubernetes-client

private static byte[] readBytes(BufferedReader reader, String endMarker) throws IOException {
  String line;
  StringBuffer buf = new StringBuffer();

  while ((line = reader.readLine()) != null) {
   if (line.indexOf(endMarker) != -1) {
    return ByteString.decodeBase64(buf.toString()).toByteArray();
   }
   buf.append(line.trim());
  }
  throw new IOException("PEM is invalid : No end marker");
 }
}

相关文章