Codec's encoder/decoder instances caching

pull/255/head
Nikita 10 years ago
parent 40af48f4ae
commit fbef4ee89a

@ -25,14 +25,16 @@ public class LongCodec extends StringCodec {
public static final LongCodec INSTANCE = new LongCodec();
public final Decoder<Object> decoder = new Decoder<Object>() {
@Override
public Object decode(ByteBuf buf, State state) {
return Long.valueOf(buf.toString(CharsetUtil.UTF_8));
}
};
@Override
public Decoder<Object> getValueDecoder() {
return new Decoder<Object>() {
@Override
public Object decode(ByteBuf buf, State state) {
return Long.valueOf(buf.toString(CharsetUtil.UTF_8));
}
};
return decoder;
}
}

@ -28,24 +28,28 @@ public class StringCodec implements Codec {
public static final StringCodec INSTANCE = new StringCodec();
private final Encoder encoder = new Encoder() {
@Override
public byte[] encode(Object in) throws IOException {
return in.toString().getBytes("UTF-8");
}
};
private final Decoder<Object> decoder = new Decoder<Object>() {
@Override
public Object decode(ByteBuf buf, State state) {
return buf.toString(CharsetUtil.UTF_8);
}
};
@Override
public Decoder<Object> getValueDecoder() {
return new Decoder<Object>() {
@Override
public Object decode(ByteBuf buf, State state) {
return buf.toString(CharsetUtil.UTF_8);
}
};
return decoder;
}
@Override
public Encoder getValueEncoder() {
return new Encoder() {
@Override
public byte[] encode(Object in) throws IOException {
return in.toString().getBytes("UTF-8");
}
};
return encoder;
}
@Override

@ -44,7 +44,21 @@ import io.netty.buffer.ByteBufInputStream;
*/
public class JsonJacksonCodec implements Codec {
private ObjectMapper mapObjectMapper = new ObjectMapper();
private final ObjectMapper mapObjectMapper = new ObjectMapper();
private final Encoder encoder = new Encoder() {
@Override
public byte[] encode(Object in) throws IOException {
return mapObjectMapper.writeValueAsBytes(in);
}
};
private final Decoder<Object> decoder = new Decoder<Object>() {
@Override
public Object decode(ByteBuf buf, State state) throws IOException {
return mapObjectMapper.readValue(new ByteBufInputStream(buf), Object.class);
}
};
public JsonJacksonCodec() {
init(mapObjectMapper);
@ -94,24 +108,12 @@ public class JsonJacksonCodec implements Codec {
@Override
public Decoder<Object> getMapValueDecoder() {
return new Decoder<Object>() {
@Override
public Object decode(ByteBuf buf, State state) throws IOException {
return mapObjectMapper.readValue(new ByteBufInputStream(buf), Object.class);
}
};
return decoder;
}
@Override
public Encoder getMapValueEncoder() {
return new Encoder() {
@Override
public byte[] encode(Object in) throws IOException {
return mapObjectMapper.writeValueAsBytes(in);
}
};
return encoder;
}
@Override

@ -93,6 +93,51 @@ public class KryoCodec implements Codec {
private final KryoPool kryoPool;
private final Decoder<Object> decoder = new Decoder<Object>() {
@Override
public Object decode(ByteBuf buf, State state) throws IOException {
Kryo kryo = null;
try {
kryo = kryoPool.get();
return kryo.readClassAndObject(new Input(new ByteBufInputStream(buf)));
} catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new RedissonKryoCodecException(e);
} finally {
if (kryo != null) {
kryoPool.yield(kryo);
}
}
}
};
private final Encoder encoder = new Encoder() {
@Override
public byte[] encode(Object in) throws IOException {
Kryo kryo = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Output output = new Output(baos);
kryo = kryoPool.get();
kryo.writeClassAndObject(output, in);
output.close();
return baos.toByteArray();
} catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new RedissonKryoCodecException(e);
} finally {
if (kryo != null) {
kryoPool.yield(kryo);
}
}
}
};
public KryoCodec() {
this(new KryoPoolImpl(Collections.<Class<?>>emptyList()));
}
@ -128,54 +173,12 @@ public class KryoCodec implements Codec {
@Override
public Decoder<Object> getValueDecoder() {
return new Decoder<Object>() {
@Override
public Object decode(ByteBuf buf, State state) throws IOException {
Kryo kryo = null;
try {
kryo = kryoPool.get();
return kryo.readClassAndObject(new Input(new ByteBufInputStream(buf)));
} catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new RedissonKryoCodecException(e);
} finally {
if (kryo != null) {
kryoPool.yield(kryo);
}
}
}
};
return decoder;
}
@Override
public Encoder getValueEncoder() {
return new Encoder() {
@Override
public byte[] encode(Object in) throws IOException {
Kryo kryo = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Output output = new Output(baos);
kryo = kryoPool.get();
kryo.writeClassAndObject(output, in);
output.close();
return baos.toByteArray();
} catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new RedissonKryoCodecException(e);
} finally {
if (kryo != null) {
kryoPool.yield(kryo);
}
}
}
};
return encoder;
}
}

@ -35,6 +35,32 @@ import io.netty.buffer.ByteBufInputStream;
*/
public class SerializationCodec implements Codec {
private final Decoder<Object> decoder = new Decoder<Object>() {
@Override
public Object decode(ByteBuf buf, State state) throws IOException {
try {
ObjectInputStream inputStream = new ObjectInputStream(new ByteBufInputStream(buf));
return inputStream.readObject();
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new IOException(e);
}
}
};
private final Encoder encoder = new Encoder() {
@Override
public byte[] encode(Object in) throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
ObjectOutputStream outputStream = new ObjectOutputStream(result);
outputStream.writeObject(in);
outputStream.close();
return result.toByteArray();
}
};
@Override
public Decoder<Object> getMapValueDecoder() {
return getValueDecoder();
@ -57,34 +83,12 @@ public class SerializationCodec implements Codec {
@Override
public Decoder<Object> getValueDecoder() {
return new Decoder<Object>() {
@Override
public Object decode(ByteBuf buf, State state) throws IOException {
try {
ObjectInputStream inputStream = new ObjectInputStream(new ByteBufInputStream(buf));
return inputStream.readObject();
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new IOException(e);
}
}
};
return decoder;
}
@Override
public Encoder getValueEncoder() {
return new Encoder() {
@Override
public byte[] encode(Object in) throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
ObjectOutputStream outputStream = new ObjectOutputStream(result);
outputStream.writeObject(in);
outputStream.close();
return result.toByteArray();
}
};
return encoder;
}
}

Loading…
Cancel
Save