View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.mime4j.util;
21  
22  import java.nio.ByteBuffer;
23  import java.nio.CharBuffer;
24  import java.nio.charset.Charset;
25  
26  /**
27   * Utility methods for converting textual content of a message.
28   */
29  public class ContentUtil {
30  
31      private ContentUtil() {
32      }
33  
34      /**
35       * Encodes the specified string into an immutable sequence of bytes using
36       * the US-ASCII charset.
37       * 
38       * @param string
39       *            string to encode.
40       * @return encoded string as an immutable sequence of bytes.
41       */
42      public static ByteSequence encode(String string) {
43          return encode(CharsetUtil.US_ASCII, string);
44      }
45  
46      /**
47       * Encodes the specified string into an immutable sequence of bytes using
48       * the specified charset.
49       * 
50       * @param charset
51       *            Java charset to be used for the conversion.
52       * @param string
53       *            string to encode.
54       * @return encoded string as an immutable sequence of bytes.
55       */
56      public static ByteSequence encode(Charset charset, String string) {
57          ByteBuffer encoded = charset.encode(CharBuffer.wrap(string));
58          ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining());
59          bab.append(encoded.array(), encoded.position(), encoded.remaining());
60          return bab;
61      }
62  
63      /**
64       * Decodes the specified sequence of bytes into a string using the US-ASCII
65       * charset.
66       * 
67       * @param byteSequence
68       *            sequence of bytes to decode.
69       * @return decoded string.
70       */
71      public static String decode(ByteSequence byteSequence) {
72          return decode(CharsetUtil.US_ASCII, byteSequence, 0, byteSequence
73                  .length());
74      }
75  
76      /**
77       * Decodes the specified sequence of bytes into a string using the specified
78       * charset.
79       * 
80       * @param charset
81       *            Java charset to be used for the conversion.
82       * @param byteSequence
83       *            sequence of bytes to decode.
84       * @return decoded string.
85       */
86      public static String decode(Charset charset, ByteSequence byteSequence) {
87          return decode(charset, byteSequence, 0, byteSequence.length());
88      }
89  
90      /**
91       * Decodes a sub-sequence of the specified sequence of bytes into a string
92       * using the US-ASCII charset.
93       * 
94       * @param byteSequence
95       *            sequence of bytes to decode.
96       * @param offset
97       *            offset into the byte sequence.
98       * @param length
99       *            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 }