| 1 |
/* |
| 2 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
* you may not use this file except in compliance with the License. |
| 4 |
* You may obtain a copy of the License at |
| 5 |
* |
| 6 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
* |
| 8 |
* Unless required by applicable law or agreed to in writing, software |
| 9 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
* See the License for the specific language governing permissions and |
| 12 |
* limitations under the License. |
| 13 |
*/ |
| 14 |
package io.airlift.slice; |
| 15 |
|
| 16 |
import java.nio.ByteBuffer; |
| 17 |
import java.nio.CharBuffer; |
| 18 |
import java.nio.charset.CharacterCodingException; |
| 19 |
import java.nio.charset.Charset; |
| 20 |
import java.nio.charset.CharsetDecoder; |
| 21 |
import java.nio.charset.CoderResult; |
| 22 |
import java.nio.charset.CodingErrorAction; |
| 23 |
import java.util.IdentityHashMap; |
| 24 |
import java.util.Map; |
| 25 |
|
| 26 |
import static io.airlift.slice.Preconditions.checkNotNull; |
| 27 |
|
| 28 |
final class StringDecoder |
| 29 |
{ |
| 30 |
private static final ThreadLocal<Map<Charset, CharsetDecoder>> decoders = new ThreadLocal<Map<Charset, CharsetDecoder>>() |
| 31 |
{ |
| 32 |
@Override |
| 33 |
protected Map<Charset, CharsetDecoder> initialValue() |
| 34 |
{ |
| 35 |
return new IdentityHashMap<>(); |
| 36 |
} |
| 37 |
}; |
| 38 |
|
| 39 |
private StringDecoder() {} |
| 40 |
|
| 41 |
@SuppressWarnings("ObjectToString") |
| 42 |
public static String decodeString(ByteBuffer src, Charset charset) |
| 43 |
{ |
| 44 |
CharsetDecoder decoder = getDecoder(charset); |
| 45 |
CharBuffer dst = CharBuffer.allocate((int) ((double) src.remaining() * decoder.maxCharsPerByte())); |
| 46 |
try { |
| 47 |
CoderResult cr = decoder.decode(src, dst, true); |
| 48 |
if (!cr.isUnderflow()) { |
| 49 |
cr.throwException(); |
| 50 |
} |
| 51 |
cr = decoder.flush(dst); |
| 52 |
if (!cr.isUnderflow()) { |
| 53 |
cr.throwException(); |
| 54 |
} |
| 55 |
} |
| 56 |
catch (CharacterCodingException x) { |
| 57 |
throw new IllegalStateException(x); |
| 58 |
} |
| 59 |
return dst.flip().toString(); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns a cached thread-local {@link java.nio.charset.CharsetDecoder} for the specified <tt>charset</tt>. |
| 64 |
*/ |
| 65 |
private static CharsetDecoder getDecoder(Charset charset) |
| 66 |
{ |
| 67 |
checkNotNull(charset, "charset is null"); |
| 68 |
|
| 69 |
Map<Charset, CharsetDecoder> map = decoders.get(); |
| 70 |
CharsetDecoder d = map.get(charset); |
| 71 |
if (d != null) { |
| 72 |
d.reset(); |
| 73 |
d.onMalformedInput(CodingErrorAction.REPLACE); |
| 74 |
d.onUnmappableCharacter(CodingErrorAction.REPLACE); |
| 75 |
return d; |
| 76 |
} |
| 77 |
|
| 78 |
d = charset.newDecoder(); |
| 79 |
d.onMalformedInput(CodingErrorAction.REPLACE); |
| 80 |
d.onUnmappableCharacter(CodingErrorAction.REPLACE); |
| 81 |
map.put(charset, d); |
| 82 |
return d; |
| 83 |
} |
| 84 |
} |