mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-25 19:32:11 +01:00
java: adds array templates
This commit is contained in:
parent
6eedb50f56
commit
16264a5693
@ -0,0 +1,76 @@
|
||||
//
|
||||
// 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.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class BooleanArrayTemplate implements Template {
|
||||
private BooleanArrayTemplate() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if(!(target instanceof boolean[])) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
boolean[] array = (boolean[])target;
|
||||
pk.packArray(array.length);
|
||||
for(boolean a : array) {
|
||||
pk.pack(a);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackArray();
|
||||
boolean[] array;
|
||||
if(to != null && to instanceof boolean[] && ((boolean[])to).length == length) {
|
||||
array = (boolean[])to;
|
||||
} else {
|
||||
array = new boolean[length];
|
||||
}
|
||||
for(int i=0; i < length; i++) {
|
||||
array[i] = pac.unpackBoolean();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
MessagePackObject[] src = from.asArray();
|
||||
boolean[] array;
|
||||
if(to != null && to instanceof boolean[] && ((boolean[])to).length == src.length) {
|
||||
array = (boolean[])to;
|
||||
} else {
|
||||
array = new boolean[src.length];
|
||||
}
|
||||
for(int i=0; i < src.length; i++) {
|
||||
MessagePackObject s = src[i];
|
||||
array[i] = s.asBoolean();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
static public BooleanArrayTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final BooleanArrayTemplate instance = new BooleanArrayTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(boolean[].class, instance);
|
||||
}
|
||||
}
|
||||
|
@ -43,6 +43,7 @@ public class ByteArrayTemplate implements Template {
|
||||
|
||||
static {
|
||||
CustomMessage.register(byte[].class, instance);
|
||||
TemplateRegistry.register(byte[].class, instance);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,76 @@
|
||||
//
|
||||
// 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.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class DoubleArrayTemplate implements Template {
|
||||
private DoubleArrayTemplate() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if(!(target instanceof double[])) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
double[] array = (double[])target;
|
||||
pk.packArray(array.length);
|
||||
for(double a : array) {
|
||||
pk.pack(a);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackArray();
|
||||
double[] array;
|
||||
if(to != null && to instanceof double[] && ((double[])to).length == length) {
|
||||
array = (double[])to;
|
||||
} else {
|
||||
array = new double[length];
|
||||
}
|
||||
for(int i=0; i < length; i++) {
|
||||
array[i] = pac.unpackDouble();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
MessagePackObject[] src = from.asArray();
|
||||
double[] array;
|
||||
if(to != null && to instanceof double[] && ((double[])to).length == src.length) {
|
||||
array = (double[])to;
|
||||
} else {
|
||||
array = new double[src.length];
|
||||
}
|
||||
for(int i=0; i < src.length; i++) {
|
||||
MessagePackObject s = src[i];
|
||||
array[i] = s.asDouble();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
static public DoubleArrayTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final DoubleArrayTemplate instance = new DoubleArrayTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(double[].class, instance);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,76 @@
|
||||
//
|
||||
// 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.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class FloatArrayTemplate implements Template {
|
||||
private FloatArrayTemplate() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if(!(target instanceof float[])) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
float[] array = (float[])target;
|
||||
pk.packArray(array.length);
|
||||
for(float a : array) {
|
||||
pk.pack(a);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackArray();
|
||||
float[] array;
|
||||
if(to != null && to instanceof float[] && ((float[])to).length == length) {
|
||||
array = (float[])to;
|
||||
} else {
|
||||
array = new float[length];
|
||||
}
|
||||
for(int i=0; i < length; i++) {
|
||||
array[i] = pac.unpackFloat();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
MessagePackObject[] src = from.asArray();
|
||||
float[] array;
|
||||
if(to != null && to instanceof float[] && ((float[])to).length == src.length) {
|
||||
array = (float[])to;
|
||||
} else {
|
||||
array = new float[src.length];
|
||||
}
|
||||
for(int i=0; i < src.length; i++) {
|
||||
MessagePackObject s = src[i];
|
||||
array[i] = s.asFloat();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
static public FloatArrayTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final FloatArrayTemplate instance = new FloatArrayTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(float[].class, instance);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,76 @@
|
||||
//
|
||||
// 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.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class IntArrayTemplate implements Template {
|
||||
private IntArrayTemplate() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if(!(target instanceof int[])) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
int[] array = (int[])target;
|
||||
pk.packArray(array.length);
|
||||
for(int a : array) {
|
||||
pk.pack(a);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackArray();
|
||||
int[] array;
|
||||
if(to != null && to instanceof int[] && ((int[])to).length == length) {
|
||||
array = (int[])to;
|
||||
} else {
|
||||
array = new int[length];
|
||||
}
|
||||
for(int i=0; i < length; i++) {
|
||||
array[i] = pac.unpackInt();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
MessagePackObject[] src = from.asArray();
|
||||
int[] array;
|
||||
if(to != null && to instanceof int[] && ((int[])to).length == src.length) {
|
||||
array = (int[])to;
|
||||
} else {
|
||||
array = new int[src.length];
|
||||
}
|
||||
for(int i=0; i < src.length; i++) {
|
||||
MessagePackObject s = src[i];
|
||||
array[i] = s.asInt();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
static public IntArrayTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final IntArrayTemplate instance = new IntArrayTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(int[].class, instance);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,76 @@
|
||||
//
|
||||
// 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.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class LongArrayTemplate implements Template {
|
||||
private LongArrayTemplate() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if(!(target instanceof long[])) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
long[] array = (long[])target;
|
||||
pk.packArray(array.length);
|
||||
for(long a : array) {
|
||||
pk.pack(a);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackArray();
|
||||
long[] array;
|
||||
if(to != null && to instanceof long[] && ((long[])to).length == length) {
|
||||
array = (long[])to;
|
||||
} else {
|
||||
array = new long[length];
|
||||
}
|
||||
for(int i=0; i < length; i++) {
|
||||
array[i] = pac.unpackLong();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
MessagePackObject[] src = from.asArray();
|
||||
long[] array;
|
||||
if(to != null && to instanceof long[] && ((long[])to).length == src.length) {
|
||||
array = (long[])to;
|
||||
} else {
|
||||
array = new long[src.length];
|
||||
}
|
||||
for(int i=0; i < src.length; i++) {
|
||||
MessagePackObject s = src[i];
|
||||
array[i] = s.asLong();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
static public LongArrayTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final LongArrayTemplate instance = new LongArrayTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(long[].class, instance);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,79 @@
|
||||
//
|
||||
// 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.template;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class ObjectArrayTemplate implements Template {
|
||||
static void load() { }
|
||||
|
||||
private Template componentTemplate;
|
||||
|
||||
public ObjectArrayTemplate(Template componentTemplate) {
|
||||
this.componentTemplate = componentTemplate;
|
||||
}
|
||||
|
||||
public Template getcomponentTemplate() {
|
||||
return componentTemplate;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if(!(target instanceof Object[])) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
Object[] array = (Object[])target;
|
||||
pk.packArray(array.length);
|
||||
for(Object a : array) {
|
||||
componentTemplate.pack(pk, a);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackArray();
|
||||
Object[] array;
|
||||
if(to != null && to instanceof Object[] && ((Object[])to).length == length) {
|
||||
array = (Object[])to;
|
||||
} else {
|
||||
array = new Object[length];
|
||||
}
|
||||
for(int i=0; i < length; i++) {
|
||||
array[i] = componentTemplate.unpack(pac, null);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
MessagePackObject[] src = from.asArray();
|
||||
Object[] array;
|
||||
if(to != null && to instanceof Object[] && ((Object[])to).length == src.length) {
|
||||
array = (Object[])to;
|
||||
} else {
|
||||
array = new Object[src.length];
|
||||
}
|
||||
for(int i=0; i < src.length; i++) {
|
||||
MessagePackObject s = src[i];
|
||||
array[i] = componentTemplate.convert(s, array[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,76 @@
|
||||
//
|
||||
// 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.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class ShortArrayTemplate implements Template {
|
||||
private ShortArrayTemplate() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if(!(target instanceof short[])) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
short[] array = (short[])target;
|
||||
pk.packArray(array.length);
|
||||
for(short a : array) {
|
||||
pk.pack(a);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackArray();
|
||||
short[] array;
|
||||
if(to != null && to instanceof short[] && ((short[])to).length == length) {
|
||||
array = (short[])to;
|
||||
} else {
|
||||
array = new short[length];
|
||||
}
|
||||
for(int i=0; i < length; i++) {
|
||||
array[i] = pac.unpackShort();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
MessagePackObject[] src = from.asArray();
|
||||
short[] array;
|
||||
if(to != null && to instanceof short[] && ((short[])to).length == src.length) {
|
||||
array = (short[])to;
|
||||
} else {
|
||||
array = new short[src.length];
|
||||
}
|
||||
for(int i=0; i < src.length; i++) {
|
||||
MessagePackObject s = src[i];
|
||||
array[i] = s.asShort();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
static public ShortArrayTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final ShortArrayTemplate instance = new ShortArrayTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(short[].class, instance);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user