From c84b0a1b109684bdac07151616aa43cd82156630 Mon Sep 17 00:00:00 2001 From: zrlw Date: Wed, 24 Nov 2021 18:19:11 +0800 Subject: [PATCH] support BitSet --- .../com/caucho/hessian/io/BitSetHandle.java | 70 +++++++++++++++++ .../caucho/hessian/io/BitSetSerializer.java | 75 +++++++++++++++++++ .../caucho/hessian/io/SerializerFactory.java | 2 + .../hessian/io/BitSetSerializerTest.java | 48 ++++++++++++ 4 files changed, 195 insertions(+) create mode 100644 src/main/java/com/alibaba/com/caucho/hessian/io/BitSetHandle.java create mode 100644 src/main/java/com/alibaba/com/caucho/hessian/io/BitSetSerializer.java create mode 100644 src/test/java/com/alibaba/com/caucho/hessian/io/BitSetSerializerTest.java diff --git a/src/main/java/com/alibaba/com/caucho/hessian/io/BitSetHandle.java b/src/main/java/com/alibaba/com/caucho/hessian/io/BitSetHandle.java new file mode 100644 index 00000000..4e257b7e --- /dev/null +++ b/src/main/java/com/alibaba/com/caucho/hessian/io/BitSetHandle.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2001-2008 Caucho Technology, Inc. All rights reserved. + * + * The Apache Software License, Version 1.1 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Caucho Technology (http://www.caucho.com/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "Hessian", "Resin", and "Caucho" must not be used to + * endorse or promote products derived from this software without prior + * written permission. For written permission, please contact + * info@caucho.com. + * + * 5. Products derived from this software may not be called "Resin" + * nor may "Resin" appear in their names without prior written + * permission of Caucho Technology. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * @author Scott Ferguson + */ + +package com.alibaba.com.caucho.hessian.io; + +import java.util.BitSet; + +/** + * Handle for a BitSet object. + */ +public class BitSetHandle implements java.io.Serializable, HessianHandle { + private long[] words; + + public BitSetHandle(long[] words) { + this.words = words; + } + + private Object readResolve() { + if (words == null) { + return null; + } + + return BitSet.valueOf(words); + } +} diff --git a/src/main/java/com/alibaba/com/caucho/hessian/io/BitSetSerializer.java b/src/main/java/com/alibaba/com/caucho/hessian/io/BitSetSerializer.java new file mode 100644 index 00000000..9b7c56e6 --- /dev/null +++ b/src/main/java/com/alibaba/com/caucho/hessian/io/BitSetSerializer.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved. + * + * The Apache Software License, Version 1.1 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Caucho Technology (http://www.caucho.com/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "Hessian", "Resin", and "Caucho" must not be used to + * endorse or promote products derived from this software without prior + * written permission. For written permission, please contact + * info@caucho.com. + * + * 5. Products derived from this software may not be called "Resin" + * nor may "Resin" appear in their names without prior written + * permission of Caucho Technology. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * @author Scott Ferguson + */ + +package com.alibaba.com.caucho.hessian.io; + +import java.io.IOException; +import java.util.BitSet; + +/** + * Serializing a BitSet. + */ +public class BitSetSerializer extends AbstractSerializer { + private static BitSetSerializer SERIALIZER = new BitSetSerializer(); + + public static BitSetSerializer create() { + return SERIALIZER; + } + + @Override + public void writeObject(Object obj, AbstractHessianOutput out) + throws IOException { + if (obj == null) + out.writeNull(); + else { + BitSet bitSet = (BitSet) obj; + + out.writeObject(new BitSetHandle(bitSet.toLongArray())); + } + } +} diff --git a/src/main/java/com/alibaba/com/caucho/hessian/io/SerializerFactory.java b/src/main/java/com/alibaba/com/caucho/hessian/io/SerializerFactory.java index 8f38f5fa..00fe94d0 100644 --- a/src/main/java/com/alibaba/com/caucho/hessian/io/SerializerFactory.java +++ b/src/main/java/com/alibaba/com/caucho/hessian/io/SerializerFactory.java @@ -385,6 +385,8 @@ else if (Map.class.isAssignableFrom(cl)) { serializer = LocaleSerializer.create(); } else if (Enum.class.isAssignableFrom(cl)) { serializer = new EnumSerializer(cl); + } else if (BitSet.class.isAssignableFrom(cl)) { + serializer = BitSetSerializer.create(); } if (serializer == null) { diff --git a/src/test/java/com/alibaba/com/caucho/hessian/io/BitSetSerializerTest.java b/src/test/java/com/alibaba/com/caucho/hessian/io/BitSetSerializerTest.java new file mode 100644 index 00000000..94a5fd91 --- /dev/null +++ b/src/test/java/com/alibaba/com/caucho/hessian/io/BitSetSerializerTest.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alibaba.com.caucho.hessian.io; + +import java.io.IOException; +import java.util.BitSet; +import java.util.Locale; + +import org.junit.Test; + +import com.alibaba.com.caucho.hessian.io.base.SerializeTestBase; + +import junit.framework.TestCase; + +public class BitSetSerializerTest extends SerializeTestBase { + + /** {@linkplain BitSetSerializer#writeObject(Object, AbstractHessianOutput)} */ + @Test + public void bitSet() throws IOException { + long[] words = new long[3]; + words[0] = 0x0102030405060708L; + words[1] = 0x1112131415161718L; + words[2] = 0x2122232425262728L; + assertBitSet(null); + assertBitSet(new BitSet()); + assertBitSet(new BitSet(1)); + assertBitSet(BitSet.valueOf(words)); + } + + private void assertBitSet(BitSet bitSet) throws IOException { + TestCase.assertEquals(bitSet, baseHessian2Serialize(bitSet)); + TestCase.assertEquals(bitSet, baseHessianSerialize(bitSet)); + } +} \ No newline at end of file