写点什么呢?不写了,直接贴代码. 懂的不写也懂,不懂的等用到的时候就懂了

/**

* Copyright 2001-2004 The Apache Software Foundation.

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

import java.io.ByteArrayOutputStream;

import java.io.UnsupportedEncodingException;

import java.util.BitSet;

import org.apache.commons.codec.BinaryDecoder;

import org.apache.commons.codec.BinaryEncoder;

import org.apache.commons.codec.DecoderException;

import org.apache.commons.codec.EncoderException;

import org.apache.commons.codec.StringDecoder;

import org.apache.commons.codec.StringEncoder;

/***

*

* Codec for the Quoted-Printable section of RFC 1521 .

*

*

* The Quoted-Printable encoding is intended to represent data that largely consists of octets that correspond to

* printable characters in the ASCII character set. It encodes the data in such a way that the resulting octets are

* unlikely to be modified by mail transport. If the data being encoded are mostly ASCII text, the encoded form of the

* data remains largely recognizable by humans. A body which is entirely ASCII may also be encoded in Quoted-Printable

* to ensure the integrity of the data should the message pass through a character- translating, and/or line-wrapping

* gateway.

*

*

*

* Note:

*

*

* Rules #3, #4, and #5 of the quoted-printable spec are not implemented yet because the complete quoted-printable spec

* does not lend itself well into the byte[] oriented codec framework. Complete the codec once the steamable codec

* framework is ready. The motivation behind providing the codec in a partial form is that it can already come in handy

* for those applications that do not require quoted-printable line formatting (rules #3, #4, #5), for instance Q codec.

*

*

* @see RFC 1521 MIME (Multipurpose Internet Mail Extensions) Part One:

* Mechanisms for Specifying and Describing the Format of Internet Message Bodies

*

* @author Apache Software Foundation

* @since 1.3

* @version $Id: QuotedPrintableCodec.java,v 1.7 2004/04/09 22:21:07 ggregory Exp $

*/

public class QuotedPrintableCodec implements BinaryEncoder, BinaryDecoder, StringEncoder, StringDecoder {

/***

* The default charset used for string decoding and encoding.

*/

private String charset = UTF8;

private static final String US_ASCII = "US-ASCII";

private static final String UTF8 = "UTF-8";

/***

* BitSet of printable characters as defined in RFC 1521.

*/

private static final BitSet PRINTABLE_CHARS = new BitSet(256);

private static byte ESCAPE_CHAR = '=';

private static byte TAB = 9;

private static byte SPACE = 32;

// Static initializer for printable chars collection

static {

// alpha characters

for (int i = 33; i <= 60; i++) {

PRINTABLE_CHARS.set(i);

}

for (int i = 62; i <= 126; i++) {

PRINTABLE_CHARS.set(i);

}

PRINTABLE_CHARS.set(TAB);

PRINTABLE_CHARS.set(SPACE);

}

/***

* Default constructor.

*/

public QuotedPrintableCodec() {

super();

}

/***

* Constructor which allows for the selection of a default charset

*

* @param charset

* the default string charset to use.

*/

public QuotedPrintableCodec(String charset) {

super();

this.charset = charset;

}

/***

* Encodes byte into its quoted-printable representation.

*

* @param b

* byte to encode

* @param buffer

* the buffer to write to

*/

private static final void encodeQuotedPrintable(int b, ByteArrayOutputStream buffer) {

buffer.write(ESCAPE_CHAR);

char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16));

char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));

buffer.write(hex1);

buffer.write(hex2);

}

/***

* Encodes an array of bytes into an array of quoted-printable 7-bit characters. Unsafe characters are escaped.

*

*

* This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in

* RFC 1521 and is suitable for encoding binary data and unformatted text.

*

*

* @param printable

* bitset of characters deemed quoted-printable

* @param bytes

* array of bytes to be encoded

* @return array of bytes containing quoted-printable data

*/

public static final byte[] encodeQuotedPrintable(BitSet printable, byte[] bytes) {

if (bytes == null) {

return null;

}

if (printable == null) {

printable = PRINTABLE_CHARS;

}

ByteArrayOutputStream buffer = new ByteArrayOutputStream();

for (int i = 0; i < bytes.length; i++) {

int b = bytes[i];

if (b < 0) {

b = 256 + b;

}

if (printable.get(b)) {

buffer.write(b);

} else {

encodeQuotedPrintable(b, buffer);

}

}

return buffer.toByteArray();

}

/***

* Decodes an array quoted-printable characters into an array of original bytes. Escaped characters are converted

* back to their original representation.

*

*

* This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in

* RFC 1521.

*

*

* @param bytes

* array of quoted-printable characters

* @return array of original bytes

* @throws DecoderException

* Thrown if quoted-printable decoding is unsuccessful

*/

