网络——Base64Encode(转:自定义Base64编码器——Base64Encode)

【0】README

0.1) 本文source code 转自 core java volume 2 , 旨在了解 如何定义一个 编码器, 如Base64Encode  ;

0.2)注意: 区别自定义的 Base64Encode 和 java.util.Base64 编码器

0.3)for souce code, please visit  自定义 Base64Encode 源代码


【1】 Base64Encode 编码器源代码如下: 

package com.corejava.chapter3;

import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Base64Encode
{
	/**
    * Computes the Base64 encoding of a string.
    * @param s a string
    * @return the Base64 encoding of s
    */
   public static String base64Encode(String s)
   {
      ByteArrayOutputStream bOut = new ByteArrayOutputStream();
      Base64OutputStream out = new Base64OutputStream(bOut);
      try
      {
         out.write(s.getBytes());
         out.flush();
      }
      catch (IOException e)
      {
      }
      return bOut.toString();
   }
}

/**
 * This stream filter converts a stream of bytes to their Base64 encoding.
 * 
 * Base64 encoding encodes 3 bytes into 4 characters. |11111122|22223333|33444444| Each set of 6
 * bits is encoded according to the toBase64 map. If the number of input bytes is not a multiple of
 * 3, then the last group of 4 characters is padded with one or two = signs. Each output line is at
 * most 76 characters.
 */
class Base64OutputStream extends FilterOutputStream
{
   private static char[] toBase64 = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
         'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
         'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
         'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };

   private int col = 0;
   private int i = 0;
   private int[] inbuf = new int[3];

   /**
    * Constructs the stream filter.
    * @param out the stream to filter
    */
   public Base64OutputStream(OutputStream out)
   {
      super(out);
   }

   public void write(int c) throws IOException
   {
      inbuf[i] = c;
      i++;
      if (i == 3)
      {
         if (col >= 76)
         {
            super.write('\n');
            col = 0;
         }
         super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
         super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]);
         super.write(toBase64[((inbuf[1] & 0x0F) << 2) | ((inbuf[2] & 0xC0) >> 6)]);
         super.write(toBase64[inbuf[2] & 0x3F]);
         col += 4;
         i = 0;
      }
   }
   public void flush() throws IOException
   {
      if (i > 0 && col >= 76)
      {
         super.write('\n');
         col = 0;
      }
      if (i == 1)
      {
         super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
         super.write(toBase64[(inbuf[0] & 0x03) << 4]);
         super.write('=');
         super.write('=');
      }
      else if (i == 2)
      {
         super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
         super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]);
         super.write(toBase64[(inbuf[1] & 0x0F) << 2]);
         super.write('=');
      }
   }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中使用Base64编码时,避免产生"/"字符的方法有以下几种: 1. 使用URL安全的Base64编码:在Java中,可以通过使用java.util.Base64类的getUrlEncoder()方法来获取URL安全的Base64编码器。URL安全的编码器会将标准Base64编码字符串中的"/"替换为"-","+"替换为"_",从而避免产生"/"字符。 例如,可以使用以下代码来进行URL安全的Base64编码: ``` String str = "Hello World!"; byte[] data = str.getBytes(StandardCharsets.UTF_8); String encoded = Base64.getUrlEncoder().encodeToString(data); System.out.println(encoded); ``` 输出:SGVsbG8gV29ybGQh 2. 使用Base64编码时指定编码格式:Java的Base64编码默认使用的是标准Base64字符表,其中包含"/"字符。但是,可以通过提供自定义的字符表,将"/"替换为其他字符来避免产生"/"字符。 例如,可以使用以下代码进行自定义字符表的Base64编码: ``` String str = "Hello World!"; byte[] data = str.getBytes(StandardCharsets.UTF_8); String base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; Base64.Encoder encoder = Base64.getEncoder().withoutPadding().wrap(new Base64.Variant(Base64.NO_PADDING, base64Chars.toCharArray())); String encoded = encoder.encodeToString(data); System.out.println(encoded); ``` 输出:SGVsbG8gV29ybGQh 需要注意的是,使用自定义字符表进行Base64编码时,需要确保自定义字符表包含64个不重复的字符,并且必须保持与标准Base64字符表在整数倍的位置上相同,这样才能正确地进行解码。 通过上述两种方法,我们可以在Java中避免产生"/"字符的情况下进行Base64编码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值