org.redisson.codec.FuryCodec Maven / Gradle / Ivy
Show all versions of redisson-all Show documentation
/**
* Copyright (c) 2013-2024 Nikita Koksharov
*
* 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.redisson.codec;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import org.apache.fury.Fury;
import org.apache.fury.ThreadSafeFury;
import org.apache.fury.config.FuryBuilder;
import org.apache.fury.config.Language;
import org.apache.fury.io.FuryStreamReader;
import org.apache.fury.memory.MemoryBuffer;
import org.apache.fury.memory.MemoryUtils;
import org.redisson.client.codec.BaseCodec;
import org.redisson.client.handler.State;
import org.redisson.client.protocol.Decoder;
import org.redisson.client.protocol.Encoder;
import java.io.IOException;
import java.util.Collections;
import java.util.Set;
/**
* Apache Fury codec
*
* Fully thread-safe.
*
* @author Nikita Koksharov
*
*/
public class FuryCodec extends BaseCodec {
private final ThreadSafeFury fury;
private final Set allowedClasses;
private final Language language;
public FuryCodec() {
this(null, Collections.emptySet(), Language.JAVA);
}
public FuryCodec(Set allowedClasses) {
this(null, allowedClasses, Language.JAVA);
}
public FuryCodec(Language language) {
this(null, Collections.emptySet(), language);
}
public FuryCodec(Set allowedClasses, Language language) {
this(null, allowedClasses, language);
}
public FuryCodec(ClassLoader classLoader, FuryCodec codec) {
this(classLoader, codec.allowedClasses, codec.language);
}
public FuryCodec(ClassLoader classLoader) {
this(classLoader, Collections.emptySet(), Language.JAVA);
}
public FuryCodec(ClassLoader classLoader, Set allowedClasses, Language language) {
this.allowedClasses = allowedClasses;
this.language = language;
FuryBuilder builder = Fury.builder();
if (classLoader != null) {
builder.withClassLoader(classLoader);
}
builder.withLanguage(language);
builder.requireClassRegistration(!allowedClasses.isEmpty());
fury = builder.buildThreadSafeFuryPool(10, 512);
for (String allowedClass : allowedClasses) {
try {
fury.register(Class.forName(allowedClass));
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
}
}
private final Decoder