0
|
1 //
|
|
2 // MessagePack for Java
|
|
3 //
|
|
4 // Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
|
|
5 //
|
|
6 // Licensed under the Apache License, Version 2.0 (the "License");
|
|
7 // you may not use this file except in compliance with the License.
|
|
8 // 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, software
|
|
13 // distributed under the License is distributed on an "AS IS" BASIS,
|
|
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15 // See the License for the specific language governing permissions and
|
|
16 // limitations under the License.
|
|
17 //
|
|
18 package org.msgpack.type;
|
|
19
|
|
20 import java.util.Arrays;
|
|
21 import java.io.IOException;
|
|
22 import java.nio.ByteBuffer;
|
|
23 import java.nio.charset.Charset;
|
|
24 import java.nio.charset.CharacterCodingException;
|
|
25 import java.nio.charset.CharsetDecoder;
|
|
26 import java.nio.charset.CodingErrorAction;
|
|
27 import org.msgpack.packer.Packer;
|
|
28 import org.msgpack.MessageTypeException;
|
|
29
|
|
30 class ByteArrayRawValueImpl extends AbstractRawValue {
|
|
31 private static ByteArrayRawValueImpl emptyInstance = new ByteArrayRawValueImpl(new byte[0], true);
|
|
32
|
|
33 public static RawValue getEmptyInstance() {
|
|
34 return emptyInstance;
|
|
35 }
|
|
36
|
|
37 private static final ThreadLocal<CharsetDecoder> decoderStore = new ThreadLocal<CharsetDecoder>() {
|
|
38 @Override
|
|
39 protected CharsetDecoder initialValue() {
|
|
40 return Charset.forName("UTF-8").newDecoder()
|
|
41 .onMalformedInput(CodingErrorAction.REPORT)
|
|
42 .onUnmappableCharacter(CodingErrorAction.REPORT);
|
|
43 }
|
|
44 };
|
|
45
|
|
46 private byte[] bytes;
|
|
47
|
|
48 ByteArrayRawValueImpl(byte[] bytes, boolean gift) {
|
|
49 if (gift) {
|
|
50 this.bytes = bytes;
|
|
51 } else {
|
|
52 this.bytes = new byte[bytes.length];
|
|
53 System.arraycopy(bytes, 0, this.bytes, 0, bytes.length);
|
|
54 }
|
|
55 }
|
|
56
|
|
57 ByteArrayRawValueImpl(byte[] b, int off, int len) {
|
|
58 // TODO reference
|
|
59 this.bytes = new byte[len];
|
|
60 System.arraycopy(b, off, this.bytes, 0, len);
|
|
61 }
|
|
62
|
|
63 @Override
|
|
64 public byte[] getByteArray() {
|
|
65 return bytes;
|
|
66 }
|
|
67
|
|
68 @Override
|
|
69 public String getString() {
|
|
70 CharsetDecoder decoder = decoderStore.get();
|
|
71 try {
|
|
72 return decoder.decode(ByteBuffer.wrap(bytes)).toString();
|
|
73 } catch (CharacterCodingException ex) {
|
|
74 throw new MessageTypeException(ex);
|
|
75 }
|
|
76 }
|
|
77
|
|
78 @Override
|
|
79 public void writeTo(Packer pk) throws IOException {
|
|
80 pk.write(bytes);
|
|
81 }
|
|
82
|
|
83 @Override
|
|
84 public boolean equals(Object o) {
|
|
85 if (this == o) {
|
|
86 return true;
|
|
87 }
|
|
88 if (!(o instanceof Value)) {
|
|
89 return false;
|
|
90 }
|
|
91 Value v = (Value) o;
|
|
92 if (!v.isRawValue()) {
|
|
93 return false;
|
|
94 }
|
|
95
|
|
96 return Arrays.equals(bytes, v.asRawValue().getByteArray());
|
|
97 }
|
|
98
|
|
99 @Override
|
|
100 public int hashCode() {
|
|
101 return Arrays.hashCode(bytes);
|
|
102 }
|
|
103 }
|