summaryrefslogtreecommitdiff
path: root/java-plan2/src/org/msgpack/GenericRaw.java
blob: 6de03b283035f36e6f8e79b45549ee44bd92efb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package org.msgpack;

import java.nio.charset.Charset;

public class GenericRaw extends GenericObject {
	byte[] bytes;
	String string;

	public GenericRaw()
	{
		this.bytes = new byte[0];
		this.string = null;
	}

	public GenericRaw(byte[] bytes)
	{
		this.bytes = bytes;
		this.string = null;
	}

	public GenericRaw(String string)
	{
		this.bytes = null;
		this.string = string;
	}

	public synchronized void setString(String string)
	{
		this.string = string;
		this.bytes = null;
	}

	public synchronized void setBytes(byte[] bytes)
	{
		this.bytes = bytes;
		this.string = null;
	}

	private static Charset UTF8_CHARSET = Charset.forName("UTF-8");

	@Override
	public synchronized String asString()
	{
		if(string == null) {
			return string = new String(bytes, UTF8_CHARSET);
		}
		return string;
	}

	@Override
	public synchronized byte[] asBytes()
	{
		if(bytes == null) {
			return bytes = string.getBytes(UTF8_CHARSET);
		}
		return bytes;
	}
}