diff --git a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java index 7aab4051e..dfbe2ed44 100755 --- a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java @@ -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, 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, 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")) diff --git a/src/main/java/net/openhft/xstreem/convertors/ByteBufferConverter.java b/src/main/java/net/openhft/xstreem/convertors/ByteBufferConverter.java new file mode 100644 index 000000000..8b76a66d0 --- /dev/null +++ b/src/main/java/net/openhft/xstreem/convertors/ByteBufferConverter.java @@ -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); + } +} diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleMapConvector.java b/src/main/java/net/openhft/xstreem/convertors/ChronicleMapConvecter.java similarity index 96% rename from src/main/java/net/openhft/chronicle/map/ChronicleMapConvector.java rename to src/main/java/net/openhft/xstreem/convertors/ChronicleMapConvecter.java index 411b43ed2..75f2ec9fc 100644 --- a/src/main/java/net/openhft/chronicle/map/ChronicleMapConvector.java +++ b/src/main/java/net/openhft/xstreem/convertors/ChronicleMapConvecter.java @@ -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 implements Converter { +public class ChronicleMapConvecter 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 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 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(); diff --git a/src/test/java/eg/TestBeanClass.java b/src/test/java/eg/TestBeanClass.java new file mode 100644 index 000000000..654119809 --- /dev/null +++ b/src/test/java/eg/TestBeanClass.java @@ -0,0 +1,27 @@ +package eg; + +/** + * Created by Rob on 20/12/14. + */ +public class TestBeanClass { + + + String name; + int value; + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/test/java/eg/TestMain.java b/src/test/java/eg/TestMain.java new file mode 100644 index 000000000..00beb1945 --- /dev/null +++ b/src/test/java/eg/TestMain.java @@ -0,0 +1,20 @@ +package eg; + +import com.sun.tools.internal.xjc.generator.bean.BeanGenerator; + +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; + +/** + * Created by Rob on 20/12/14. + */ +public class TestMain { + + public static void main(String... args) throws IntrospectionException { + BeanInfo info= Introspector.getBeanInfo(TestBeanClass .class); + System.out.println(info); + + info.getPropertyDescriptors(); + } +} diff --git a/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java b/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java index e9e63f241..d23eff8a0 100644 --- a/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java +++ b/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java @@ -121,9 +121,8 @@ public class CHMUseCasesTest { return; - - // waitTillEqual(5000); - // assertEquals(map1, map2); + // waitTillEqual(5000); + // assertEquals(map1, map2); } if (typeOfMap == TypeOfMap.SIMPLE) @@ -139,12 +138,6 @@ public class CHMUseCasesTest { return; } - - if (ByteBuffer.class.isAssignableFrom(map1.keyClass()) || - ByteBuffer.class.isAssignableFrom(map1.valueClass())) { - return; - } - File file = new File(TMP + "/chronicle-map-" + System.nanoTime() + ".json"); file.deleteOnExit();