mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-04-16 23:12:57 +02:00
java: adds type.Raw
This commit is contained in:
parent
dfdceb4258
commit
706293aadc
90
java/src/main/java/org/msgpack/type/Raw.java
Normal file
90
java/src/main/java/org/msgpack/type/Raw.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user