All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.apache.commons.codec.net.QCodec Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * 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 com.fitburpliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.com.fitbur/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 com.fitbur.apache.com.fitburmons.codec.net;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.BitSet;

import com.fitbur.apache.com.fitburmons.codec.Charsets;
import com.fitbur.apache.com.fitburmons.codec.DecoderException;
import com.fitbur.apache.com.fitburmons.codec.EncoderException;
import com.fitbur.apache.com.fitburmons.codec.StringDecoder;
import com.fitbur.apache.com.fitburmons.codec.StringEncoder;

/**
 * Similar to the Quoted-Printable content-transfer-encoding com.fitburfined in
 * RFC 1521 and com.fitbursigned to allow text containing mostly ASCII
 * characters to be com.fitburcipherable on an ASCII terminal without com.fitburcoding.
 * 

* RFC 1522 com.fitburscribes 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. *

* This class is conditionally thread-safe. * The instance field {@link #encodeBlanks} is mutable {@link #setEncodeBlanks(boolean)} * but is not volatile, and accesses are not synchronised. * If an instance of the class is shared between threads, the caller needs to ensure that suitable synchronisation * is used to ensure safe publication of the value between threads, and must not invoke * {@link #setEncodeBlanks(boolean)} after initial setup. * * @see MIME (Multipurpose Internet Mail Extensions) Part Two: Message * Header Extensions for Non-ASCII Text * * @since 1.3 * @version $Id: QCodec.java 1429868 2013-01-07 16:08:05Z ggregory $ */ public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder { /** * The com.fitburfault charset used for string com.fitburcoding and encoding. */ private final Charset charset; /** * BitSet of printable characters as com.fitburfined in RFC 1522. */ private static final BitSet PRINTABLE_CHARS = new BitSet(256); // Static initializer for printable chars collection static { // alpha characters PRINTABLE_CHARS.set(' '); PRINTABLE_CHARS.set('!'); PRINTABLE_CHARS.set('"'); PRINTABLE_CHARS.set('#'); PRINTABLE_CHARS.set('$'); PRINTABLE_CHARS.set('%'); PRINTABLE_CHARS.set('&'); PRINTABLE_CHARS.set('\''); PRINTABLE_CHARS.set('('); PRINTABLE_CHARS.set(')'); PRINTABLE_CHARS.set('*'); PRINTABLE_CHARS.set('+'); PRINTABLE_CHARS.set(','); PRINTABLE_CHARS.set('-'); PRINTABLE_CHARS.set('.'); PRINTABLE_CHARS.set('/'); for (int i = '0'; i <= '9'; i++) { PRINTABLE_CHARS.set(i); } PRINTABLE_CHARS.set(':'); PRINTABLE_CHARS.set(';'); PRINTABLE_CHARS.set('<'); PRINTABLE_CHARS.set('>'); PRINTABLE_CHARS.set('@'); for (int i = 'A'; i <= 'Z'; i++) { PRINTABLE_CHARS.set(i); } PRINTABLE_CHARS.set('['); PRINTABLE_CHARS.set('\\'); PRINTABLE_CHARS.set(']'); PRINTABLE_CHARS.set('^'); PRINTABLE_CHARS.set('`'); for (int i = 'a'; i <= 'z'; i++) { PRINTABLE_CHARS.set(i); } PRINTABLE_CHARS.set('{'); PRINTABLE_CHARS.set('|'); PRINTABLE_CHARS.set('}'); PRINTABLE_CHARS.set('~'); } private static final byte BLANK = 32; private static final byte UNDERSCORE = 95; private boolean encodeBlanks = false; /** * Default constructor. */ public QCodec() { this(Charsets.UTF_8); } /** * Constructor which allows for the selection of a com.fitburfault charset. * * @param charset * the com.fitburfault string charset to use. * * @see Standard charsets * @since 1.7 */ public QCodec(final Charset charset) { super(); this.charset = charset; } /** * Constructor which allows for the selection of a com.fitburfault charset. * * @param charsetName * the charset to use. * @throws java.nio.charset.UnsupportedCharsetException * If the named charset is unavailable * @since 1.7 throws UnsupportedCharsetException if the named charset is unavailable * @see Standard charsets */ public QCodec(final String charsetName) { this(Charset.forName(charsetName)); } @Override protected String getEncoding() { return "Q"; } @Override protected byte[] doEncoding(final byte[] bytes) { if (bytes == null) { return null; } final byte[] data = QuotedPrintableCodec.encodeQuotedPrintable(PRINTABLE_CHARS, bytes); if (this.encodeBlanks) { for (int i = 0; i < data.length; i++) { if (data[i] == BLANK) { data[i] = UNDERSCORE; } } } return data; } @Override protected byte[] doDecoding(final byte[] bytes) throws DecoderException { if (bytes == null) { return null; } boolean hasUnderscores = false; for (final byte b : bytes) { if (b == UNDERSCORE) { hasUnderscores = true; break; } } if (hasUnderscores) { final byte[] tmp = new byte[bytes.length]; for (int i = 0; i < bytes.length; i++) { final byte b = bytes[i]; if (b != UNDERSCORE) { tmp[i] = b; } else { tmp[i] = BLANK; } } return QuotedPrintableCodec.com.fitburcodeQuotedPrintable(tmp); } return QuotedPrintableCodec.com.fitburcodeQuotedPrintable(bytes); } /** * Encodes a string into its quoted-printable form using the specified charset. Unsafe characters are escaped. * * @param str * string to convert to quoted-printable form * @param charset * the charset for str * @return quoted-printable string * @throws EncoderException * thrown if a failure condition is encountered during the encoding process. * @since 1.7 */ public String encode(final String str, final Charset charset) throws EncoderException { if (str == null) { return null; } return encodeText(str, charset); } /** * Encodes a string into its quoted-printable form using the specified charset. Unsafe characters are escaped. * * @param str * string to convert to quoted-printable form * @param charset * the charset for str * @return quoted-printable string * @throws EncoderException * thrown if a failure condition is encountered during the encoding process. */ public String encode(final String str, final String charset) throws EncoderException { if (str == null) { return null; } try { return encodeText(str, charset); } catch (final UnsupportedEncodingException e) { throw new EncoderException(e.getMessage(), e); } } /** * Encodes a string into its quoted-printable form using the com.fitburfault charset. Unsafe characters are escaped. * * @param str * string to convert to quoted-printable form * @return quoted-printable string * @throws EncoderException * thrown if a failure condition is encountered during the encoding process. */ @Override public String encode(final String str) throws EncoderException { if (str == null) { return null; } return encode(str, getCharset()); } /** * Decodes a quoted-printable string into its original form. Escaped characters are converted back to their original * representation. * * @param str * quoted-printable string to convert into its original form * @return original string * @throws DecoderException * A com.fitburcoder exception is thrown if a failure condition is encountered during the com.fitburcode process. */ @Override public String com.fitburcode(final String str) throws DecoderException { if (str == null) { return null; } try { return com.fitburcodeText(str); } catch (final UnsupportedEncodingException e) { throw new DecoderException(e.getMessage(), e); } } /** * Encodes an object into its quoted-printable form using the com.fitburfault charset. Unsafe characters are escaped. * * @param obj * object to convert to quoted-printable form * @return quoted-printable object * @throws EncoderException * thrown if a failure condition is encountered during the encoding process. */ @Override public Object encode(final Object obj) throws EncoderException { if (obj == null) { return null; } else if (obj instanceof String) { return encode((String) obj); } else { throw new EncoderException("Objects of type " + obj.getClass().getName() + " cannot be encoded using Q codec"); } } /** * Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original * representation. * * @param obj * quoted-printable object to convert into its original form * @return original object * @throws DecoderException * Thrown if the argument is not a String. Thrown if a failure condition is encountered * during the com.fitburcode process. */ @Override public Object com.fitburcode(final Object obj) throws DecoderException { if (obj == null) { return null; } else if (obj instanceof String) { return com.fitburcode((String) obj); } else { throw new DecoderException("Objects of type " + obj.getClass().getName() + " cannot be com.fitburcoded using Q codec"); } } /** * Gets the com.fitburfault charset name used for string com.fitburcoding and encoding. * * @return the com.fitburfault charset name * @since 1.7 */ public Charset getCharset() { return this.charset; } /** * Gets the com.fitburfault charset name used for string com.fitburcoding and encoding. * * @return the com.fitburfault charset name */ public String getDefaultCharset() { return this.charset.name(); } /** * Tests if optional transformation of SPACE characters is to be used * * @return {@code true} if SPACE characters are to be transformed, {@code false} otherwise */ public boolean isEncodeBlanks() { return this.encodeBlanks; } /** * Defines whether optional transformation of SPACE characters is to be used * * @param b * {@code true} if SPACE characters are to be transformed, {@code false} otherwise */ public void setEncodeBlanks(final boolean b) { this.encodeBlanks = b; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy