Class JavaBinCodec
- java.lang.Object
-
- org.apache.solr.common.util.JavaBinCodec
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
,PushWriter
public class JavaBinCodec extends Object implements PushWriter
Defines a space-efficient serialization/deserialization format for transferring data.JavaBinCodec has built in support many commonly used types. This includes primitive types (boolean, byte, short, double, int, long, float), common Java containers/utilities (Date, Map, Collection, Iterator, String, Object[], byte[]), and frequently used Solr types (
NamedList
,SolrDocument
,SolrDocumentList
). Each of the above types has a pair of associated methods which read and write that type to a stream.Classes that aren't supported natively can still be serialized/deserialized by providing an
JavaBinCodec.ObjectResolver
object that knows how to work with the unsupported class. This allowsJavaBinCodec
to be used to marshall/unmarshall arbitrary content.NOTE --
JavaBinCodec
instances cannot be reused for more than one marshall or unmarshall operation.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
JavaBinCodec.BinEntryWriter
static interface
JavaBinCodec.ObjectResolver
Allows extension ofJavaBinCodec
to support serialization of arbitrary data types.static class
JavaBinCodec.StringCache
static interface
JavaBinCodec.WritableDocFields
-
Field Summary
Fields Modifier and Type Field Description static byte
ARR
static byte
BOOL_FALSE
static byte
BOOL_TRUE
static byte
BYTE
static byte
BYTEARR
protected FastOutputStream
daos
static byte
DATE
static byte
DOUBLE
static byte
END
protected static Object
END_OBJ
static byte
ENUM_FIELD_VALUE
JavaBinCodec.BinEntryWriter
ew
static byte
EXTERN_STRING
static byte
FLOAT
static byte
INT
IteratorWriter.ItemWriter
itemWriter
static byte
ITERATOR
static byte
LONG
static byte
MAP
static byte
MAP_ENTRY
static byte
MAP_ENTRY_ITER
static byte
NAMED_LST
static byte
NULL
static byte
ORDERED_MAP
protected boolean
readStringAsCharSeq
static byte
SHORT
static byte
SINT
static byte
SLONG
static byte
SOLRDOC
static byte
SOLRDOCLST
static byte
SOLRINPUTDOC
static byte
STR
static byte
TAG_AND_LEN
protected byte
tagByte
static byte
UUID
-
Constructor Summary
Constructors Constructor Description JavaBinCodec()
JavaBinCodec(OutputStream os, JavaBinCodec.ObjectResolver resolver)
Use this to use this as a PushWriter.JavaBinCodec(JavaBinCodec.ObjectResolver resolver)
JavaBinCodec(JavaBinCodec.ObjectResolver resolver, JavaBinCodec.StringCache stringCache)
-
Method Summary
-
-
-
Field Detail
-
NULL
public static final byte NULL
- See Also:
- Constant Field Values
-
BOOL_TRUE
public static final byte BOOL_TRUE
- See Also:
- Constant Field Values
-
BOOL_FALSE
public static final byte BOOL_FALSE
- See Also:
- Constant Field Values
-
BYTE
public static final byte BYTE
- See Also:
- Constant Field Values
-
SHORT
public static final byte SHORT
- See Also:
- Constant Field Values
-
DOUBLE
public static final byte DOUBLE
- See Also:
- Constant Field Values
-
INT
public static final byte INT
- See Also:
- Constant Field Values
-
LONG
public static final byte LONG
- See Also:
- Constant Field Values
-
FLOAT
public static final byte FLOAT
- See Also:
- Constant Field Values
-
DATE
public static final byte DATE
- See Also:
- Constant Field Values
-
MAP
public static final byte MAP
- See Also:
- Constant Field Values
-
SOLRDOC
public static final byte SOLRDOC
- See Also:
- Constant Field Values
-
SOLRDOCLST
public static final byte SOLRDOCLST
- See Also:
- Constant Field Values
-
BYTEARR
public static final byte BYTEARR
- See Also:
- Constant Field Values
-
ITERATOR
public static final byte ITERATOR
- See Also:
- Constant Field Values
-
END
public static final byte END
- See Also:
- Constant Field Values
-
SOLRINPUTDOC
public static final byte SOLRINPUTDOC
- See Also:
- Constant Field Values
-
MAP_ENTRY_ITER
public static final byte MAP_ENTRY_ITER
- See Also:
- Constant Field Values
-
ENUM_FIELD_VALUE
public static final byte ENUM_FIELD_VALUE
- See Also:
- Constant Field Values
-
MAP_ENTRY
public static final byte MAP_ENTRY
- See Also:
- Constant Field Values
-
UUID
public static final byte UUID
- See Also:
- Constant Field Values
-
TAG_AND_LEN
public static final byte TAG_AND_LEN
- See Also:
- Constant Field Values
-
STR
public static final byte STR
- See Also:
- Constant Field Values
-
SINT
public static final byte SINT
- See Also:
- Constant Field Values
-
SLONG
public static final byte SLONG
- See Also:
- Constant Field Values
-
ARR
public static final byte ARR
- See Also:
- Constant Field Values
-
ORDERED_MAP
public static final byte ORDERED_MAP
- See Also:
- Constant Field Values
-
NAMED_LST
public static final byte NAMED_LST
- See Also:
- Constant Field Values
-
EXTERN_STRING
public static final byte EXTERN_STRING
- See Also:
- Constant Field Values
-
daos
protected FastOutputStream daos
-
readStringAsCharSeq
protected boolean readStringAsCharSeq
-
END_OBJ
protected static final Object END_OBJ
-
tagByte
protected byte tagByte
-
ew
public final JavaBinCodec.BinEntryWriter ew
-
itemWriter
public final IteratorWriter.ItemWriter itemWriter
-
-
Constructor Detail
-
JavaBinCodec
public JavaBinCodec()
-
JavaBinCodec
public JavaBinCodec(OutputStream os, JavaBinCodec.ObjectResolver resolver) throws IOException
Use this to use this as a PushWriter. ensure that close() is called explicitly after use- Parameters:
os
- The output stream- Throws:
IOException
-
JavaBinCodec
public JavaBinCodec(JavaBinCodec.ObjectResolver resolver)
-
JavaBinCodec
public JavaBinCodec(JavaBinCodec.ObjectResolver resolver, JavaBinCodec.StringCache stringCache)
-
-
Method Detail
-
setReadStringAsCharSeq
public JavaBinCodec setReadStringAsCharSeq(boolean flag)
-
setWritableDocFields
public JavaBinCodec setWritableDocFields(JavaBinCodec.WritableDocFields writableDocFields)
-
getResolver
public JavaBinCodec.ObjectResolver getResolver()
-
marshal
public void marshal(Object nl, OutputStream os) throws IOException
- Throws:
IOException
-
initWrite
protected void initWrite(OutputStream os) throws IOException
- Throws:
IOException
-
init
public void init(FastOutputStream os)
expert: sets a new output stream
-
unmarshal
public Object unmarshal(byte[] buf) throws IOException
- Throws:
IOException
-
unmarshal
public Object unmarshal(InputStream is) throws IOException
- Throws:
IOException
-
initRead
protected FastInputStream initRead(InputStream is) throws IOException
- Throws:
IOException
-
initRead
protected FastInputStream initRead(byte[] buf) throws IOException
- Throws:
IOException
-
_init
protected FastInputStream _init(FastInputStream dis) throws IOException
- Throws:
IOException
-
readOrderedMap
public SimpleOrderedMap<Object> readOrderedMap(DataInputInputStream dis) throws IOException
- Throws:
IOException
-
readNamedList
public NamedList<Object> readNamedList(DataInputInputStream dis) throws IOException
- Throws:
IOException
-
writeNamedList
public void writeNamedList(NamedList<?> nl) throws IOException
- Throws:
IOException
-
writeVal
public void writeVal(Object val) throws IOException
- Throws:
IOException
-
readVal
public Object readVal(DataInputInputStream dis) throws IOException
- Throws:
IOException
-
readObject
protected Object readObject(DataInputInputStream dis) throws IOException
- Throws:
IOException
-
writeKnownType
public boolean writeKnownType(Object val) throws IOException
- Throws:
IOException
-
writeMap
public void writeMap(MapWriter val) throws IOException
Description copied from interface:PushWriter
Write a Map. The map is opened in the beginning of the method and closed at the end. All map entries MUST be written before this method returns- Specified by:
writeMap
in interfacePushWriter
- Throws:
IOException
-
writeTag
public void writeTag(byte tag) throws IOException
- Throws:
IOException
-
writeTag
public void writeTag(byte tag, int size) throws IOException
- Throws:
IOException
-
writeByteArray
public void writeByteArray(byte[] arr, int offset, int len) throws IOException
- Throws:
IOException
-
readByteArray
public byte[] readByteArray(DataInputInputStream dis) throws IOException
- Throws:
IOException
-
writeSolrDocument
public void writeSolrDocument(SolrDocument doc) throws IOException
- Throws:
IOException
-
toWrite
protected boolean toWrite(String key)
-
readSolrDocument
public SolrDocument readSolrDocument(DataInputInputStream dis) throws IOException
- Throws:
IOException
-
readSolrDocumentList
public SolrDocumentList readSolrDocumentList(DataInputInputStream dis) throws IOException
- Throws:
IOException
-
writeSolrDocumentList
public void writeSolrDocumentList(SolrDocumentList docs) throws IOException
- Throws:
IOException
-
readSolrInputDocument
public SolrInputDocument readSolrInputDocument(DataInputInputStream dis) throws IOException
- Throws:
IOException
-
createSolrInputDocument
protected SolrInputDocument createSolrInputDocument(int sz)
-
writeSolrInputDocument
public void writeSolrInputDocument(SolrInputDocument sdoc) throws IOException
- Throws:
IOException
-
readMapIter
public Map<Object,Object> readMapIter(DataInputInputStream dis) throws IOException
- Throws:
IOException
-
newMap
protected Map<Object,Object> newMap(int size)
create a new Map object- Parameters:
size
- expected size, -1 means unknown size
-
readMap
public Map<Object,Object> readMap(DataInputInputStream dis) throws IOException
- Throws:
IOException
-
readMap
protected Map<Object,Object> readMap(DataInputInputStream dis, int sz) throws IOException
- Throws:
IOException
-
writeIterator
public void writeIterator(IteratorWriter val) throws IOException
Description copied from interface:PushWriter
Write an array. The array is opened at the beginning of this method and closed at the end. All array entries must be written before this method returns- Specified by:
writeIterator
in interfacePushWriter
- Throws:
IOException
-
writeIterator
public void writeIterator(Iterator<?> iter) throws IOException
- Throws:
IOException
-
readIterator
public List<Object> readIterator(DataInputInputStream fis) throws IOException
- Throws:
IOException
-
writeArray
public void writeArray(List<?> l) throws IOException
- Throws:
IOException
-
writeArray
public void writeArray(Collection<?> coll) throws IOException
- Throws:
IOException
-
writeArray
public void writeArray(Object[] arr) throws IOException
- Throws:
IOException
-
readArray
public List<Object> readArray(DataInputInputStream dis) throws IOException
- Throws:
IOException
-
readArray
protected List readArray(DataInputInputStream dis, int sz) throws IOException
- Throws:
IOException
-
writeEnumFieldValue
public void writeEnumFieldValue(EnumFieldValue enumFieldValue) throws IOException
writeEnumFieldValue
as tag+int value+string value- Parameters:
enumFieldValue
- to write- Throws:
IOException
-
writeMapEntry
public void writeMapEntry(Map.Entry<?,?> val) throws IOException
- Throws:
IOException
-
readEnumFieldValue
public EnumFieldValue readEnumFieldValue(DataInputInputStream dis) throws IOException
readEnumFieldValue
(int+string) from input stream- Parameters:
dis
- data input stream- Returns:
EnumFieldValue
- Throws:
IOException
-
readMapEntry
public Map.Entry<Object,Object> readMapEntry(DataInputInputStream dis) throws IOException
- Throws:
IOException
-
writeStr
public void writeStr(CharSequence s) throws IOException
write the string as tag+length, with length being the number of UTF-8 bytes- Throws:
IOException
-
readStr
public CharSequence readStr(DataInputInputStream dis) throws IOException
- Throws:
IOException
-
readStr
public CharSequence readStr(DataInputInputStream dis, JavaBinCodec.StringCache stringCache, boolean readStringAsCharSeq) throws IOException
- Throws:
IOException
-
readUtf8
protected CharSequence readUtf8(DataInputInputStream dis) throws IOException
- Throws:
IOException
-
readUtf8
protected CharSequence readUtf8(DataInputInputStream dis, int sz) throws IOException
- Throws:
IOException
-
writeInt
public void writeInt(int val) throws IOException
- Throws:
IOException
-
readSmallInt
public int readSmallInt(DataInputInputStream dis) throws IOException
- Throws:
IOException
-
writeLong
public void writeLong(long val) throws IOException
- Throws:
IOException
-
readSmallLong
public long readSmallLong(DataInputInputStream dis) throws IOException
- Throws:
IOException
-
writeFloat
public void writeFloat(float val) throws IOException
- Throws:
IOException
-
writePrimitive
public boolean writePrimitive(Object val) throws IOException
- Throws:
IOException
-
writeBoolean
protected void writeBoolean(boolean val) throws IOException
- Throws:
IOException
-
writeDouble
protected void writeDouble(double val) throws IOException
- Throws:
IOException
-
writeMap
public void writeMap(Map<?,?> val) throws IOException
- Throws:
IOException
-
readSize
public int readSize(DataInputInputStream in) throws IOException
- Throws:
IOException
-
writeVInt
public static void writeVInt(int i, FastOutputStream out) throws IOException
Special method for variable length int (copied from lucene). Usually used for writing the length of a collection/array/map In most of the cases the length can be represented in one byte (length < 127) so it saves 3 bytes/object- Throws:
IOException
- If there is a low-level I/O error.
-
readVInt
public static int readVInt(DataInputInputStream in) throws IOException
The counterpart forwriteVInt(int, FastOutputStream)
- Throws:
IOException
- If there is a low-level I/O error.
-
writeVLong
public static void writeVLong(long i, FastOutputStream out) throws IOException
- Throws:
IOException
-
readVLong
public static long readVLong(DataInputInputStream in) throws IOException
- Throws:
IOException
-
writeExternString
public void writeExternString(CharSequence s) throws IOException
- Throws:
IOException
-
readExternString
public CharSequence readExternString(DataInputInputStream fis) throws IOException
- Throws:
IOException
-
writeUTF8Str
public void writeUTF8Str(Utf8CharSequence utf8) throws IOException
- Throws:
IOException
-
getTotalBytesWritten
public long getTotalBytesWritten()
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
-