Merge remote-tracking branch 'origin/master'

This commit is contained in:
Rob Austin
2014-12-23 09:00:37 +00:00
6 changed files with 179 additions and 21 deletions
@@ -36,6 +36,8 @@ import net.openhft.lang.model.DataValueClasses;
import net.openhft.lang.threadlocal.Provider;
import net.openhft.lang.threadlocal.StatefulCopyable;
import net.openhft.lang.threadlocal.ThreadLocalCopies;
import net.openhft.xstreem.convertors.ByteBufferConverter;
import net.openhft.xstreem.convertors.ChronicleMapConvecter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
@@ -659,7 +661,9 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
final XStream xstream = new XStream(new JettisonMappedXmlDriver());
xstream.setMode(XStream.NO_REFERENCES);
xstream.alias("cmap", VanillaChronicleMap.EntrySet.class);
xstream.registerConverter(new ChronicleMapConvector(this));
xstream.registerConverter(new ChronicleMapConvecter(this));
xstream.registerConverter(new ByteBufferConverter());
OutputStream outputStream = new FileOutputStream(toFile);
if (toFile.getName().toLowerCase().endsWith(".gz"))
@@ -674,7 +678,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
final XStream xstream = new XStream(new JettisonMappedXmlDriver());
xstream.setMode(XStream.NO_REFERENCES);
xstream.alias("cmap", VanillaChronicleMap.EntrySet.class);
xstream.registerConverter(new ChronicleMapConvector(this));
xstream.registerConverter(new ChronicleMapConvecter(this));
xstream.registerConverter(new ByteBufferConverter());
InputStream inputStream = new FileInputStream(fromFile);
if (fromFile.getName().toLowerCase().endsWith(".gz"))
@@ -0,0 +1,116 @@
package net.openhft.xstreem.convertors;
import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.*;
/**
* Created by Rob on 22/12/14.
*/
public class ByteBufferConverter implements Converter {
Charset charset = Charset.forName("ISO-8859-1");
CharsetDecoder decoder = charset.newDecoder();
@Override
public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext marshallingContext) {
ByteBuffer buffer = (ByteBuffer) o;
writer.startNode("position");
marshallingContext.convertAnother(buffer.position());
writer.endNode();
writer.startNode("capacity");
marshallingContext.convertAnother(buffer.capacity());
writer.endNode();
writer.startNode("limit");
marshallingContext.convertAnother(buffer.limit());
writer.endNode();
writer.startNode("isDirect");
marshallingContext.convertAnother(buffer.isDirect());
writer.endNode();
buffer.limit();
buffer.capacity();
int position = buffer.position();
int limit = buffer.limit();
buffer.clear();
writer.startNode("data");
try {
CharBuffer charBuffer = decoder.decode(buffer);
writer.setValue(charBuffer.toString());
} catch (CharacterCodingException e) {
throw new ConversionException("", e);
}
writer.endNode();
buffer.limit(limit);
buffer.position(position);
}
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext unmarshallingContext) {
reader.moveDown();
int position = (Integer) unmarshallingContext.convertAnother(null, int.class);
reader.moveUp();
reader.moveDown();
int capacity = (Integer) unmarshallingContext.convertAnother(null, int.class);
reader.moveUp();
reader.moveDown();
int limit = (Integer) unmarshallingContext.convertAnother(null, int.class);
reader.moveUp();
reader.moveDown();
boolean isDirect = (Boolean) unmarshallingContext.convertAnother(null, boolean.class);
reader.moveUp();
ByteBuffer buffer = isDirect ? ByteBuffer.allocateDirect(capacity) : ByteBuffer.allocate(capacity);
buffer.clear();
reader.moveDown();
String o = (String) unmarshallingContext.convertAnother(null, String.class);
CharBuffer uCharBuffer = CharBuffer.wrap(o);
CharsetEncoder encoder = charset.newEncoder();
CoderResult encode = encoder.encode(uCharBuffer, buffer, true);
if (encode.isError())
throw new ConversionException("");
buffer.limit(limit);
buffer.position(position);
reader.moveUp();
buffer.limit(limit);
buffer.position(position);
return buffer;
}
@Override
public boolean canConvert(Class aClass) {
return ByteBuffer.class.isAssignableFrom(aClass);
}
}
@@ -1,4 +1,4 @@
package net.openhft.chronicle.map;
package net.openhft.xstreem.convertors;
import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.Converter;
@@ -26,17 +26,17 @@ import static java.beans.Introspector.getBeanInfo;
/**
* @author Rob Austin.
*/
public class ChronicleMapConvector<K, V> implements Converter {
public class ChronicleMapConvecter<K, V> implements Converter {
private static final Logger LOG = LoggerFactory.getLogger(VanillaChronicleMap.class);
private static final Logger LOG = LoggerFactory.getLogger(ChronicleMapConvecter.class);
private final Class entrySetClass;
private final Class writeThroughEntryClass;
private final Map<K, V> map;
public ChronicleMapConvector(@NotNull Map map) {
public ChronicleMapConvecter(@NotNull Map map) {
final String vanillaChronicleMap = VanillaChronicleMap.class.getCanonicalName();
final String vanillaChronicleMap = "net.openhft.chronicle.map.VanillaChronicleMap";
try {
this.entrySetClass = Class.forName(vanillaChronicleMap + "$EntrySet");
@@ -72,17 +72,14 @@ public class ChronicleMapConvector<K, V> implements Converter {
if (writeThroughEntryClass.isAssignableFrom(o.getClass())) {
final AbstractMap.SimpleEntry e = (AbstractMap.SimpleEntry) o;
Object key = e.getKey();
final Object key = e.getKey();
writer.startNode(key.getClass().getCanonicalName());
marshallingContext.convertAnother(key);
writer.endNode();
Object value = e.getValue();
writer.startNode(value.getClass().getCanonicalName());
marshallingContext.convertAnother(value);
writer.endNode();