From 706293aadca888d637c443e5304866cbfd253636 Mon Sep 17 00:00:00 2001 From: FURUHASHI Sadayuki Date: Tue, 30 Nov 2010 15:34:32 +0900 Subject: [PATCH] java: adds type.Raw --- java/src/main/java/org/msgpack/type/Raw.java | 90 ++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 java/src/main/java/org/msgpack/type/Raw.java diff --git a/java/src/main/java/org/msgpack/type/Raw.java b/java/src/main/java/org/msgpack/type/Raw.java new file mode 100644 index 00000000..24d4b444 --- /dev/null +++ b/java/src/main/java/org/msgpack/type/Raw.java @@ -0,0 +1,90 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.type; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.nio.ByteBuffer; +import org.msgpack.*; + +public final class Raw/* implements MessagePackable, MessageUnpackable, MessageConvertable*/ { + private byte[] bytes; + private String string; + + /* + public Raw() { + this.bytes = null; + this.string = ""; + } + */ + + public Raw(byte[] bytes) { + this.bytes = bytes; + this.string = null; + } + + public Raw(String string) { + this.bytes = null; + this.string = string; + } + + public String toString() { + if(string == null) { + try { + string = new String(bytes, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + return string; + } + + public byte[] toByteArray() { + if(bytes == null) { + try { + bytes = string.getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + return bytes; + } + + public ByteBuffer toByteBuffer() { + return ByteBuffer.wrap(toByteArray()); + } + + /* + public void messagePack(Packer pk) throws IOException { + pk.packByteArray(toByteArray()); + } + + public void messageUnpack(Unpacker pac) throws IOException, MessageTypeException { + // FIXME immutable + this.bytes = pac.unpackByteArray(); + this.string = null; + } + + public void messageConvert(MessagePackObject obj) throws MessageTypeException { + // FIXME immutable + this.bytes = obj.asByteArray(); + this.string = null; + } + */ +} +