public static final byte[] decodeQuotedPrintable(byte[] bytes) throws DecoderException {

if (bytes == null) {

return null;

}

ByteArrayOutputStream buffer = new ByteArrayOutputStream();

for (int i = 0; i < bytes.length; i++) {

int b = bytes[i];

if (b == ESCAPE_CHAR) {

try {

int u = Character.digit((char) bytes[++i], 16);

int l = Character.digit((char) bytes[++i], 16);

if (u == -1 || l == -1) {

throw new DecoderException("Invalid quoted-printable encoding");

}

buffer.write((char) ((u << 4) + l));

} catch (ArrayIndexOutOfBoundsException e) {

throw new DecoderException("Invalid quoted-printable encoding");

}

} else {

buffer.write(b);

}

}

return buffer.toByteArray();

}

/***

* Encodes an array of bytes into an array of quoted-printable 7-bit characters. Unsafe characters are escaped.

*

*

* This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in

* RFC 1521 and is suitable for encoding binary data and unformatted text.

*

*

* @param bytes

* array of bytes to be encoded

* @return array of bytes containing quoted-printable data

*/

public byte[] encode(byte[] bytes) {

return encodeQuotedPrintable(PRINTABLE_CHARS, bytes);

}

/***

* Decodes an array of quoted-printable characters into an array of original bytes. Escaped characters are converted

* back to their original representation.

*

*

* This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in

* RFC 1521.

*

*

* @param bytes

* array of quoted-printable characters

* @return array of original bytes

* @throws DecoderException

* Thrown if quoted-printable decoding is unsuccessful

*/

public byte[] decode(byte[] bytes) throws DecoderException {

return decodeQuotedPrintable(bytes);

}

/***

* Encodes a string into its quoted-printable form using the default string charset. Unsafe characters are escaped.

*

*

* This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in

* RFC 1521 and is suitable for encoding binary data.

*

*

* @param pString

* string to convert to quoted-printable form

* @return quoted-printable string

*

* @throws EncoderException

* Thrown if quoted-printable encoding is unsuccessful

*

* @see #getDefaultCharset()

*/

public String encode(String pString) throws EncoderException {

if (pString == null) {

return null;

}

try {

return encode(pString, getDefaultCharset());

} catch (UnsupportedEncodingException e) {

throw new EncoderException(e.getMessage());

}

}

/***

* Decodes a quoted-printable string into its original form using the specified string charset. Escaped characters

* are converted back to their original representation.

*

* @param pString

* quoted-printable string to convert into its original form

* @param charset

* the original string charset

* @return original string

* @throws DecoderException

* Thrown if quoted-printable decoding is unsuccessful

* @throws UnsupportedEncodingException

* Thrown if charset is not supported

*/

public String decode(String pString, String charset) throws DecoderException, UnsupportedEncodingException {

if (pString == null) {

return null;

}

return new String(decode(pString.getBytes(US_ASCII)), charset);

}

/***

* Decodes a quoted-printable string into its original form using the default string charset. Escaped characters are

* converted back to their original representation.

*

* @param pString

* quoted-printable string to convert into its original form

* @return original string

* @throws DecoderException

* Thrown if quoted-printable decoding is unsuccessful

* @throws UnsupportedEncodingException

* Thrown if charset is not supported

* @see #getDefaultCharset()

*/

public String decode(String pString) throws DecoderException {

if (pString == null) {

return null;

}

try {

return decode(pString, getDefaultCharset());

} catch (UnsupportedEncodingException e) {

throw new DecoderException(e.getMessage());

}

}

/***

* Encodes an object into its quoted-printable safe form. Unsafe characters are escaped.

*

* @param pObject

* string to convert to a quoted-printable form

* @return quoted-printable object

* @throws EncoderException

* Thrown if quoted-printable encoding is not applicable to objects of this type or if encoding is

* unsuccessful

*/

public Object encode(Object pObject) throws EncoderException {

if (pObject == null) {

return null;

} else if (pObject instanceof byte[]) {

return encode((byte[]) pObject);

} else if (pObject instanceof String) {

return encode((String) pObject);

} else {

throw new EncoderException("Objects of type "

+ pObject.getClass().getName()

+ " cannot be quoted-printable encoded");

}

}

/***

* Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original

* representation.

*

* @param pObject

* quoted-printable object to convert into its original form

* @return original object

* @throws DecoderException

* Thrown if quoted-printable decoding is not applicable to objects of this type if decoding is

* unsuccessful

*/

public Object decode(Object pObject) throws DecoderException {

if (pObject == null) {

return null;

} else if (pObject instanceof byte[]) {

return decode((byte[]) pObject);

} else if (pObject instanceof String) {

return decode((String) pObject);

} else {

throw new DecoderException("Objects of type "

+ pObject.getClass().getName()

+ " cannot be quoted-printable decoded");

}

}

/***

* Returns the default charset used for string decoding and encoding.

*

* @return the default string charset.

*/

public String getDefaultCharset() {

return this.charset;

}

/***

* Encodes a string into its quoted-printable form using the specified charset. Unsafe characters are escaped.

*

*

* This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in

* RFC 1521 and is suitable for encoding binary data and unformatted text.

*

*

* @param pString

* string to convert to quoted-printable form

* @param charset

* the charset for pString

* @return quoted-printable string

*

* @throws UnsupportedEncodingException

* Thrown if the charset is not supported

*/

