mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
Add CharSequenceBytesReader and prohibit Readers and Writers of CharSequences and Strings to read and write nulls
This commit is contained in:
+69
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (C) 2015 higherfrequencytrading.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.openhft.chronicle.hash.serialization.impl;
|
||||
|
||||
import net.openhft.chronicle.bytes.Bytes;
|
||||
import net.openhft.chronicle.hash.serialization.BytesReader;
|
||||
import net.openhft.chronicle.hash.serialization.ListMarshaller;
|
||||
import net.openhft.chronicle.hash.serialization.SetMarshaller;
|
||||
import net.openhft.chronicle.hash.serialization.StatefulCopyable;
|
||||
import net.openhft.chronicle.wire.WireIn;
|
||||
import net.openhft.chronicle.wire.WireOut;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
|
||||
/**
|
||||
* {@link BytesReader} implementation for {@code CharSequence}, for the primary ChronicleMap's key
|
||||
* of value type {@link CharSequenceSizedReader} + {@link CharSequenceSizedWriter} are more
|
||||
* efficient (because don't store the size twice), so this reader is useful in conjunction with
|
||||
* {@link ListMarshaller} or {@link SetMarshaller}.
|
||||
*
|
||||
* @see CharSequenceBytesWriter
|
||||
*/
|
||||
public enum CharSequenceBytesReader implements BytesReader<CharSequence>,
|
||||
StatefulCopyable<CharSequenceBytesReader>, EnumMarshallable<CharSequenceBytesReader> {
|
||||
INSTANCE;
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CharSequence read(Bytes in, @Nullable CharSequence using) {
|
||||
StringBuilder usingSB;
|
||||
if (using instanceof StringBuilder) {
|
||||
usingSB = (StringBuilder) using;
|
||||
} else {
|
||||
usingSB = new StringBuilder();
|
||||
}
|
||||
if (in.readUtf8(usingSB)) {
|
||||
return usingSB;
|
||||
} else {
|
||||
throw new NullPointerException("BytesReader couldn't read null");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequenceBytesReader copy() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequenceBytesReader readResolve() {
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -25,10 +25,10 @@ import org.jetbrains.annotations.NotNull;
|
||||
/**
|
||||
* {@link BytesWriter} implementation for {@link CharSequence}, for the primary ChronicleMap's
|
||||
* key or value type {@link CharSequenceSizedWriter} + {@link StringSizedReader} are more
|
||||
* effective (because don't store the size twice), so this writer is useful in conjunction with
|
||||
* efficient (because don't store the size twice), so this writer is useful in conjunction with
|
||||
* {@link ListMarshaller} or {@link SetMarshaller}.
|
||||
*
|
||||
* @see CharSequenceBytesWriter
|
||||
* @see StringBytesReader
|
||||
*/
|
||||
public enum CharSequenceBytesWriter
|
||||
implements BytesWriter<CharSequence>, EnumMarshallable<CharSequenceBytesWriter> {
|
||||
@@ -36,6 +36,8 @@ public enum CharSequenceBytesWriter
|
||||
|
||||
@Override
|
||||
public void write(Bytes out, @NotNull CharSequence toWrite) {
|
||||
if (toWrite == null)
|
||||
throw new NullPointerException("BytesWriter couldn't write null");
|
||||
out.writeUtf8(toWrite);
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ import java.io.ObjectInputStream;
|
||||
|
||||
/**
|
||||
* {@link BytesReader} implementation for String, for the primary ChronicleMap's key of value type
|
||||
* {@link StringSizedReader} + {@link CharSequenceSizedWriter} are more effective (because don't
|
||||
* {@link StringSizedReader} + {@link CharSequenceSizedWriter} are more efficient (because don't
|
||||
* store the size twice), so this reader is useful in conjunction with {@link ListMarshaller} or
|
||||
* {@link SetMarshaller}.
|
||||
*
|
||||
@@ -59,9 +59,11 @@ public class StringBytesReader
|
||||
@NotNull
|
||||
@Override
|
||||
public String read(Bytes in, @Nullable String using) {
|
||||
sb.setLength(0);
|
||||
in.readUtf8(sb);
|
||||
return sb.toString();
|
||||
if (in.readUtf8(sb)) {
|
||||
return sb.toString();
|
||||
} else {
|
||||
throw new NullPointerException("BytesReader couldn't read null");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user