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 | if (string == null) { |
---|
44 | return null; |
---|
45 | } |
---|
46 | ByteArrayBuffer buf = new ByteArrayBuffer(string.length()); |
---|
47 | for (int i = 0; i < string.length(); i++) { |
---|
48 | buf.append((byte) string.charAt(i)); |
---|
49 | } |
---|
50 | return buf; |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * Encodes the specified string into an immutable sequence of bytes using |
---|
55 | * the specified charset. |
---|
56 | * |
---|
57 | * @param charset |
---|
58 | * Java charset to be used for the conversion. |
---|
59 | * @param string |
---|
60 | * string to encode. |
---|
61 | * @return encoded string as an immutable sequence of bytes. |
---|
62 | */ |
---|
63 | public static ByteSequence encode(Charset charset, String string) { |
---|
64 | if (string == null) { |
---|
65 | return null; |
---|
66 | } |
---|
67 | if (charset == null) { |
---|
68 | charset = Charset.defaultCharset(); |
---|
69 | } |
---|
70 | ByteBuffer encoded = charset.encode(CharBuffer.wrap(string)); |
---|
71 | ByteArrayBuffer buf = new ByteArrayBuffer(encoded.remaining()); |
---|
72 | buf.append(encoded.array(), encoded.position(), encoded.remaining()); |
---|
73 | return buf; |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * Decodes the specified sequence of bytes into a string using the US-ASCII |
---|
78 | * charset. |
---|
79 | * |
---|
80 | * @param byteSequence |
---|
81 | * sequence of bytes to decode. |
---|
82 | * @return decoded string. |
---|
83 | */ |
---|
84 | public static String decode(ByteSequence byteSequence) { |
---|
85 | if (byteSequence == null) { |
---|
86 | return null; |
---|
87 | } |
---|
88 | return decode(byteSequence, 0, byteSequence.length()); |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * Decodes the specified sequence of bytes into a string using the specified |
---|
93 | * charset. |
---|
94 | * |
---|
95 | * @param charset |
---|
96 | * Java charset to be used for the conversion. |
---|
97 | * @param byteSequence |
---|
98 | * sequence of bytes to decode. |
---|
99 | * @return decoded string. |
---|
100 | */ |
---|
101 | public static String decode(Charset charset, ByteSequence byteSequence) { |
---|
102 | return decode(charset, byteSequence, 0, byteSequence.length()); |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * Decodes a sub-sequence of the specified sequence of bytes into a string |
---|
107 | * using the US-ASCII charset. |
---|
108 | * |
---|
109 | * @param byteSequence |
---|
110 | * sequence of bytes to decode. |
---|
111 | * @param offset |
---|
112 | * offset into the byte sequence. |
---|
113 | * @param length |
---|
114 | * number of bytes. |
---|
115 | * @return decoded string. |
---|
116 | */ |
---|
117 | public static String decode(ByteSequence byteSequence, int offset, int length) { |
---|
118 | if (byteSequence == null) { |
---|
119 | return null; |
---|
120 | } |
---|
121 | StringBuilder buf = new StringBuilder(length); |
---|
122 | for (int i = offset; i < offset + length; i++) { |
---|
123 | buf.append((char) (byteSequence.byteAt(i) & 0xff)); |
---|
124 | } |
---|
125 | return buf.toString(); |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * Decodes a sub-sequence of the specified sequence of bytes into a string |
---|
130 | * using the specified charset. |
---|
131 | * |
---|
132 | * @param charset |
---|
133 | * Java charset to be used for the conversion. |
---|
134 | * @param byteSequence |
---|
135 | * sequence of bytes to decode. |
---|
136 | * @param offset |
---|
137 | * offset into the byte sequence. |
---|
138 | * @param length |
---|
139 | * number of bytes. |
---|
140 | * @return decoded string. |
---|
141 | */ |
---|
142 | public static String decode(Charset charset, ByteSequence byteSequence, |
---|
143 | int offset, int length) { |
---|
144 | if (byteSequence == null) { |
---|
145 | return null; |
---|
146 | } |
---|
147 | if (charset == null) { |
---|
148 | charset = Charset.defaultCharset(); |
---|
149 | } |
---|
150 | if (byteSequence instanceof ByteArrayBuffer) { |
---|
151 | ByteArrayBuffer bab = (ByteArrayBuffer) byteSequence; |
---|
152 | return decode(charset, bab.buffer(), offset, length); |
---|
153 | } else { |
---|
154 | byte[] bytes = byteSequence.toByteArray(); |
---|
155 | return decode(charset, bytes, offset, length); |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | private static String decode(Charset charset, byte[] buffer, int offset, |
---|
160 | int length) { |
---|
161 | return charset.decode(ByteBuffer.wrap(buffer, offset, length)) |
---|
162 | .toString(); |
---|
163 | } |
---|
164 | |
---|
165 | } |
---|