001    /****************************************************************
002     * Licensed to the Apache Software Foundation (ASF) under one   *
003     * or more contributor license agreements.  See the NOTICE file *
004     * distributed with this work for additional information        *
005     * regarding copyright ownership.  The ASF licenses this file   *
006     * to you under the Apache License, Version 2.0 (the            *
007     * "License"); you may not use this file except in compliance   *
008     * with the License.  You may obtain a copy of the License at   *
009     *                                                              *
010     *   http://www.apache.org/licenses/LICENSE-2.0                 *
011     *                                                              *
012     * Unless required by applicable law or agreed to in writing,   *
013     * software distributed under the License is distributed on an  *
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015     * KIND, either express or implied.  See the License for the    *
016     * specific language governing permissions and limitations      *
017     * under the License.                                           *
018     ****************************************************************/
019    
020    package org.apache.james.mime4j.util;
021    
022    import java.nio.ByteBuffer;
023    import java.nio.CharBuffer;
024    import java.nio.charset.Charset;
025    
026    /**
027     * Utility methods for converting textual content of a message.
028     */
029    public class ContentUtil {
030    
031        private ContentUtil() {
032        }
033    
034        /**
035         * Encodes the specified string into an immutable sequence of bytes using
036         * the US-ASCII charset.
037         * 
038         * @param string
039         *            string to encode.
040         * @return encoded string as an immutable sequence of bytes.
041         */
042        public static ByteSequence encode(String string) {
043            return encode(CharsetUtil.US_ASCII, string);
044        }
045    
046        /**
047         * Encodes the specified string into an immutable sequence of bytes using
048         * the specified charset.
049         * 
050         * @param charset
051         *            Java charset to be used for the conversion.
052         * @param string
053         *            string to encode.
054         * @return encoded string as an immutable sequence of bytes.
055         */
056        public static ByteSequence encode(Charset charset, String string) {
057            ByteBuffer encoded = charset.encode(CharBuffer.wrap(string));
058            ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining());
059            bab.append(encoded.array(), encoded.position(), encoded.remaining());
060            return bab;
061        }
062    
063        /**
064         * Decodes the specified sequence of bytes into a string using the US-ASCII
065         * charset.
066         * 
067         * @param byteSequence
068         *            sequence of bytes to decode.
069         * @return decoded string.
070         */
071        public static String decode(ByteSequence byteSequence) {
072            return decode(CharsetUtil.US_ASCII, byteSequence, 0, byteSequence
073                    .length());
074        }
075    
076        /**
077         * Decodes the specified sequence of bytes into a string using the specified
078         * charset.
079         * 
080         * @param charset
081         *            Java charset to be used for the conversion.
082         * @param byteSequence
083         *            sequence of bytes to decode.
084         * @return decoded string.
085         */
086        public static String decode(Charset charset, ByteSequence byteSequence) {
087            return decode(charset, byteSequence, 0, byteSequence.length());
088        }
089    
090        /**
091         * Decodes a sub-sequence of the specified sequence of bytes into a string
092         * using the US-ASCII charset.
093         * 
094         * @param byteSequence
095         *            sequence of bytes to decode.
096         * @param offset
097         *            offset into the byte sequence.
098         * @param length
099         *            number of bytes.
100         * @return decoded string.
101         */
102        public static String decode(ByteSequence byteSequence, int offset,
103                int length) {
104            return decode(CharsetUtil.US_ASCII, byteSequence, offset, length);
105        }
106    
107        /**
108         * Decodes a sub-sequence of the specified sequence of bytes into a string
109         * using the specified charset.
110         * 
111         * @param charset
112         *            Java charset to be used for the conversion.
113         * @param byteSequence
114         *            sequence of bytes to decode.
115         * @param offset
116         *            offset into the byte sequence.
117         * @param length
118         *            number of bytes.
119         * @return decoded string.
120         */
121        public static String decode(Charset charset, ByteSequence byteSequence,
122                int offset, int length) {
123            if (byteSequence instanceof ByteArrayBuffer) {
124                ByteArrayBuffer bab = (ByteArrayBuffer) byteSequence;
125                return decode(charset, bab.buffer(), offset, length);
126            } else {
127                byte[] bytes = byteSequence.toByteArray();
128                return decode(charset, bytes, offset, length);
129            }
130        }
131    
132        private static String decode(Charset charset, byte[] buffer, int offset,
133                int length) {
134            return charset.decode(ByteBuffer.wrap(buffer, offset, length))
135                    .toString();
136        }
137    
138    }