Skip to content

Commit

Permalink
support BitSet
Browse files Browse the repository at this point in the history
  • Loading branch information
zrlw committed Nov 24, 2021
1 parent 42cf3b7 commit c84b0a1
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/main/java/com/alibaba/com/caucho/hessian/io/BitSetHandle.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit c84b0a1

Please sign in to comment.