|
Commons Codec example source code file (RFC1522Codec.java)
The Commons Codec RFC1522Codec.java source code
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.commons.codec.net;
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.binary.StringUtils;
/**
* <p>
* Implements methods common to all codecs defined in RFC 1522.
* </p>
*
* <p>
* <a href="http://www.ietf.org/rfc/rfc1522.txt">RFC 1522
* describes techniques to allow the encoding of non-ASCII text in
* various portions of a RFC 822 [2] message header, in a manner which
* is unlikely to confuse existing message handling software.
* </p>
* @see <a href="http://www.ietf.org/rfc/rfc1522.txt">
* MIME (Multipurpose Internet Mail Extensions) Part Two:
* Message Header Extensions for Non-ASCII Text</a>
* </p>
*
* @author Apache Software Foundation
* @since 1.3
* @version $Id: RFC1522Codec.java 798428 2009-07-28 07:32:49Z ggregory $
*/
abstract class RFC1522Codec {
/**
* Separator.
*/
protected static final char SEP = '?';
/**
* Prefix
*/
protected static final String POSTFIX = "?=";
/**
* Postfix
*/
protected static final String PREFIX = "=?";
/**
* Applies an RFC 1522 compliant encoding scheme to the given string of text with the
* given charset. This method constructs the "encoded-word" header common to all the
* RFC 1522 codecs and then invokes {@link #doEncoding(byte [])} method of a concrete
* class to perform the specific enconding.
*
* @param text a string to encode
* @param charset a charset to be used
*
* @return RFC 1522 compliant "encoded-word"
*
* @throws EncoderException thrown if there is an error conidition during the Encoding
* process.
* @throws UnsupportedEncodingException thrown if charset is not supported
*
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets
*/
protected String encodeText(final String text, final String charset)
throws EncoderException, UnsupportedEncodingException
{
if (text == null) {
return null;
}
StringBuffer buffer = new StringBuffer();
buffer.append(PREFIX);
buffer.append(charset);
buffer.append(SEP);
buffer.append(getEncoding());
buffer.append(SEP);
byte [] rawdata = doEncoding(text.getBytes(charset));
buffer.append(StringUtils.newStringUsAscii(rawdata));
buffer.append(POSTFIX);
return buffer.toString();
}
/**
* Applies an RFC 1522 compliant decoding scheme to the given string of text. This method
* processes the "encoded-word" header common to all the RFC 1522 codecs and then invokes
* {@link #doEncoding(byte [])} method of a concrete class to perform the specific deconding.
*
* @param text a string to decode
* @return A new decoded String or <code>null if the input is
Other Commons Codec examples (source code examples)Here is a short list of links related to this Commons Codec RFC1522Codec.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.