java.util.Base64类的使用及代码示例

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

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

Base64介绍

暂无

代码示例

代码示例来源:origin: gocd/gocd

public static String encodeWork(Work work) {
  try {
    try (ByteArrayOutputStream binaryOutput = new ByteArrayOutputStream()) {
      try (ObjectOutputStream objectStream = new ObjectOutputStream(binaryOutput)) {
        objectStream.writeObject(work);
      }
      return Base64.getEncoder().encodeToString(binaryOutput.toByteArray());
    }
  } catch (IOException e) {
    throw bomb(e);
  }
}

代码示例来源:origin: stackoverflow.com

byte [] data = Base64.getDecoder().decode( s );
ObjectInputStream ois = new ObjectInputStream( 
                new ByteArrayInputStream(  data ) );
Object o  = ois.readObject();
ois.close();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( o );
oos.close();
return Base64.getEncoder().encodeToString(baos.toByteArray());

代码示例来源:origin: spring-projects/spring-framework

/**
 * Base64-encode the given byte array using the RFC 4648
 * "URL and Filename Safe Alphabet".
 * @param src the original byte array
 * @return the encoded byte array
 * @since 4.2.4
 */
public static byte[] encodeUrlSafe(byte[] src) {
  if (src.length == 0) {
    return src;
  }
  return Base64.getUrlEncoder().encode(src);
}

代码示例来源:origin: stackoverflow.com

import java.util.Base64;

byte[] bytes = "Hello, World!".getBytes("UTF-8");
String encoded = Base64.getEncoder().encodeToString(bytes);
byte[] decoded = Base64.getDecoder().decode(encoded);

代码示例来源:origin: Meituan-Dianping/walle