public String encode(String pString, String charset) throws UnsupportedEncodingException {

if (pString == null) {

return null;

}

return new String(encode(pString.getBytes(charset)), US_ASCII);

}

}

java quotedprintable_Vcard Quoted-Printable 编码 解码相关推荐

  1. Java Base64加密解密编码解码

    起因 最近因为项目要做等保,需要对用户提交的字段进行加密存储,但是还需要做查询,所以需要能解密. 办法 目前打算采用rsa非对称加密保存数据,实现数据加密和解密,但是很尴尬的发现生成的公钥和秘钥居然是 ...

  2. java linux urlencode_iOS urlEncode编码解码(非过时方法,已解决)

    ios urlEncode解决字符串数据以url的形式传递给web服务器时,字符串中是不允许出现空格和特殊字符的,因此通常需要用到urlEncode技术来对url进行简单的编码,以便更好的传输给服务器 ...

  3. java svgbase64转byte_java 图片进行base64 编码解码

    java 图片进行base64 编码解码 刘振兴 代码分享 2017年06月07日 10555 2条评论 import sun.misc.BASE64Decoder; import sun.misc. ...

  4. URL编码解码工具类

    /****************************************************************************** * CREATETIME : 2016年 ...

  5. java url加密解密,java URL 编码解码,该如何解决

    java URL 编码解码 我写了两个接口 一个是对字符串加密 的,一个是解密的  .加密的可以通过调用接口生成加密字符串如下: Oc0PEwKrLzHqT25hYLhWP5wlk5HROPJoWC3 ...

  6. java学习-http中get请求的非ascii参数如何编码解码探讨

    # 背景: 看着别人项目代码看到一个PathUtils工具类, 里面只有一个方法,String  rebuild(String Path),将路径进行URLDecoder.decode解码,避免路径中 ...

  7. java中文乱码解决之道(五)—–java是如何编码解码的

    编码&解码 1:I/O操作 2:内存 3:数据库 4:javaWeb 下面主要介绍前面两种场景,数据库部分只要设置正确编码格式就不会有什么问题,javaWeb场景过多需要了解URL.get.P ...

  8. java io流学设置编码_Java学习日志(21-2-IO流-基本数据类型与字节数组对象与、编码解码)...

    操作基本数据类型的流对象DataStream /* 可以用于操作基本数据类型数据的流对象 */ import java.io.*; class DataStreamDemo{ public stati ...

  9. Java Base64 编码解码方案总结

    转载自  Java Base64 编码解码方案总结 Base64是一种能将任意Binary资料用64种字元组合成字串的方法,而这个Binary资料和字串资料彼此之间是可以互相转换的,十分方便.在实际应 ...

  10. java 编码解码_深入解析Java中的编码转换以及编码和解码操作

    一.Java编码转换过程 我们总是用一个java类文件和用户进行最直接的交互(输入.输出),这些交互内容包含的文字可能会包含中文.无论这些java类是与数据库交互,还是与前端页面交互,他们的生命周期总 ...

最新文章

  1. Grafana文档(升级Grafana)
  2. Spring - Java/J2EE Application Framework 应用框架
  3. SpringBoot中的Quartz应用
  4. python 数学建模、时间戳_python-在matplotlib中绘制Unix时间戳
  5. cocoa pods Installation
  6. Docker最全教程——从理论到实战(五)
  7. 学完php在学python_写给PHP程序员的 Python学习指南(建议去看原文)
  8. linux I/O-记录锁(record lock)
  9. 前端开发css禁止选中文本
  10. ETL学习总结(2)——ETL数据集成工具之kettle、sqoop、datax、streamSets 比较
  11. 29_2020年12月29日疫情一览
  12. linux常用命令,亲测可用
  13. EtherCAT总线伺服速度控制功能块(H5U PLC)
  14. 【MATLAB生信分析】MATLAB生物信息分析工具箱(一)
  15. html复制标签快捷键,ps复制快捷键ctrl加什么
  16. git报错 fatal: unsafe repository 解决方法 xxx is owned by someone else
  17. Vue ui/vue create创建项目报错:Failed to get response from https://registry.npmjs.org/vue-cli-version-ma
  18. Symbian OS C++程序员编码诀窍
  19. 搭建属于自己的数字IC EDA环境(六):开机自动激活 Synopsys license
  20. 安卓机器人做图软件_机器人管理与开发软件RoboStudio出安卓版本啦~

热门文章

  1. 【C】函数指针——定义一个函数指针数组
  2. 电脑联网处出现黄色感叹号
  3. jPlayer web多媒体播放器使用指南
  4. 《Django开发教程》2.2 Django模型
  5. 千言实体链指赛事登顶,冠军团队经验独家分享
  6. wpf 动态图片显示
  7. 掌上单片机实验室 – 实现PID自整定(11)
  8. 「硬见小百科」4个方面!详解电容、电感的相位差是如何产生的
  9. ue4vr插件_Unreal插件V-Ray旨在把VR带给CAD、BIM用户
  10. 计算机专业英语音标,(完整版)C++必备专业英语单词(已标注音标).doc