diff --git a/src/main/java/net/openhft/chronicle/map/SerializationBuilder.java b/src/main/java/net/openhft/chronicle/map/SerializationBuilder.java index fa7e05961..22bca8eaa 100644 --- a/src/main/java/net/openhft/chronicle/map/SerializationBuilder.java +++ b/src/main/java/net/openhft/chronicle/map/SerializationBuilder.java @@ -99,6 +99,9 @@ final class SerializationBuilder implements Cloneable { } else if (eClass == byte[].class) { reader((BytesReader) ByteArrayMarshaller.INSTANCE, factory); interop((BytesInterop) ByteArrayMarshaller.INSTANCE); + } else if (eClass == char[].class) { + reader((BytesReader) CharArrayMarshaller.INSTANCE, factory); + interop((BytesInterop) CharArrayMarshaller.INSTANCE); } else if (concreteClass(eClass)) { BytesMarshaller marshaller = chooseMarshaller(eClass, eClass); if (marshaller != null) diff --git a/src/main/java/net/openhft/chronicle/serialization/impl/CharArrayMarshaller.java b/src/main/java/net/openhft/chronicle/serialization/impl/CharArrayMarshaller.java new file mode 100644 index 000000000..d995b106e --- /dev/null +++ b/src/main/java/net/openhft/chronicle/serialization/impl/CharArrayMarshaller.java @@ -0,0 +1,66 @@ +/* + * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com + * + * 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 net.openhft.chronicle.serialization.impl; + +import net.openhft.chronicle.serialization.BytesInterop; +import net.openhft.chronicle.serialization.BytesReader; +import net.openhft.chronicle.serialization.Hasher; +import net.openhft.lang.io.Bytes; + +public enum CharArrayMarshaller implements BytesInterop, BytesReader { + INSTANCE; + + @Override + public boolean startsWith(Bytes bytes, char[] chars) { + long pos = bytes.position(); + for (int i = 0; i < chars.length; i++) { + if (bytes.readChar(pos + (i * 2L)) != chars[i]) + return false; + } + return true; + } + + @Override + public long hash(char[] chars) { + return Hasher.hash(chars, chars.length); + } + + @Override + public char[] read(Bytes bytes, long size) { + char[] chars = new char[(int) size]; + bytes.readFully(chars); + return chars; + } + + @Override + public char[] read(Bytes bytes, long size, char[] chars) { + if (chars == null || chars.length != size) + chars = new char[(int) size]; + bytes.readFully(chars); + return chars; + } + + @Override + public long size(char[] chars) { + return chars.length * 2L; + } + + @Override + public void write(Bytes bytes, char[] chars) { + bytes.write(chars); + } +}