if (sourceManifestBytes != null) {
  try {
    sourceManifest = new Manifest(new ByteArrayInputStream(sourceManifestBytes));
  } catch (IOException e) {
    throw new IllegalArgumentException("Failed to parse source MANIFEST.MF", e);
ByteArrayOutputStream manifestOut = new ByteArrayOutputStream();
Attributes mainAttrs = new Attributes();
  entryAttrs.putValue(
      entryDigestAttributeName,
      Base64.getEncoder().encodeToString(entryDigest));
  ByteArrayOutputStream sectionOut = new ByteArrayOutputStream();
  byte[] sectionBytes;
  try {
    ManifestWriter.writeIndividualSection(sectionOut, entryName, entryAttrs);
    sectionBytes = sectionOut.toByteArray();
    manifestOut.write(sectionBytes);
  } catch (IOException e) {

代码示例来源:origin: gocd/gocd

public static Work decodeWork(String data) {
  try {
    byte[] binary = Base64.getDecoder().decode(data.getBytes(StandardCharsets.UTF_8));
    try (ObjectInputStream objectStream = new ObjectInputStream(new ByteArrayInputStream(binary))) {
      return (Work) objectStream.readObject();
    }
  } catch (ClassNotFoundException | IOException e) {
    throw bomb(e);
  }
}

代码示例来源:origin: Meituan-Dianping/walle

mainAttrs.putValue(
    getManifestDigestAttributeName(manifestDigestAlgorithm),
    Base64.getEncoder().encodeToString(md.digest(manifest.contents)));
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
  SignatureFileWriter.writeMainSection(out, mainAttrs);
  String sectionName = manifestSection.getKey();
  byte[] sectionContents = manifestSection.getValue();
  byte[] sectionDigest = md.digest(sectionContents);
  Attributes attrs = new Attributes();
  attrs.putValue(
      entryDigestAttributeName,
      Base64.getEncoder().encodeToString(sectionDigest));
if ((out.size() > 0) && ((out.size() % 1024) == 0)) {
  try {
    SignatureFileWriter.writeSectionDelimiter(out);

代码示例来源:origin: wildfly/wildfly

protected static String createExpectedResponse(String secKey) throws IOException {
    try {
      final String concat = secKey + MAGIC_NUMBER;
      final MessageDigest digest = MessageDigest.getInstance("SHA1");

      digest.update(concat.getBytes("UTF-8"));
      final byte[] bytes = digest.digest();
      return Base64.getEncoder().encodeToString(bytes);
    } catch (NoSuchAlgorithmException e) {
      throw new IOException(e);
    }

  }
}

代码示例来源:origin: wildfly/wildfly

private String serialize(final Serializable serializable) {
  if (serializable == null) {
    return null;
  }
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  try {
    final Marshaller marshaller = factory.createMarshaller(configuration);
    marshaller.start(new OutputStreamByteOutput(out));
    marshaller.writeObject(serializable);
    marshaller.finish();
    out.flush();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
  return Base64.getEncoder().encodeToString(out.toByteArray());
}

代码示例来源:origin: line/armeria

/**
 * Encodes the specified {@code message} into a deflated base64 string.
 */
static String toDeflatedBase64(SAMLObject message) {
  requireNonNull(message, "message");
  final String messageStr;
  try {
    messageStr = nodeToString(XMLObjectSupport.marshall(message));
  } catch (MarshallingException e) {
    throw new SamlException("failed to serialize a SAML message", e);
  }
  final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
  try (DeflaterOutputStream deflaterStream =
         new DeflaterOutputStream(Base64.getEncoder().wrap(bytesOut),
                     new Deflater(Deflater.DEFLATED, true))) {
    deflaterStream.write(messageStr.getBytes(StandardCharsets.UTF_8));
  } catch (IOException e) {
    throw new SamlException("failed to deflate a SAML message", e);
  }
  return bytesOut.toString();
}

代码示例来源:origin: baomidou/mybatis-plus

/**
   * MD5 Base64 加密
   *
   * @param str 待加密的字符串
   * @return 加密后的字符串
   */
  public static String md5Base64(String str) {
    //确定计算方法
    try {
      MessageDigest md5 = MessageDigest.getInstance(Constants.MD5);
      //加密后的字符串
      byte[] src = md5.digest(str.getBytes(StandardCharsets.UTF_8));
      return Base64.getEncoder().encodeToString(src);
    } catch (Exception e) {
      throw ExceptionUtils.mpe(e);
    }
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

ByteArrayInputStream inputBytes = new ByteArrayInputStream(dataBytes);
try (DataInputStream input = new DataInputStream(inputBytes)) {
    ByteBuffer byteBuffer = StandardCharsets.UTF_8.encode(CharBuffer.wrap(chars));
    bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit());
    Arrays.fill(byteBuffer.array(), (byte)0);
      tmpBytes[i] = (byte)chars[i]; // PBE only stores the lower 8 bits, so this narrowing is ok
    bytes = Base64.getDecoder().decode(tmpBytes);
    Arrays.fill(tmpBytes, (byte)0);

代码示例来源:origin: stackoverflow.com

final String authorization = httpRequest.getHeader("Authorization");
 if (authorization != null && authorization.startsWith("Basic")) {
   // Authorization: Basic base64credentials
   String base64Credentials = authorization.substring("Basic".length()).trim();
   String credentials = new String(Base64.getDecoder().decode(base64Credentials),
       Charset.forName("UTF-8"));
   // credentials = username:password
   final String[] values = credentials.split(":",2);

代码示例来源:origin: AsyncHttpClient/async-http-client

public static String md5(byte[] bytes, int offset, int len) {
 try {
  MessageDigest md = MessageDigestUtils.pooledMd5MessageDigest();
  md.update(bytes, offset, len);
  return Base64.getEncoder().encodeToString(md.digest());
 } catch (Exception e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: AsyncHttpClient/async-http-client

public static String getAcceptKey(String key) {
  return Base64.getEncoder().encodeToString(pooledSha1MessageDigest().digest(
       (key + MAGIC_GUID).getBytes(US_ASCII)));
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testGetBinary() {
 byte[] bytes = TestUtils.randomByteArray(100);
 jsonObject.put("foo", bytes);
 assertArrayEquals(bytes, jsonObject.getBinary("foo"));
 assertEquals(Base64.getEncoder().encodeToString(bytes), jsonObject.getValue("foo"));
 String val = jsonObject.getString("foo");
 assertNotNull(val);
 byte[] retrieved = Base64.getDecoder().decode(val);
 assertTrue(TestUtils.byteArraysEqual(bytes, retrieved));

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testGetValue() {
 jsonObject.put("foo", 123);
 assertEquals(123, jsonObject.getValue("foo"));
 jsonObject.put("foo", 123l);
 assertEquals(123l, jsonObject.getValue("foo"));
 jsonObject.put("foo", 123f);
 byte[] bytes = TestUtils.randomByteArray(100);
 jsonObject.put("foo", bytes);
 assertTrue(TestUtils.byteArraysEqual(bytes, Base64.getDecoder().decode((String) jsonObject.getValue("foo"))));
 jsonObject.putNull("foo");
 assertNull(jsonObject.getValue("foo"));

代码示例来源:origin: org.opensingular/singular-server-commons

private static String decompress(String s) throws UnsupportedEncodingException {
  return new String(
      decompressAlgorithm(
          Base64.getUrlDecoder().decode(
              URLDecoder.decode(s, ENCODING.name())
                  .getBytes(ENCODING)
          )
      ),
      ENCODING);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Base64-encode the given byte array.
 * @param src the original byte array
 * @return the encoded byte array
 */
public static byte[] encode(byte[] src) {
  if (src.length == 0) {
    return src;
  }
  return Base64.getEncoder().encode(src);
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testFromJsonObject() {
 JsonObject object = new JsonObject();
 object.put("string", "the_string");
 object.put("integer", 4);
 object.put("boolean", true);
 object.put("binary", "hello".getBytes());
 object.put("object", new JsonObject().put("nested", 4));
 object.put("array", new JsonArray().add(1).add(2).add(3));
 Map<String, Object> map = ConversionHelper.fromObject(object);
 assertEquals(6, map.size());
 assertEquals("the_string", map.get("string"));
 assertEquals(4, map.get("integer"));
 assertEquals(true, map.get("boolean"));
 assertEquals("hello", new String(Base64.getDecoder().decode((String)map.get("binary"))));
 assertEquals(Collections.singletonMap("nested", 4), map.get("object"));
 assertEquals(Arrays.asList(1, 2, 3), map.get("array"));
}

相关文章