1 package com.atlassian.vcache.marshallers;
2
3 import com.atlassian.annotations.Internal;
4 import com.atlassian.vcache.Marshaller;
5 import com.atlassian.vcache.MarshallerException;
6
7 import java.nio.ByteBuffer;
8 import java.nio.CharBuffer;
9 import java.nio.charset.CharacterCodingException;
10 import java.nio.charset.Charset;
11 import java.nio.charset.CharsetEncoder;
12 import java.nio.charset.CodingErrorAction;
13 import java.nio.charset.StandardCharsets;
14 import java.util.Arrays;
15
16
17
18
19
20
21
22 @Internal
23 @Deprecated
24 class StringMarshaller implements Marshaller<String> {
25 private static final Charset ENCODING_CHARSET = StandardCharsets.UTF_8;
26
27 @Override
28 public byte[] marshall(String str) throws MarshallerException {
29
30 final CharsetEncoder encoder = ENCODING_CHARSET.newEncoder();
31 encoder.onMalformedInput(CodingErrorAction.REPORT);
32
33 try {
34 final ByteBuffer buffer = encoder.encode(CharBuffer.wrap(str));
35 return Arrays.copyOf(buffer.array(), buffer.limit());
36 } catch (CharacterCodingException e) {
37 throw new MarshallerException("Unable to encode: " + str, e);
38 }
39 }
40
41 @Override
42 public String unmarshall(byte[] raw) {
43 return new String(raw, ENCODING_CHARSET);
44 }
45 }