summaryrefslogtreecommitdiff
path: root/java-plan2/src/org/msgpack/schema/StringSchema.java
blob: fc6855bfabf0fd2a0b324ad6cd59284d08ca9a6f (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
package org.msgpack.schema;

import java.io.IOException;
import java.nio.charset.Charset;
import org.msgpack.*;

public class StringSchema extends Schema {
	public StringSchema()
	{
		super("string");
	}

	public String getFullName()
	{
		return "String";
	}

	@Override
	public void pack(Packer pk, Object obj) throws IOException
	{
		if(obj == null) {
			pk.packNil();
			return;
		}
		String s = (String)obj;
		byte[] d = s.getBytes("UTF-8");
		pk.packRaw(d.length);
		pk.packRawBody(d);
	}

	@Override
	public Object convert(GenericObject obj)
	{
		return obj.asString();
	}

	@Override
	public Object createRaw(byte[] b, int offset, int length)
	{
		try {
			return new String(b, offset, length, "UTF-8");  // XXX FIXME debug
		} catch (Exception e) {
			// FIXME
			throw new RuntimeException(e.getMessage());
		}
	}
}